1 <?php namespace BookStack\Uploads;
3 use BookStack\Exceptions\FileUploadException;
5 use Illuminate\Support\Str;
6 use Symfony\Component\HttpFoundation\File\UploadedFile;
8 class AttachmentService extends UploadService
12 * Get the storage that will be used for storing files.
13 * @return \Illuminate\Contracts\Filesystem\Filesystem
15 protected function getStorage()
17 $storageType = config('filesystems.attachments');
19 // Override default location if set to local public to ensure not visible.
20 if ($storageType === 'local') {
21 $storageType = 'local_secure';
24 return $this->fileSystem->disk($storageType);
28 * Get an attachment from storage.
29 * @param Attachment $attachment
31 * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
33 public function getAttachmentFromStorage(Attachment $attachment)
35 return $this->getStorage()->get($attachment->path);
39 * Store a new attachment upon user upload.
40 * @param UploadedFile $uploadedFile
43 * @throws FileUploadException
45 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
47 $attachmentName = $uploadedFile->getClientOriginalName();
48 $attachmentPath = $this->putFileInStorage($uploadedFile);
49 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
51 $attachment = Attachment::forceCreate([
52 'name' => $attachmentName,
53 'path' => $attachmentPath,
54 'extension' => $uploadedFile->getClientOriginalExtension(),
55 'uploaded_to' => $page_id,
56 'created_by' => user()->id,
57 'updated_by' => user()->id,
58 'order' => $largestExistingOrder + 1
65 * Store a upload, saving to a file and deleting any existing uploads
66 * attached to that file.
67 * @param UploadedFile $uploadedFile
68 * @param Attachment $attachment
70 * @throws FileUploadException
72 public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
74 if (!$attachment->external) {
75 $this->deleteFileInStorage($attachment);
78 $attachmentName = $uploadedFile->getClientOriginalName();
79 $attachmentPath = $this->putFileInStorage($uploadedFile);
81 $attachment->name = $attachmentName;
82 $attachment->path = $attachmentPath;
83 $attachment->external = false;
84 $attachment->extension = $uploadedFile->getClientOriginalExtension();
90 * Save a new File attachment from a given link and name.
92 public function saveNewFromLink(string $name, string $link, int $page_id): Attachment
94 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
95 return Attachment::forceCreate([
100 'uploaded_to' => $page_id,
101 'created_by' => user()->id,
102 'updated_by' => user()->id,
103 'order' => $largestExistingOrder + 1
108 * Updates the ordering for a listing of attached files.
110 public function updateFileOrderWithinPage(array $attachmentOrder, string $pageId)
112 foreach ($attachmentOrder as $index => $attachmentId) {
113 Attachment::query()->where('uploaded_to', '=', $pageId)
114 ->where('id', '=', $attachmentId)
115 ->update(['order' => $index]);
121 * Update the details of a file.
123 public function updateFile(Attachment $attachment, array $requestData): Attachment
125 $attachment->name = $requestData['name'];
127 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
128 $attachment->path = $requestData['link'];
129 if (!$attachment->external) {
130 $this->deleteFileInStorage($attachment);
131 $attachment->external = true;
140 * Delete a File from the database and storage.
141 * @param Attachment $attachment
144 public function deleteFile(Attachment $attachment)
146 if ($attachment->external) {
147 $attachment->delete();
151 $this->deleteFileInStorage($attachment);
152 $attachment->delete();
156 * Delete a file from the filesystem it sits on.
157 * Cleans any empty leftover folders.
158 * @param Attachment $attachment
160 protected function deleteFileInStorage(Attachment $attachment)
162 $storage = $this->getStorage();
163 $dirPath = dirname($attachment->path);
165 $storage->delete($attachment->path);
166 if (count($storage->allFiles($dirPath)) === 0) {
167 $storage->deleteDirectory($dirPath);
172 * Store a file in storage with the given filename
173 * @param UploadedFile $uploadedFile
175 * @throws FileUploadException
177 protected function putFileInStorage(UploadedFile $uploadedFile)
179 $attachmentData = file_get_contents($uploadedFile->getRealPath());
181 $storage = $this->getStorage();
182 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
184 $uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension();
185 while ($storage->exists($basePath . $uploadFileName)) {
186 $uploadFileName = Str::random(3) . $uploadFileName;
189 $attachmentPath = $basePath . $uploadFileName;
191 $storage->put($attachmentPath, $attachmentData);
192 } catch (Exception $e) {
193 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
196 return $attachmentPath;