1 <?php namespace BookStack\Services;
3 use BookStack\Exceptions\FileUploadException;
4 use BookStack\Attachment;
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.default');
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($attachmentName, $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($attachmentName, $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.
96 public function saveNewFromLink($name, $link, $page_id)
98 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
99 return Attachment::forceCreate([
104 'uploaded_to' => $page_id,
105 'created_by' => user()->id,
106 'updated_by' => user()->id,
107 'order' => $largestExistingOrder + 1
112 * Updates the file ordering for a listing of attached files.
113 * @param array $attachmentList
116 public function updateFileOrderWithinPage($attachmentList, $pageId)
118 foreach ($attachmentList as $index => $attachment) {
119 Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
125 * Update the details of a file.
126 * @param Attachment $attachment
127 * @param $requestData
130 public function updateFile(Attachment $attachment, $requestData)
132 $attachment->name = $requestData['name'];
133 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
134 $attachment->path = $requestData['link'];
135 if (!$attachment->external) {
136 $this->deleteFileInStorage($attachment);
137 $attachment->external = true;
145 * Delete a File from the database and storage.
146 * @param Attachment $attachment
149 public function deleteFile(Attachment $attachment)
151 if ($attachment->external) {
152 $attachment->delete();
156 $this->deleteFileInStorage($attachment);
157 $attachment->delete();
161 * Delete a file from the filesystem it sits on.
162 * Cleans any empty leftover folders.
163 * @param Attachment $attachment
165 protected function deleteFileInStorage(Attachment $attachment)
167 $storage = $this->getStorage();
168 $dirPath = dirname($attachment->path);
170 $storage->delete($attachment->path);
171 if (count($storage->allFiles($dirPath)) === 0) {
172 $storage->deleteDirectory($dirPath);
177 * Store a file in storage with the given filename
178 * @param $attachmentName
179 * @param UploadedFile $uploadedFile
181 * @throws FileUploadException
183 protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
185 $attachmentData = file_get_contents($uploadedFile->getRealPath());
187 $storage = $this->getStorage();
188 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
190 $uploadFileName = $attachmentName;
191 while ($storage->exists($basePath . $uploadFileName)) {
192 $uploadFileName = str_random(3) . $uploadFileName;
195 $attachmentPath = $basePath . $uploadFileName;
197 $storage->put($attachmentPath, $attachmentData);
198 } catch (Exception $e) {
199 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
202 return $attachmentPath;