1 <?php namespace BookStack\Uploads;
3 use BookStack\Exceptions\FileUploadException;
4 use BookStack\Uploads\Attachment;
5 use BookStack\Uploads\UploadService;
7 use Symfony\Component\HttpFoundation\File\UploadedFile;
9 class AttachmentService extends UploadService
13 * Get the storage that will be used for storing files.
14 * @return \Illuminate\Contracts\Filesystem\Filesystem
16 protected function getStorage()
18 $storageType = config('filesystems.default');
20 // Override default location if set to local public to ensure not visible.
21 if ($storageType === 'local') {
22 $storageType = 'local_secure';
25 return $this->fileSystem->disk($storageType);
29 * Get an attachment from storage.
30 * @param Attachment $attachment
32 * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
34 public function getAttachmentFromStorage(Attachment $attachment)
36 return $this->getStorage()->get($attachment->path);
40 * Store a new attachment upon user upload.
41 * @param UploadedFile $uploadedFile
44 * @throws FileUploadException
46 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
48 $attachmentName = $uploadedFile->getClientOriginalName();
49 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
50 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
52 $attachment = Attachment::forceCreate([
53 'name' => $attachmentName,
54 'path' => $attachmentPath,
55 'extension' => $uploadedFile->getClientOriginalExtension(),
56 'uploaded_to' => $page_id,
57 'created_by' => user()->id,
58 'updated_by' => user()->id,
59 'order' => $largestExistingOrder + 1
66 * Store a upload, saving to a file and deleting any existing uploads
67 * attached to that file.
68 * @param UploadedFile $uploadedFile
69 * @param Attachment $attachment
71 * @throws FileUploadException
73 public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
75 if (!$attachment->external) {
76 $this->deleteFileInStorage($attachment);
79 $attachmentName = $uploadedFile->getClientOriginalName();
80 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
82 $attachment->name = $attachmentName;
83 $attachment->path = $attachmentPath;
84 $attachment->external = false;
85 $attachment->extension = $uploadedFile->getClientOriginalExtension();
91 * Save a new File attachment from a given link and name.
97 public function saveNewFromLink($name, $link, $page_id)
99 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
100 return Attachment::forceCreate([
105 'uploaded_to' => $page_id,
106 'created_by' => user()->id,
107 'updated_by' => user()->id,
108 'order' => $largestExistingOrder + 1
113 * Updates the file ordering for a listing of attached files.
114 * @param array $attachmentList
117 public function updateFileOrderWithinPage($attachmentList, $pageId)
119 foreach ($attachmentList as $index => $attachment) {
120 Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
126 * Update the details of a file.
127 * @param Attachment $attachment
128 * @param $requestData
131 public function updateFile(Attachment $attachment, $requestData)
133 $attachment->name = $requestData['name'];
134 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
135 $attachment->path = $requestData['link'];
136 if (!$attachment->external) {
137 $this->deleteFileInStorage($attachment);
138 $attachment->external = true;
146 * Delete a File from the database and storage.
147 * @param Attachment $attachment
150 public function deleteFile(Attachment $attachment)
152 if ($attachment->external) {
153 $attachment->delete();
157 $this->deleteFileInStorage($attachment);
158 $attachment->delete();
162 * Delete a file from the filesystem it sits on.
163 * Cleans any empty leftover folders.
164 * @param Attachment $attachment
166 protected function deleteFileInStorage(Attachment $attachment)
168 $storage = $this->getStorage();
169 $dirPath = dirname($attachment->path);
171 $storage->delete($attachment->path);
172 if (count($storage->allFiles($dirPath)) === 0) {
173 $storage->deleteDirectory($dirPath);
178 * Store a file in storage with the given filename
179 * @param $attachmentName
180 * @param UploadedFile $uploadedFile
182 * @throws FileUploadException
184 protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
186 $attachmentData = file_get_contents($uploadedFile->getRealPath());
188 $storage = $this->getStorage();
189 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
191 $uploadFileName = $attachmentName;
192 while ($storage->exists($basePath . $uploadFileName)) {
193 $uploadFileName = str_random(3) . $uploadFileName;
196 $attachmentPath = $basePath . $uploadFileName;
198 $storage->put($attachmentPath, $attachmentData);
199 } catch (Exception $e) {
200 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
203 return $attachmentPath;