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) {
18 return $this->storageInstance;
21 $storageType = config('filesystems.default');
23 // Override default location if set to local public to ensure not visible.
24 if ($storageType === 'local') {
25 $storageType = 'local_secure';
28 $this->storageInstance = $this->fileSystem->disk($storageType);
30 return $this->storageInstance;
34 * Get an attachment from storage.
35 * @param Attachment $attachment
37 * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
39 public function getAttachmentFromStorage(Attachment $attachment)
41 return $this->getStorage()->get($attachment->path);
45 * Store a new attachment upon user upload.
46 * @param UploadedFile $uploadedFile
49 * @throws FileUploadException
51 public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
53 $attachmentName = $uploadedFile->getClientOriginalName();
54 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
55 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
57 $attachment = Attachment::forceCreate([
58 'name' => $attachmentName,
59 'path' => $attachmentPath,
60 'extension' => $uploadedFile->getClientOriginalExtension(),
61 'uploaded_to' => $page_id,
62 'created_by' => user()->id,
63 'updated_by' => user()->id,
64 'order' => $largestExistingOrder + 1
71 * Store a upload, saving to a file and deleting any existing uploads
72 * attached to that file.
73 * @param UploadedFile $uploadedFile
74 * @param Attachment $attachment
76 * @throws FileUploadException
78 public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment)
80 if (!$attachment->external) {
81 $this->deleteFileInStorage($attachment);
84 $attachmentName = $uploadedFile->getClientOriginalName();
85 $attachmentPath = $this->putFileInStorage($attachmentName, $uploadedFile);
87 $attachment->name = $attachmentName;
88 $attachment->path = $attachmentPath;
89 $attachment->external = false;
90 $attachment->extension = $uploadedFile->getClientOriginalExtension();
96 * Save a new File attachment from a given link and name.
102 public function saveNewFromLink($name, $link, $page_id)
104 $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order');
105 return Attachment::forceCreate([
110 'uploaded_to' => $page_id,
111 'created_by' => user()->id,
112 'updated_by' => user()->id,
113 'order' => $largestExistingOrder + 1
118 * Updates the file ordering for a listing of attached files.
119 * @param array $attachmentList
122 public function updateFileOrderWithinPage($attachmentList, $pageId)
124 foreach ($attachmentList as $index => $attachment) {
125 Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]);
131 * Update the details of a file.
132 * @param Attachment $attachment
133 * @param $requestData
136 public function updateFile(Attachment $attachment, $requestData)
138 $attachment->name = $requestData['name'];
139 if (isset($requestData['link']) && trim($requestData['link']) !== '') {
140 $attachment->path = $requestData['link'];
141 if (!$attachment->external) {
142 $this->deleteFileInStorage($attachment);
143 $attachment->external = true;
151 * Delete a File from the database and storage.
152 * @param Attachment $attachment
155 public function deleteFile(Attachment $attachment)
157 if ($attachment->external) {
158 $attachment->delete();
162 $this->deleteFileInStorage($attachment);
163 $attachment->delete();
167 * Delete a file from the filesystem it sits on.
168 * Cleans any empty leftover folders.
169 * @param Attachment $attachment
171 protected function deleteFileInStorage(Attachment $attachment)
173 $storage = $this->getStorage();
174 $dirPath = dirname($attachment->path);
176 $storage->delete($attachment->path);
177 if (count($storage->allFiles($dirPath)) === 0) {
178 $storage->deleteDirectory($dirPath);
183 * Store a file in storage with the given filename
184 * @param $attachmentName
185 * @param UploadedFile $uploadedFile
187 * @throws FileUploadException
189 protected function putFileInStorage($attachmentName, UploadedFile $uploadedFile)
191 $attachmentData = file_get_contents($uploadedFile->getRealPath());
193 $storage = $this->getStorage();
194 $basePath = 'uploads/files/' . Date('Y-m-M') . '/';
196 $uploadFileName = $attachmentName;
197 while ($storage->exists($basePath . $uploadFileName)) {
198 $uploadFileName = str_random(3) . $uploadFileName;
201 $attachmentPath = $basePath . $uploadFileName;
203 $storage->put($attachmentPath, $attachmentData);
204 } catch (Exception $e) {
205 throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath]));
208 return $attachmentPath;