Dan Brown

Renamed files to attachments

1 <?php namespace BookStack; 1 <?php namespace BookStack;
2 2
3 3
4 -class File extends Ownable 4 +class Attachment extends Ownable
5 { 5 {
6 protected $fillable = ['name', 'order']; 6 protected $fillable = ['name', 'order'];
7 7
...@@ -30,7 +30,7 @@ class File extends Ownable ...@@ -30,7 +30,7 @@ class File extends Ownable
30 */ 30 */
31 public function getUrl() 31 public function getUrl()
32 { 32 {
33 - return baseUrl('/files/' . $this->id); 33 + return baseUrl('/attachments/' . $this->id);
34 } 34 }
35 35
36 } 36 }
......
1 <?php namespace BookStack\Http\Controllers; 1 <?php namespace BookStack\Http\Controllers;
2 2
3 use BookStack\Exceptions\FileUploadException; 3 use BookStack\Exceptions\FileUploadException;
4 -use BookStack\File; 4 +use BookStack\Attachment;
5 use BookStack\Repos\PageRepo; 5 use BookStack\Repos\PageRepo;
6 -use BookStack\Services\FileService; 6 +use BookStack\Services\AttachmentService;
7 use Illuminate\Http\Request; 7 use Illuminate\Http\Request;
8 8
9 -use BookStack\Http\Requests; 9 +class AttachmentController extends Controller
10 -
11 -class FileController extends Controller
12 { 10 {
13 - protected $fileService; 11 + protected $attachmentService;
14 - protected $file; 12 + protected $attachment;
15 protected $pageRepo; 13 protected $pageRepo;
16 14
17 /** 15 /**
18 - * FileController constructor. 16 + * AttachmentController constructor.
19 - * @param FileService $fileService 17 + * @param AttachmentService $attachmentService
20 - * @param File $file 18 + * @param Attachment $attachment
21 * @param PageRepo $pageRepo 19 * @param PageRepo $pageRepo
22 */ 20 */
23 - public function __construct(FileService $fileService, File $file, PageRepo $pageRepo) 21 + public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo)
24 { 22 {
25 - $this->fileService = $fileService; 23 + $this->attachmentService = $attachmentService;
26 - $this->file = $file; 24 + $this->attachment = $attachment;
27 $this->pageRepo = $pageRepo; 25 $this->pageRepo = $pageRepo;
26 + parent::__construct();
28 } 27 }
29 28
30 29
31 /** 30 /**
32 - * Endpoint at which files are uploaded to. 31 + * Endpoint at which attachments are uploaded to.
33 * @param Request $request 32 * @param Request $request
33 + * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
34 */ 34 */
35 public function upload(Request $request) 35 public function upload(Request $request)
36 { 36 {
...@@ -42,27 +42,27 @@ class FileController extends Controller ...@@ -42,27 +42,27 @@ class FileController extends Controller
42 $pageId = $request->get('uploaded_to'); 42 $pageId = $request->get('uploaded_to');
43 $page = $this->pageRepo->getById($pageId); 43 $page = $this->pageRepo->getById($pageId);
44 44
45 - $this->checkPermission('file-create-all'); 45 + $this->checkPermission('attachment-create-all');
46 $this->checkOwnablePermission('page-update', $page); 46 $this->checkOwnablePermission('page-update', $page);
47 47
48 $uploadedFile = $request->file('file'); 48 $uploadedFile = $request->file('file');
49 49
50 try { 50 try {
51 - $file = $this->fileService->saveNewUpload($uploadedFile, $pageId); 51 + $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
52 } catch (FileUploadException $e) { 52 } catch (FileUploadException $e) {
53 return response($e->getMessage(), 500); 53 return response($e->getMessage(), 500);
54 } 54 }
55 55
56 - return response()->json($file); 56 + return response()->json($attachment);
57 } 57 }
58 58
59 /** 59 /**
60 - * Update an uploaded file. 60 + * Update an uploaded attachment.
61 - * @param int $fileId 61 + * @param int $attachmentId
62 * @param Request $request 62 * @param Request $request
63 * @return mixed 63 * @return mixed
64 */ 64 */
65 - public function uploadUpdate($fileId, Request $request) 65 + public function uploadUpdate($attachmentId, Request $request)
66 { 66 {
67 $this->validate($request, [ 67 $this->validate($request, [
68 'uploaded_to' => 'required|integer|exists:pages,id', 68 'uploaded_to' => 'required|integer|exists:pages,id',
...@@ -71,33 +71,33 @@ class FileController extends Controller ...@@ -71,33 +71,33 @@ class FileController extends Controller
71 71
72 $pageId = $request->get('uploaded_to'); 72 $pageId = $request->get('uploaded_to');
73 $page = $this->pageRepo->getById($pageId); 73 $page = $this->pageRepo->getById($pageId);
74 - $file = $this->file->findOrFail($fileId); 74 + $attachment = $this->attachment->findOrFail($attachmentId);
75 75
76 $this->checkOwnablePermission('page-update', $page); 76 $this->checkOwnablePermission('page-update', $page);
77 - $this->checkOwnablePermission('file-create', $file); 77 + $this->checkOwnablePermission('attachment-create', $attachment);
78 78
79 - if (intval($pageId) !== intval($file->uploaded_to)) { 79 + if (intval($pageId) !== intval($attachment->uploaded_to)) {
80 return $this->jsonError('Page mismatch during attached file update'); 80 return $this->jsonError('Page mismatch during attached file update');
81 } 81 }
82 82
83 $uploadedFile = $request->file('file'); 83 $uploadedFile = $request->file('file');
84 84
85 try { 85 try {
86 - $file = $this->fileService->saveUpdatedUpload($uploadedFile, $file); 86 + $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
87 } catch (FileUploadException $e) { 87 } catch (FileUploadException $e) {
88 return response($e->getMessage(), 500); 88 return response($e->getMessage(), 500);
89 } 89 }
90 90
91 - return response()->json($file); 91 + return response()->json($attachment);
92 } 92 }
93 93
94 /** 94 /**
95 * Update the details of an existing file. 95 * Update the details of an existing file.
96 - * @param $fileId 96 + * @param $attachmentId
97 * @param Request $request 97 * @param Request $request
98 - * @return File|mixed 98 + * @return Attachment|mixed
99 */ 99 */
100 - public function update($fileId, Request $request) 100 + public function update($attachmentId, Request $request)
101 { 101 {
102 $this->validate($request, [ 102 $this->validate($request, [
103 'uploaded_to' => 'required|integer|exists:pages,id', 103 'uploaded_to' => 'required|integer|exists:pages,id',
...@@ -107,21 +107,21 @@ class FileController extends Controller ...@@ -107,21 +107,21 @@ class FileController extends Controller
107 107
108 $pageId = $request->get('uploaded_to'); 108 $pageId = $request->get('uploaded_to');
109 $page = $this->pageRepo->getById($pageId); 109 $page = $this->pageRepo->getById($pageId);
110 - $file = $this->file->findOrFail($fileId); 110 + $attachment = $this->attachment->findOrFail($attachmentId);
111 111
112 $this->checkOwnablePermission('page-update', $page); 112 $this->checkOwnablePermission('page-update', $page);
113 - $this->checkOwnablePermission('file-create', $file); 113 + $this->checkOwnablePermission('attachment-create', $attachment);
114 114
115 - if (intval($pageId) !== intval($file->uploaded_to)) { 115 + if (intval($pageId) !== intval($attachment->uploaded_to)) {
116 return $this->jsonError('Page mismatch during attachment update'); 116 return $this->jsonError('Page mismatch during attachment update');
117 } 117 }
118 118
119 - $file = $this->fileService->updateFile($file, $request->all()); 119 + $attachment = $this->attachmentService->updateFile($attachment, $request->all());
120 - return $file; 120 + return $attachment;
121 } 121 }
122 122
123 /** 123 /**
124 - * Attach a link to a page as a file. 124 + * Attach a link to a page.
125 * @param Request $request 125 * @param Request $request
126 * @return mixed 126 * @return mixed
127 */ 127 */
...@@ -136,18 +136,18 @@ class FileController extends Controller ...@@ -136,18 +136,18 @@ class FileController extends Controller
136 $pageId = $request->get('uploaded_to'); 136 $pageId = $request->get('uploaded_to');
137 $page = $this->pageRepo->getById($pageId); 137 $page = $this->pageRepo->getById($pageId);
138 138
139 - $this->checkPermission('file-create-all'); 139 + $this->checkPermission('attachment-create-all');
140 $this->checkOwnablePermission('page-update', $page); 140 $this->checkOwnablePermission('page-update', $page);
141 141
142 - $fileName = $request->get('name'); 142 + $attachmentName = $request->get('name');
143 $link = $request->get('link'); 143 $link = $request->get('link');
144 - $file = $this->fileService->saveNewFromLink($fileName, $link, $pageId); 144 + $attachment = $this->attachmentService->saveNewFromLink($attachmentName, $link, $pageId);
145 145
146 - return response()->json($file); 146 + return response()->json($attachment);
147 } 147 }
148 148
149 /** 149 /**
150 - * Get the files for a specific page. 150 + * Get the attachments for a specific page.
151 * @param $pageId 151 * @param $pageId
152 * @return mixed 152 * @return mixed
153 */ 153 */
...@@ -159,7 +159,7 @@ class FileController extends Controller ...@@ -159,7 +159,7 @@ class FileController extends Controller
159 } 159 }
160 160
161 /** 161 /**
162 - * Update the file sorting. 162 + * Update the attachment sorting.
163 * @param $pageId 163 * @param $pageId
164 * @param Request $request 164 * @param Request $request
165 * @return mixed 165 * @return mixed
...@@ -173,42 +173,43 @@ class FileController extends Controller ...@@ -173,42 +173,43 @@ class FileController extends Controller
173 $page = $this->pageRepo->getById($pageId); 173 $page = $this->pageRepo->getById($pageId);
174 $this->checkOwnablePermission('page-update', $page); 174 $this->checkOwnablePermission('page-update', $page);
175 175
176 - $files = $request->get('files'); 176 + $attachments = $request->get('files');
177 - $this->fileService->updateFileOrderWithinPage($files, $pageId); 177 + $this->attachmentService->updateFileOrderWithinPage($attachments, $pageId);
178 return response()->json(['message' => 'Attachment order updated']); 178 return response()->json(['message' => 'Attachment order updated']);
179 } 179 }
180 180
181 /** 181 /**
182 - * Get a file from storage. 182 + * Get an attachment from storage.
183 - * @param $fileId 183 + * @param $attachmentId
184 + * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Symfony\Component\HttpFoundation\Response
184 */ 185 */
185 - public function get($fileId) 186 + public function get($attachmentId)
186 { 187 {
187 - $file = $this->file->findOrFail($fileId); 188 + $attachment = $this->attachment->findOrFail($attachmentId);
188 - $page = $this->pageRepo->getById($file->uploaded_to); 189 + $page = $this->pageRepo->getById($attachment->uploaded_to);
189 $this->checkOwnablePermission('page-view', $page); 190 $this->checkOwnablePermission('page-view', $page);
190 191
191 - if ($file->external) { 192 + if ($attachment->external) {
192 - return redirect($file->path); 193 + return redirect($attachment->path);
193 } 194 }
194 195
195 - $fileContents = $this->fileService->getFile($file); 196 + $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
196 - return response($fileContents, 200, [ 197 + return response($attachmentContents, 200, [
197 'Content-Type' => 'application/octet-stream', 198 'Content-Type' => 'application/octet-stream',
198 - 'Content-Disposition' => 'attachment; filename="'. $file->getFileName() .'"' 199 + 'Content-Disposition' => 'attachment; filename="'. $attachment->getFileName() .'"'
199 ]); 200 ]);
200 } 201 }
201 202
202 /** 203 /**
203 - * Delete a specific file in the system. 204 + * Delete a specific attachment in the system.
204 - * @param $fileId 205 + * @param $attachmentId
205 * @return mixed 206 * @return mixed
206 */ 207 */
207 - public function delete($fileId) 208 + public function delete($attachmentId)
208 { 209 {
209 - $file = $this->file->findOrFail($fileId); 210 + $attachment = $this->attachment->findOrFail($attachmentId);
210 - $this->checkOwnablePermission('file-delete', $file); 211 + $this->checkOwnablePermission('attachment-delete', $attachment);
211 - $this->fileService->deleteFile($file); 212 + $this->attachmentService->deleteFile($attachment);
212 return response()->json(['message' => 'Attachment deleted']); 213 return response()->json(['message' => 'Attachment deleted']);
213 } 214 }
214 } 215 }
......
...@@ -55,12 +55,12 @@ class Page extends Entity ...@@ -55,12 +55,12 @@ class Page extends Entity
55 } 55 }
56 56
57 /** 57 /**
58 - * Get the files attached to this page. 58 + * Get the attachments assigned to this page.
59 * @return \Illuminate\Database\Eloquent\Relations\HasMany 59 * @return \Illuminate\Database\Eloquent\Relations\HasMany
60 */ 60 */
61 - public function files() 61 + public function attachments()
62 { 62 {
63 - return $this->hasMany(File::class, 'uploaded_to')->orderBy('order', 'asc'); 63 + return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
64 } 64 }
65 65
66 /** 66 /**
......
...@@ -5,7 +5,7 @@ use BookStack\Book; ...@@ -5,7 +5,7 @@ use BookStack\Book;
5 use BookStack\Chapter; 5 use BookStack\Chapter;
6 use BookStack\Entity; 6 use BookStack\Entity;
7 use BookStack\Exceptions\NotFoundException; 7 use BookStack\Exceptions\NotFoundException;
8 -use BookStack\Services\FileService; 8 +use BookStack\Services\AttachmentService;
9 use Carbon\Carbon; 9 use Carbon\Carbon;
10 use DOMDocument; 10 use DOMDocument;
11 use DOMXPath; 11 use DOMXPath;
...@@ -636,9 +636,9 @@ class PageRepo extends EntityRepo ...@@ -636,9 +636,9 @@ class PageRepo extends EntityRepo
636 $this->permissionService->deleteJointPermissionsForEntity($page); 636 $this->permissionService->deleteJointPermissionsForEntity($page);
637 637
638 // Delete AttachedFiles 638 // Delete AttachedFiles
639 - $fileService = app(FileService::class); 639 + $attachmentService = app(AttachmentService::class);
640 - foreach ($page->files as $file) { 640 + foreach ($page->attachments as $attachment) {
641 - $fileService->deleteFile($file); 641 + $attachmentService->deleteFile($attachment);
642 } 642 }
643 643
644 $page->delete(); 644 $page->delete();
...@@ -647,6 +647,7 @@ class PageRepo extends EntityRepo ...@@ -647,6 +647,7 @@ class PageRepo extends EntityRepo
647 /** 647 /**
648 * Get the latest pages added to the system. 648 * Get the latest pages added to the system.
649 * @param $count 649 * @param $count
650 + * @return mixed
650 */ 651 */
651 public function getRecentlyCreatedPaginated($count = 20) 652 public function getRecentlyCreatedPaginated($count = 20)
652 { 653 {
...@@ -656,6 +657,7 @@ class PageRepo extends EntityRepo ...@@ -656,6 +657,7 @@ class PageRepo extends EntityRepo
656 /** 657 /**
657 * Get the latest pages added to the system. 658 * Get the latest pages added to the system.
658 * @param $count 659 * @param $count
660 + * @return mixed
659 */ 661 */
660 public function getRecentlyUpdatedPaginated($count = 20) 662 public function getRecentlyUpdatedPaginated($count = 20)
661 { 663 {
......
1 <?php namespace BookStack\Services; 1 <?php namespace BookStack\Services;
2 2
3 -
4 use BookStack\Exceptions\FileUploadException; 3 use BookStack\Exceptions\FileUploadException;
5 -use BookStack\File; 4 +use BookStack\Attachment;
6 use Exception; 5 use Exception;
7 -use Illuminate\Contracts\Filesystem\FileNotFoundException;
8 -use Illuminate\Support\Collection;
9 use Symfony\Component\HttpFoundation\File\UploadedFile; 6 use Symfony\Component\HttpFoundation\File\UploadedFile;
10 7
11 -class FileService extends UploadService 8 +class AttachmentService extends UploadService
12 { 9 {
13 10
14 /** 11 /**
15 - * Get a file from storage. 12 + * Get an attachment from storage.
16 - * @param File $file 13 + * @param Attachment $attachment
17 * @return string 14 * @return string
18 */ 15 */
19 - public function getFile(File $file) 16 + public function getAttachmentFromStorage(Attachment $attachment)
20 { 17 {
21 - $filePath = $this->getStorageBasePath() . $file->path; 18 + $attachmentPath = $this->getStorageBasePath() . $attachment->path;
22 - return $this->getStorage()->get($filePath); 19 + return $this->getStorage()->get($attachmentPath);
23 } 20 }
24 21
25 /** 22 /**
26 - * Store a new file upon user upload. 23 + * Store a new attachment upon user upload.
27 * @param UploadedFile $uploadedFile 24 * @param UploadedFile $uploadedFile
28 * @param int $page_id 25 * @param int $page_id
29 - * @return File 26 + * @return Attachment
30 * @throws FileUploadException 27 * @throws FileUploadException
31 */ 28 */
32 public function saveNewUpload(UploadedFile $uploadedFile, $page_id) 29 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
33 { 30 {
34 - $fileName = $uploadedFile->getClientOriginalName(); 31 + $attachmentName = $uploadedFile->getClientOriginalName();
35 - $filePath = $this->putFileInStorage($fileName, $uploadedFile); 32 + $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
36 - $largestExistingOrder = File::where('uploaded_to', '=', $page_id)->max('order'); 33 + $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
37 34
38 - $file = File::forceCreate([ 35 + $attachment = Attachment::forceCreate([
39 - 'name' => $fileName, 36 + 'name' => $attachmentName,
40 - 'path' => $filePath, 37 + 'path' => $attachmentPath,
41 'extension' => $uploadedFile->getClientOriginalExtension(), 38 'extension' => $uploadedFile->getClientOriginalExtension(),
42 'uploaded_to' => $page_id, 39 'uploaded_to' => $page_id,
43 'created_by' => user()->id, 40 'created_by' => user()->id,
...@@ -45,32 +42,32 @@ class FileService extends UploadService ...@@ -45,32 +42,32 @@ class FileService extends UploadService
45 'order' => $largestExistingOrder + 1 42 'order' => $largestExistingOrder + 1
46 ]); 43 ]);
47 44
48 - return $file; 45 + return $attachment;
49 } 46 }
50 47
51 /** 48 /**
52 * Store a upload, saving to a file and deleting any existing uploads 49 * Store a upload, saving to a file and deleting any existing uploads
53 * attached to that file. 50 * attached to that file.
54 * @param UploadedFile $uploadedFile 51 * @param UploadedFile $uploadedFile
55 - * @param File $file 52 + * @param Attachment $attachment
56 - * @return File 53 + * @return Attachment
57 * @throws FileUploadException 54 * @throws FileUploadException
58 */ 55 */
59 - public function saveUpdatedUpload(UploadedFile $uploadedFile, File $file) 56 + public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
60 { 57 {
61 - if (!$file->external) { 58 + if (!$attachment->external) {
62 - $this->deleteFileInStorage($file); 59 + $this->deleteFileInStorage($attachment);
63 } 60 }
64 61
65 - $fileName = $uploadedFile->getClientOriginalName(); 62 + $attachmentName = $uploadedFile->getClientOriginalName();
66 - $filePath = $this->putFileInStorage($fileName, $uploadedFile); 63 + $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
67 64
68 - $file->name = $fileName; 65 + $attachment->name = $attachmentName;
69 - $file->path = $filePath; 66 + $attachment->path = $attachmentPath;
70 - $file->external = false; 67 + $attachment->external = false;
71 - $file->extension = $uploadedFile->getClientOriginalExtension(); 68 + $attachment->extension = $uploadedFile->getClientOriginalExtension();
72 - $file->save(); 69 + $attachment->save();
73 - return $file; 70 + return $attachment;
74 } 71 }
75 72
76 /** 73 /**
...@@ -78,12 +75,12 @@ class FileService extends UploadService ...@@ -78,12 +75,12 @@ class FileService extends UploadService
78 * @param string $name 75 * @param string $name
79 * @param string $link 76 * @param string $link
80 * @param int $page_id 77 * @param int $page_id
81 - * @return File 78 + * @return Attachment
82 */ 79 */
83 public function saveNewFromLink($name, $link, $page_id) 80 public function saveNewFromLink($name, $link, $page_id)
84 { 81 {
85 - $largestExistingOrder = File::where('uploaded_to', '=', $page_id)->max('order'); 82 + $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
86 - return File::forceCreate([ 83 + return Attachment::forceCreate([
87 'name' => $name, 84 'name' => $name,
88 'path' => $link, 85 'path' => $link,
89 'external' => true, 86 'external' => true,
...@@ -107,60 +104,60 @@ class FileService extends UploadService ...@@ -107,60 +104,60 @@ class FileService extends UploadService
107 104
108 /** 105 /**
109 * Updates the file ordering for a listing of attached files. 106 * Updates the file ordering for a listing of attached files.
110 - * @param array $fileList 107 + * @param array $attachmentList
111 * @param $pageId 108 * @param $pageId
112 */ 109 */
113 - public function updateFileOrderWithinPage($fileList, $pageId) 110 + public function updateFileOrderWithinPage($attachmentList, $pageId)
114 { 111 {
115 - foreach ($fileList as $index => $file) { 112 + foreach ($attachmentList as $index => $attachment) {
116 - File::where('uploaded_to', '=', $pageId)->where('id', '=', $file['id'])->update(['order' => $index]); 113 + Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
117 } 114 }
118 } 115 }
119 116
120 117
121 /** 118 /**
122 * Update the details of a file. 119 * Update the details of a file.
123 - * @param File $file 120 + * @param Attachment $attachment
124 * @param $requestData 121 * @param $requestData
125 - * @return File 122 + * @return Attachment
126 */ 123 */
127 - public function updateFile(File $file, $requestData) 124 + public function updateFile(Attachment $attachment, $requestData)
128 { 125 {
129 - $file->name = $requestData['name']; 126 + $attachment->name = $requestData['name'];
130 if (isset($requestData['link']) && trim($requestData['link']) !== '') { 127 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
131 - $file->path = $requestData['link']; 128 + $attachment->path = $requestData['link'];
132 - if (!$file->external) { 129 + if (!$attachment->external) {
133 - $this->deleteFileInStorage($file); 130 + $this->deleteFileInStorage($attachment);
134 - $file->external = true; 131 + $attachment->external = true;
135 } 132 }
136 } 133 }
137 - $file->save(); 134 + $attachment->save();
138 - return $file; 135 + return $attachment;
139 } 136 }
140 137
141 /** 138 /**
142 * Delete a File from the database and storage. 139 * Delete a File from the database and storage.
143 - * @param File $file 140 + * @param Attachment $attachment
144 */ 141 */
145 - public function deleteFile(File $file) 142 + public function deleteFile(Attachment $attachment)
146 { 143 {
147 - if ($file->external) { 144 + if ($attachment->external) {
148 - $file->delete(); 145 + $attachment->delete();
149 return; 146 return;
150 } 147 }
151 148
152 - $this->deleteFileInStorage($file); 149 + $this->deleteFileInStorage($attachment);
153 - $file->delete(); 150 + $attachment->delete();
154 } 151 }
155 152
156 /** 153 /**
157 * Delete a file from the filesystem it sits on. 154 * Delete a file from the filesystem it sits on.
158 * Cleans any empty leftover folders. 155 * Cleans any empty leftover folders.
159 - * @param File $file 156 + * @param Attachment $attachment
160 */ 157 */
161 - protected function deleteFileInStorage(File $file) 158 + protected function deleteFileInStorage(Attachment $attachment)
162 { 159 {
163 - $storedFilePath = $this->getStorageBasePath() . $file->path; 160 + $storedFilePath = $this->getStorageBasePath() . $attachment->path;
164 $storage = $this->getStorage(); 161 $storage = $this->getStorage();
165 $dirPath = dirname($storedFilePath); 162 $dirPath = dirname($storedFilePath);
166 163
...@@ -172,33 +169,33 @@ class FileService extends UploadService ...@@ -172,33 +169,33 @@ class FileService extends UploadService
172 169
173 /** 170 /**
174 * Store a file in storage with the given filename 171 * Store a file in storage with the given filename
175 - * @param $fileName 172 + * @param $attachmentName
176 * @param UploadedFile $uploadedFile 173 * @param UploadedFile $uploadedFile
177 * @return string 174 * @return string
178 * @throws FileUploadException 175 * @throws FileUploadException
179 */ 176 */
180 - protected function putFileInStorage($fileName, UploadedFile $uploadedFile) 177 + protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
181 { 178 {
182 - $fileData = file_get_contents($uploadedFile->getRealPath()); 179 + $attachmentData = file_get_contents($uploadedFile->getRealPath());
183 180
184 $storage = $this->getStorage(); 181 $storage = $this->getStorage();
185 - $fileBasePath = 'uploads/files/' . Date('Y-m-M') . '/'; 182 + $attachmentBasePath = 'uploads/files/' . Date('Y-m-M') . '/';
186 - $storageBasePath = $this->getStorageBasePath() . $fileBasePath; 183 + $storageBasePath = $this->getStorageBasePath() . $attachmentBasePath;
187 184
188 - $uploadFileName = $fileName; 185 + $uploadFileName = $attachmentName;
189 while ($storage->exists($storageBasePath . $uploadFileName)) { 186 while ($storage->exists($storageBasePath . $uploadFileName)) {
190 $uploadFileName = str_random(3) . $uploadFileName; 187 $uploadFileName = str_random(3) . $uploadFileName;
191 } 188 }
192 189
193 - $filePath = $fileBasePath . $uploadFileName; 190 + $attachmentPath = $attachmentBasePath . $uploadFileName;
194 - $fileStoragePath = $this->getStorageBasePath() . $filePath; 191 + $attachmentStoragePath = $this->getStorageBasePath() . $attachmentPath;
195 192
196 try { 193 try {
197 - $storage->put($fileStoragePath, $fileData); 194 + $storage->put($attachmentStoragePath, $attachmentData);
198 } catch (Exception $e) { 195 } catch (Exception $e) {
199 - throw new FileUploadException('File path ' . $fileStoragePath . ' could not be uploaded to. Ensure it is writable to the server.'); 196 + throw new FileUploadException('File path ' . $attachmentStoragePath . ' could not be uploaded to. Ensure it is writable to the server.');
200 } 197 }
201 - return $filePath; 198 + return $attachmentPath;
202 } 199 }
203 200
204 } 201 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; ...@@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint; 4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration; 5 use Illuminate\Database\Migrations\Migration;
6 6
7 -class CreateFilesTable extends Migration 7 +class CreateAttachmentsTable extends Migration
8 { 8 {
9 /** 9 /**
10 * Run the migrations. 10 * Run the migrations.
...@@ -13,7 +13,7 @@ class CreateFilesTable extends Migration ...@@ -13,7 +13,7 @@ class CreateFilesTable extends Migration
13 */ 13 */
14 public function up() 14 public function up()
15 { 15 {
16 - Schema::create('files', function (Blueprint $table) { 16 + Schema::create('attachments', function (Blueprint $table) {
17 $table->increments('id'); 17 $table->increments('id');
18 $table->string('name'); 18 $table->string('name');
19 $table->string('path'); 19 $table->string('path');
...@@ -35,7 +35,7 @@ class CreateFilesTable extends Migration ...@@ -35,7 +35,7 @@ class CreateFilesTable extends Migration
35 35
36 // Create & attach new entity permissions 36 // Create & attach new entity permissions
37 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own']; 37 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
38 - $entity = 'File'; 38 + $entity = 'Attachment';
39 foreach ($ops as $op) { 39 foreach ($ops as $op) {
40 $permissionId = DB::table('role_permissions')->insertGetId([ 40 $permissionId = DB::table('role_permissions')->insertGetId([
41 'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)), 41 'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
...@@ -58,11 +58,11 @@ class CreateFilesTable extends Migration ...@@ -58,11 +58,11 @@ class CreateFilesTable extends Migration
58 */ 58 */
59 public function down() 59 public function down()
60 { 60 {
61 - Schema::dropIfExists('files'); 61 + Schema::dropIfExists('attachments');
62 62
63 // Create & attach new entity permissions 63 // Create & attach new entity permissions
64 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own']; 64 $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
65 - $entity = 'File'; 65 + $entity = 'Attachment';
66 foreach ($ops as $op) { 66 foreach ($ops as $op) {
67 $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)); 67 $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
68 DB::table('role_permissions')->where('name', '=', $permName)->delete(); 68 DB::table('role_permissions')->where('name', '=', $permName)->delete();
......
...@@ -570,7 +570,7 @@ export default function (ngApp, events) { ...@@ -570,7 +570,7 @@ export default function (ngApp, events) {
570 if (newOrder === currentOrder) return; 570 if (newOrder === currentOrder) return;
571 571
572 currentOrder = newOrder; 572 currentOrder = newOrder;
573 - $http.put(window.baseUrl(`/files/sort/page/${pageId}`), {files: $scope.files}).then(resp => { 573 + $http.put(window.baseUrl(`/attachments/sort/page/${pageId}`), {files: $scope.files}).then(resp => {
574 events.emit('success', resp.data.message); 574 events.emit('success', resp.data.message);
575 }, checkError('sort')); 575 }, checkError('sort'));
576 } 576 }
...@@ -581,14 +581,14 @@ export default function (ngApp, events) { ...@@ -581,14 +581,14 @@ export default function (ngApp, events) {
581 */ 581 */
582 $scope.getUploadUrl = function (file) { 582 $scope.getUploadUrl = function (file) {
583 let suffix = (typeof file !== 'undefined') ? `/${file.id}` : ''; 583 let suffix = (typeof file !== 'undefined') ? `/${file.id}` : '';
584 - return window.baseUrl(`/files/upload${suffix}`); 584 + return window.baseUrl(`/attachments/upload${suffix}`);
585 }; 585 };
586 586
587 /** 587 /**
588 * Get files for the current page from the server. 588 * Get files for the current page from the server.
589 */ 589 */
590 function getFiles() { 590 function getFiles() {
591 - let url = window.baseUrl(`/files/get/page/${pageId}`) 591 + let url = window.baseUrl(`/attachments/get/page/${pageId}`)
592 $http.get(url).then(resp => { 592 $http.get(url).then(resp => {
593 $scope.files = resp.data; 593 $scope.files = resp.data;
594 currentOrder = resp.data.map(file => {return file.id}).join(':'); 594 currentOrder = resp.data.map(file => {return file.id}).join(':');
...@@ -636,7 +636,7 @@ export default function (ngApp, events) { ...@@ -636,7 +636,7 @@ export default function (ngApp, events) {
636 file.deleting = true; 636 file.deleting = true;
637 return; 637 return;
638 } 638 }
639 - $http.delete(window.baseUrl(`/files/${file.id}`)).then(resp => { 639 + $http.delete(window.baseUrl(`/attachments/${file.id}`)).then(resp => {
640 events.emit('success', resp.data.message); 640 events.emit('success', resp.data.message);
641 $scope.files.splice($scope.files.indexOf(file), 1); 641 $scope.files.splice($scope.files.indexOf(file), 1);
642 }, checkError('delete')); 642 }, checkError('delete'));
...@@ -648,7 +648,7 @@ export default function (ngApp, events) { ...@@ -648,7 +648,7 @@ export default function (ngApp, events) {
648 */ 648 */
649 $scope.attachLinkSubmit = function(file) { 649 $scope.attachLinkSubmit = function(file) {
650 file.uploaded_to = pageId; 650 file.uploaded_to = pageId;
651 - $http.post(window.baseUrl('/files/link'), file).then(resp => { 651 + $http.post(window.baseUrl('/attachments/link'), file).then(resp => {
652 $scope.files.push(resp.data); 652 $scope.files.push(resp.data);
653 events.emit('success', 'Link attached'); 653 events.emit('success', 'Link attached');
654 $scope.file = getCleanFile(); 654 $scope.file = getCleanFile();
...@@ -676,7 +676,7 @@ export default function (ngApp, events) { ...@@ -676,7 +676,7 @@ export default function (ngApp, events) {
676 * @param file 676 * @param file
677 */ 677 */
678 $scope.updateFile = function(file) { 678 $scope.updateFile = function(file) {
679 - $http.put(window.baseUrl(`/files/${file.id}`), file).then(resp => { 679 + $http.put(window.baseUrl(`/attachments/${file.id}`), file).then(resp => {
680 let search = filesIndexOf(resp.data); 680 let search = filesIndexOf(resp.data);
681 if (search !== -1) $scope.files[search] = resp.data; 681 if (search !== -1) $scope.files[search] = resp.data;
682 682
...@@ -692,7 +692,7 @@ export default function (ngApp, events) { ...@@ -692,7 +692,7 @@ export default function (ngApp, events) {
692 * Get the url of a file. 692 * Get the url of a file.
693 */ 693 */
694 $scope.getFileUrl = function(file) { 694 $scope.getFileUrl = function(file) {
695 - return window.baseUrl('/files/' + file.id); 695 + return window.baseUrl('/attachments/' + file.id);
696 }; 696 };
697 697
698 /** 698 /**
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
4 <div class="tabs primary-background-light"> 4 <div class="tabs primary-background-light">
5 <span toolbox-toggle><i class="zmdi zmdi-caret-left-circle"></i></span> 5 <span toolbox-toggle><i class="zmdi zmdi-caret-left-circle"></i></span>
6 <span toolbox-tab-button="tags" title="Page Tags" class="active"><i class="zmdi zmdi-tag"></i></span> 6 <span toolbox-tab-button="tags" title="Page Tags" class="active"><i class="zmdi zmdi-tag"></i></span>
7 - @if(userCan('file-create-all')) 7 + @if(userCan('attachment-create-all'))
8 <span toolbox-tab-button="files" title="Attachments"><i class="zmdi zmdi-attachment"></i></span> 8 <span toolbox-tab-button="files" title="Attachments"><i class="zmdi zmdi-attachment"></i></span>
9 @endif 9 @endif
10 </div> 10 </div>
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
37 </div> 37 </div>
38 </div> 38 </div>
39 39
40 - @if(userCan('file-create-all')) 40 + @if(userCan('attachment-create-all'))
41 <div toolbox-tab-content="files" ng-controller="PageAttachmentController" page-id="{{ $page->id or 0 }}"> 41 <div toolbox-tab-content="files" ng-controller="PageAttachmentController" page-id="{{ $page->id or 0 }}">
42 <h4>Attachments</h4> 42 <h4>Attachments</h4>
43 <div class="padded files"> 43 <div class="padded files">
......
1 1
2 <div class="book-tree" ng-non-bindable> 2 <div class="book-tree" ng-non-bindable>
3 3
4 - @if (isset($page) && $page->files->count() > 0) 4 + @if (isset($page) && $page->attachments->count() > 0)
5 <h6 class="text-muted">Attachments</h6> 5 <h6 class="text-muted">Attachments</h6>
6 - @foreach($page->files as $file) 6 + @foreach($page->attachments as $attachment)
7 <div class="attachment"> 7 <div class="attachment">
8 - <a href="{{ $file->getUrl() }}" @if($file->external) target="_blank" @endif><i class="zmdi zmdi-{{ $file->external ? 'open-in-new' : 'file' }}"></i> {{ $file->name }}</a> 8 + <a href="{{ $attachment->getUrl() }}" @if($attachment->external) target="_blank" @endif><i class="zmdi zmdi-{{ $attachment->external ? 'open-in-new' : 'file' }}"></i> {{ $attachment->name }}</a>
9 </div> 9 </div>
10 @endforeach 10 @endforeach
11 @endif 11 @endif
......
...@@ -107,16 +107,16 @@ ...@@ -107,16 +107,16 @@
107 </td> 107 </td>
108 </tr> 108 </tr>
109 <tr> 109 <tr>
110 - <td>Attached <br>Files</td> 110 + <td>Attachments</td>
111 - <td>@include('settings/roles/checkbox', ['permission' => 'file-create-all'])</td> 111 + <td>@include('settings/roles/checkbox', ['permission' => 'attachment-create-all'])</td>
112 <td style="line-height:1.2;"><small class="faded">Controlled by the asset they are uploaded to</small></td> 112 <td style="line-height:1.2;"><small class="faded">Controlled by the asset they are uploaded to</small></td>
113 <td> 113 <td>
114 - <label>@include('settings/roles/checkbox', ['permission' => 'file-update-own']) Own</label> 114 + <label>@include('settings/roles/checkbox', ['permission' => 'attachment-update-own']) Own</label>
115 - <label>@include('settings/roles/checkbox', ['permission' => 'file-update-all']) All</label> 115 + <label>@include('settings/roles/checkbox', ['permission' => 'attachment-update-all']) All</label>
116 </td> 116 </td>
117 <td> 117 <td>
118 - <label>@include('settings/roles/checkbox', ['permission' => 'file-delete-own']) Own</label> 118 + <label>@include('settings/roles/checkbox', ['permission' => 'attachment-delete-own']) Own</label>
119 - <label>@include('settings/roles/checkbox', ['permission' => 'file-delete-all']) All</label> 119 + <label>@include('settings/roles/checkbox', ['permission' => 'attachment-delete-all']) All</label>
120 </td> 120 </td>
121 </tr> 121 </tr>
122 </table> 122 </table>
......
...@@ -87,15 +87,15 @@ Route::group(['middleware' => 'auth'], function () { ...@@ -87,15 +87,15 @@ Route::group(['middleware' => 'auth'], function () {
87 Route::delete('/{imageId}', 'ImageController@destroy'); 87 Route::delete('/{imageId}', 'ImageController@destroy');
88 }); 88 });
89 89
90 - // File routes 90 + // Attachments routes
91 - Route::get('/files/{id}', 'FileController@get'); 91 + Route::get('/attachments/{id}', 'AttachmentController@get');
92 - Route::post('/files/upload', 'FileController@upload'); 92 + Route::post('/attachments/upload', 'AttachmentController@upload');
93 - Route::post('/files/upload/{id}', 'FileController@uploadUpdate'); 93 + Route::post('/attachments/upload/{id}', 'AttachmentController@uploadUpdate');
94 - Route::post('/files/link', 'FileController@attachLink'); 94 + Route::post('/attachments/link', 'AttachmentController@attachLink');
95 - Route::put('/files/{id}', 'FileController@update'); 95 + Route::put('/attachments/{id}', 'AttachmentController@update');
96 - Route::get('/files/get/page/{pageId}', 'FileController@listForPage'); 96 + Route::get('/attachments/get/page/{pageId}', 'AttachmentController@listForPage');
97 - Route::put('/files/sort/page/{pageId}', 'FileController@sortForPage'); 97 + Route::put('/attachments/sort/page/{pageId}', 'AttachmentController@sortForPage');
98 - Route::delete('/files/{id}', 'FileController@delete'); 98 + Route::delete('/attachments/{id}', 'AttachmentController@delete');
99 99
100 // AJAX routes 100 // AJAX routes
101 Route::put('/ajax/page/{id}/save-draft', 'PageController@saveDraft'); 101 Route::put('/ajax/page/{id}/save-draft', 'PageController@saveDraft');
......
...@@ -21,7 +21,7 @@ class AttachmentTest extends TestCase ...@@ -21,7 +21,7 @@ class AttachmentTest extends TestCase
21 protected function uploadFile($name, $uploadedTo = 0) 21 protected function uploadFile($name, $uploadedTo = 0)
22 { 22 {
23 $file = $this->getTestFile($name); 23 $file = $this->getTestFile($name);
24 - return $this->call('POST', '/files/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []); 24 + return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
25 } 25 }
26 26
27 /** 27 /**
...@@ -40,8 +40,8 @@ class AttachmentTest extends TestCase ...@@ -40,8 +40,8 @@ class AttachmentTest extends TestCase
40 */ 40 */
41 protected function deleteUploads() 41 protected function deleteUploads()
42 { 42 {
43 - $fileService = $this->app->make(\BookStack\Services\FileService::class); 43 + $fileService = $this->app->make(\BookStack\Services\AttachmentService::class);
44 - foreach (\BookStack\File::all() as $file) { 44 + foreach (\BookStack\Attachment::all() as $file) {
45 $fileService->deleteFile($file); 45 $fileService->deleteFile($file);
46 } 46 }
47 } 47 }
...@@ -66,7 +66,7 @@ class AttachmentTest extends TestCase ...@@ -66,7 +66,7 @@ class AttachmentTest extends TestCase
66 $this->uploadFile($fileName, $page->id); 66 $this->uploadFile($fileName, $page->id);
67 $this->assertResponseOk(); 67 $this->assertResponseOk();
68 $this->seeJsonContains($expectedResp); 68 $this->seeJsonContains($expectedResp);
69 - $this->seeInDatabase('files', $expectedResp); 69 + $this->seeInDatabase('attachments', $expectedResp);
70 70
71 $this->deleteUploads(); 71 $this->deleteUploads();
72 } 72 }
...@@ -94,7 +94,7 @@ class AttachmentTest extends TestCase ...@@ -94,7 +94,7 @@ class AttachmentTest extends TestCase
94 $admin = $this->getAdmin(); 94 $admin = $this->getAdmin();
95 $this->asAdmin(); 95 $this->asAdmin();
96 96
97 - $this->call('POST', 'files/link', [ 97 + $this->call('POST', 'attachments/link', [
98 'link' => 'https://example.com', 98 'link' => 'https://example.com',
99 'name' => 'Example Attachment Link', 99 'name' => 'Example Attachment Link',
100 'uploaded_to' => $page->id, 100 'uploaded_to' => $page->id,
...@@ -113,7 +113,7 @@ class AttachmentTest extends TestCase ...@@ -113,7 +113,7 @@ class AttachmentTest extends TestCase
113 113
114 $this->assertResponseOk(); 114 $this->assertResponseOk();
115 $this->seeJsonContains($expectedResp); 115 $this->seeJsonContains($expectedResp);
116 - $this->seeInDatabase('files', $expectedResp); 116 + $this->seeInDatabase('attachments', $expectedResp);
117 117
118 $this->visit($page->getUrl())->seeLink('Example Attachment Link') 118 $this->visit($page->getUrl())->seeLink('Example Attachment Link')
119 ->click('Example Attachment Link')->seePageIs('https://example.com'); 119 ->click('Example Attachment Link')->seePageIs('https://example.com');
...@@ -126,15 +126,15 @@ class AttachmentTest extends TestCase ...@@ -126,15 +126,15 @@ class AttachmentTest extends TestCase
126 $page = \BookStack\Page::first(); 126 $page = \BookStack\Page::first();
127 $this->asAdmin(); 127 $this->asAdmin();
128 128
129 - $this->call('POST', 'files/link', [ 129 + $this->call('POST', 'attachments/link', [
130 'link' => 'https://example.com', 130 'link' => 'https://example.com',
131 'name' => 'Example Attachment Link', 131 'name' => 'Example Attachment Link',
132 'uploaded_to' => $page->id, 132 'uploaded_to' => $page->id,
133 ]); 133 ]);
134 134
135 - $attachmentId = \BookStack\File::first()->id; 135 + $attachmentId = \BookStack\Attachment::first()->id;
136 136
137 - $this->call('PUT', 'files/' . $attachmentId, [ 137 + $this->call('PUT', 'attachments/' . $attachmentId, [
138 'uploaded_to' => $page->id, 138 'uploaded_to' => $page->id,
139 'name' => 'My new attachment name', 139 'name' => 'My new attachment name',
140 'link' => 'https://test.example.com' 140 'link' => 'https://test.example.com'
...@@ -148,7 +148,7 @@ class AttachmentTest extends TestCase ...@@ -148,7 +148,7 @@ class AttachmentTest extends TestCase
148 148
149 $this->assertResponseOk(); 149 $this->assertResponseOk();
150 $this->seeJsonContains($expectedResp); 150 $this->seeJsonContains($expectedResp);
151 - $this->seeInDatabase('files', $expectedResp); 151 + $this->seeInDatabase('attachments', $expectedResp);
152 152
153 $this->deleteUploads(); 153 $this->deleteUploads();
154 } 154 }
...@@ -164,10 +164,10 @@ class AttachmentTest extends TestCase ...@@ -164,10 +164,10 @@ class AttachmentTest extends TestCase
164 164
165 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist'); 165 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
166 166
167 - $attachmentId = \BookStack\File::first()->id; 167 + $attachmentId = \BookStack\Attachment::first()->id;
168 - $this->call('DELETE', 'files/' . $attachmentId); 168 + $this->call('DELETE', 'attachments/' . $attachmentId);
169 169
170 - $this->dontSeeInDatabase('files', [ 170 + $this->dontSeeInDatabase('attachments', [
171 'name' => $fileName 171 'name' => $fileName
172 ]); 172 ]);
173 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected'); 173 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
...@@ -185,13 +185,13 @@ class AttachmentTest extends TestCase ...@@ -185,13 +185,13 @@ class AttachmentTest extends TestCase
185 $filePath = base_path('storage/' . $this->getUploadPath($fileName)); 185 $filePath = base_path('storage/' . $this->getUploadPath($fileName));
186 186
187 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist'); 187 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
188 - $this->seeInDatabase('files', [ 188 + $this->seeInDatabase('attachments', [
189 'name' => $fileName 189 'name' => $fileName
190 ]); 190 ]);
191 191
192 $this->call('DELETE', $page->getUrl()); 192 $this->call('DELETE', $page->getUrl());
193 193
194 - $this->dontSeeInDatabase('files', [ 194 + $this->dontSeeInDatabase('attachments', [
195 'name' => $fileName 195 'name' => $fileName
196 ]); 196 ]);
197 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected'); 197 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
......