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 if ($this->storageInstance !== null) return $this->storageInstance;
19 $storageType = config('filesystems.default');
21 // Override default location if set to local public to ensure not visible.
22 if ($storageType === 'local') {
23 $storageType = 'local_secure';
26 $this->storageInstance = $this->fileSystem->disk($storageType);
28 return $this->storageInstance;
32 * Get an attachment from storage.
33 * @param Attachment $attachment
35 * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
37 public function getAttachmentFromStorage(Attachment $attachment)
39 return $this->getStorage()->get($attachment->path);
43 * Store a new attachment upon user upload.
44 * @param UploadedFile $uploadedFile
47 * @throws FileUploadException
49 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
51 $attachmentName = $uploadedFile->getClientOriginalName();
52 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
53 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
55 $attachment = Attachment::forceCreate([
56 'name' => $attachmentName,
57 'path' => $attachmentPath,
58 'extension' => $uploadedFile->getClientOriginalExtension(),
59 'uploaded_to' => $page_id,
60 'created_by' => user()->id,
61 'updated_by' => user()->id,
62 'order' => $largestExistingOrder + 1
69 * Store a upload, saving to a file and deleting any existing uploads
70 * attached to that file.
71 * @param UploadedFile $uploadedFile
72 * @param Attachment $attachment
74 * @throws FileUploadException
76 public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
78 if (!$attachment->external) {
79 $this->deleteFileInStorage($attachment);
82 $attachmentName = $uploadedFile->getClientOriginalName();
83 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
85 $attachment->name = $attachmentName;
86 $attachment->path = $attachmentPath;
87 $attachment->external = false;
88 $attachment->extension = $uploadedFile->getClientOriginalExtension();
94 * Save a new File attachment from a given link and name.
100 public function saveNewFromLink($name, $link, $page_id)
102 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
103 return Attachment::forceCreate([
108 'uploaded_to' => $page_id,
109 'created_by' => user()->id,
110 'updated_by' => user()->id,
111 'order' => $largestExistingOrder + 1
116 * Updates the file ordering for a listing of attached files.
117 * @param array $attachmentList
120 public function updateFileOrderWithinPage($attachmentList, $pageId)
122 foreach ($attachmentList as $index => $attachment) {
123 Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
129 * Update the details of a file.
130 * @param Attachment $attachment
131 * @param $requestData
134 public function updateFile(Attachment $attachment, $requestData)
136 $attachment->name = $requestData['name'];
137 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
138 $attachment->path = $requestData['link'];
139 if (!$attachment->external) {
140 $this->deleteFileInStorage($attachment);
141 $attachment->external = true;
149 * Delete a File from the database and storage.
150 * @param Attachment $attachment
153 public function deleteFile(Attachment $attachment)
155 if ($attachment->external) {
156 $attachment->delete();
160 $this->deleteFileInStorage($attachment);
161 $attachment->delete();
165 * Delete a file from the filesystem it sits on.
166 * Cleans any empty leftover folders.
167 * @param Attachment $attachment
169 protected function deleteFileInStorage(Attachment $attachment)
171 $storage = $this->getStorage();
172 $dirPath = dirname($attachment->path);
174 $storage->delete($attachment->path);
175 if (count($storage->allFiles($dirPath)) === 0) {
176 $storage->deleteDirectory($dirPath);
181 * Store a file in storage with the given filename
182 * @param $attachmentName
183 * @param UploadedFile $uploadedFile
185 * @throws FileUploadException
187 protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
189 $attachmentData = file_get_contents($uploadedFile->getRealPath());
191 $storage = $this->getStorage();
192 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
194 $uploadFileName = $attachmentName;
195 while ($storage->exists($basePath . $uploadFileName)) {
196 $uploadFileName = str_random(3) . $uploadFileName;
199 $attachmentPath = $basePath . $uploadFileName;
201 $storage->put($attachmentPath, $attachmentData);
202 } catch (Exception $e) {
203 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
206 return $attachmentPath;