1 <?php namespace BookStack\Uploads;
3 use BookStack\Exceptions\FileUploadException;
5 use Symfony\Component\HttpFoundation\File\UploadedFile;
7 class AttachmentService extends UploadService
11 * Get the storage that will be used for storing files.
12 * @return \Illuminate\Contracts\Filesystem\Filesystem
14 protected function getStorage()
16 $storageType = config('filesystems.attachments');
18 // Override default location if set to local public to ensure not visible.
19 if ($storageType === 'local') {
20 $storageType = 'local_secure';
23 return $this->fileSystem->disk($storageType);
27 * Get an attachment from storage.
28 * @param Attachment $attachment
30 * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
32 public function getAttachmentFromStorage(Attachment $attachment)
34 return $this->getStorage()->get($attachment->path);
38 * Store a new attachment upon user upload.
39 * @param UploadedFile $uploadedFile
42 * @throws FileUploadException
44 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
46 $attachmentName = $uploadedFile->getClientOriginalName();
47 $attachmentPath = $this->putFileInStorage($uploadedFile);
48 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
50 $attachment = Attachment::forceCreate([
51 'name' => $attachmentName,
52 'path' => $attachmentPath,
53 'extension' => $uploadedFile->getClientOriginalExtension(),
54 'uploaded_to' => $page_id,
55 'created_by' => user()->id,
56 'updated_by' => user()->id,
57 'order' => $largestExistingOrder + 1
64 * Store a upload, saving to a file and deleting any existing uploads
65 * attached to that file.
66 * @param UploadedFile $uploadedFile
67 * @param Attachment $attachment
69 * @throws FileUploadException
71 public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
73 if (!$attachment->external) {
74 $this->deleteFileInStorage($attachment);
77 $attachmentName = $uploadedFile->getClientOriginalName();
78 $attachmentPath = $this->putFileInStorage($uploadedFile);
80 $attachment->name = $attachmentName;
81 $attachment->path = $attachmentPath;
82 $attachment->external = false;
83 $attachment->extension = $uploadedFile->getClientOriginalExtension();
89 * Save a new File attachment from a given link and name.
95 public function saveNewFromLink($name, $link, $page_id)
97 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
98 return Attachment::forceCreate([
103 'uploaded_to' => $page_id,
104 'created_by' => user()->id,
105 'updated_by' => user()->id,
106 'order' => $largestExistingOrder + 1
111 * Updates the file ordering for a listing of attached files.
112 * @param array $attachmentList
115 public function updateFileOrderWithinPage($attachmentList, $pageId)
117 foreach ($attachmentList as $index => $attachment) {
118 Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
124 * Update the details of a file.
125 * @param Attachment $attachment
126 * @param $requestData
129 public function updateFile(Attachment $attachment, $requestData)
131 $attachment->name = $requestData['name'];
132 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
133 $attachment->path = $requestData['link'];
134 if (!$attachment->external) {
135 $this->deleteFileInStorage($attachment);
136 $attachment->external = true;
144 * Delete a File from the database and storage.
145 * @param Attachment $attachment
148 public function deleteFile(Attachment $attachment)
150 if ($attachment->external) {
151 $attachment->delete();
155 $this->deleteFileInStorage($attachment);
156 $attachment->delete();
160 * Delete a file from the filesystem it sits on.
161 * Cleans any empty leftover folders.
162 * @param Attachment $attachment
164 protected function deleteFileInStorage(Attachment $attachment)
166 $storage = $this->getStorage();
167 $dirPath = dirname($attachment->path);
169 $storage->delete($attachment->path);
170 if (count($storage->allFiles($dirPath)) === 0) {
171 $storage->deleteDirectory($dirPath);
176 * Store a file in storage with the given filename
177 * @param UploadedFile $uploadedFile
179 * @throws FileUploadException
181 protected function putFileInStorage(UploadedFile $uploadedFile)
183 $attachmentData = file_get_contents($uploadedFile->getRealPath());
185 $storage = $this->getStorage();
186 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
188 $uploadFileName = str_random(16) . '.' . $uploadedFile->getClientOriginalExtension();
189 while ($storage->exists($basePath . $uploadFileName)) {
190 $uploadFileName = str_random(3) . $uploadFileName;
193 $attachmentPath = $basePath . $uploadFileName;
195 $storage->put($attachmentPath, $attachmentData);
196 } catch (Exception $e) {
197 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
200 return $attachmentPath;