X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/193e2ffebe920234438faff812f731de7b36ca9f..refs/pull/2902/head:/app/Uploads/AttachmentService.php diff --git a/app/Uploads/AttachmentService.php b/app/Uploads/AttachmentService.php index e613642c4..298d53a04 100644 --- a/app/Uploads/AttachmentService.php +++ b/app/Uploads/AttachmentService.php @@ -1,19 +1,34 @@ -fileSystem = $fileSystem; + } /** * Get the storage that will be used for storing files. - * @return \Illuminate\Contracts\Filesystem\Filesystem */ - protected function getStorage() + protected function getStorage(): FileSystemInstance { - $storageType = config('filesystems.default'); + $storageType = config('filesystems.attachments'); // Override default location if set to local public to ensure not visible. if ($storageType === 'local') { @@ -25,21 +40,23 @@ class AttachmentService extends UploadService /** * Get an attachment from storage. - * @param Attachment $attachment - * @return string - * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + * + * @throws FileNotFoundException */ - public function getAttachmentFromStorage(Attachment $attachment) + public function getAttachmentFromStorage(Attachment $attachment): string { return $this->getStorage()->get($attachment->path); } /** * Store a new attachment upon user upload. + * * @param UploadedFile $uploadedFile - * @param int $page_id - * @return Attachment + * @param int $page_id + * * @throws FileUploadException + * + * @return Attachment */ public function saveNewUpload(UploadedFile $uploadedFile, $page_id) { @@ -48,13 +65,13 @@ class AttachmentService extends UploadService $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order'); $attachment = Attachment::forceCreate([ - 'name' => $attachmentName, - 'path' => $attachmentPath, - 'extension' => $uploadedFile->getClientOriginalExtension(), + 'name' => $attachmentName, + 'path' => $attachmentPath, + 'extension' => $uploadedFile->getClientOriginalExtension(), 'uploaded_to' => $page_id, - 'created_by' => user()->id, - 'updated_by' => user()->id, - 'order' => $largestExistingOrder + 1 + 'created_by' => user()->id, + 'updated_by' => user()->id, + 'order' => $largestExistingOrder + 1, ]); return $attachment; @@ -63,10 +80,13 @@ class AttachmentService extends UploadService /** * Store a upload, saving to a file and deleting any existing uploads * attached to that file. + * * @param UploadedFile $uploadedFile - * @param Attachment $attachment - * @return Attachment + * @param Attachment $attachment + * * @throws FileUploadException + * + * @return Attachment */ public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attachment) { @@ -82,53 +102,48 @@ class AttachmentService extends UploadService $attachment->external = false; $attachment->extension = $uploadedFile->getClientOriginalExtension(); $attachment->save(); + return $attachment; } /** * Save a new File attachment from a given link and name. - * @param string $name - * @param string $link - * @param int $page_id - * @return Attachment */ - public function saveNewFromLink($name, $link, $page_id) + public function saveNewFromLink(string $name, string $link, int $page_id): Attachment { $largestExistingOrder = Attachment::where('uploaded_to', '=', $page_id)->max('order'); + return Attachment::forceCreate([ - 'name' => $name, - 'path' => $link, - 'external' => true, - 'extension' => '', + 'name' => $name, + 'path' => $link, + 'external' => true, + 'extension' => '', 'uploaded_to' => $page_id, - 'created_by' => user()->id, - 'updated_by' => user()->id, - 'order' => $largestExistingOrder + 1 + 'created_by' => user()->id, + 'updated_by' => user()->id, + 'order' => $largestExistingOrder + 1, ]); } /** - * Updates the file ordering for a listing of attached files. - * @param array $attachmentList - * @param $pageId + * Updates the ordering for a listing of attached files. */ - public function updateFileOrderWithinPage($attachmentList, $pageId) + public function updateFileOrderWithinPage(array $attachmentOrder, string $pageId) { - foreach ($attachmentList as $index => $attachment) { - Attachment::where('uploaded_to', '=', $pageId)->where('id', '=', $attachment['id'])->update(['order' => $index]); + foreach ($attachmentOrder as $index => $attachmentId) { + Attachment::query()->where('uploaded_to', '=', $pageId) + ->where('id', '=', $attachmentId) + ->update(['order' => $index]); } } - /** * Update the details of a file. - * @param Attachment $attachment - * @param $requestData - * @return Attachment */ - public function updateFile(Attachment $attachment, $requestData) + public function updateFile(Attachment $attachment, array $requestData): Attachment { $attachment->name = $requestData['name']; + if (isset($requestData['link']) && trim($requestData['link']) !== '') { $attachment->path = $requestData['link']; if (!$attachment->external) { @@ -136,22 +151,27 @@ class AttachmentService extends UploadService $attachment->external = true; } } + $attachment->save(); + return $attachment; } /** * Delete a File from the database and storage. + * * @param Attachment $attachment + * * @throws Exception */ public function deleteFile(Attachment $attachment) { if ($attachment->external) { $attachment->delete(); + return; } - + $this->deleteFileInStorage($attachment); $attachment->delete(); } @@ -159,6 +179,7 @@ class AttachmentService extends UploadService /** * Delete a file from the filesystem it sits on. * Cleans any empty leftover folders. + * * @param Attachment $attachment */ protected function deleteFileInStorage(Attachment $attachment) @@ -173,27 +194,33 @@ class AttachmentService extends UploadService } /** - * Store a file in storage with the given filename + * Store a file in storage with the given filename. + * * @param UploadedFile $uploadedFile - * @return string + * * @throws FileUploadException + * + * @return string */ protected function putFileInStorage(UploadedFile $uploadedFile) { $attachmentData = file_get_contents($uploadedFile->getRealPath()); $storage = $this->getStorage(); - $basePath = 'uploads/files/' . Date('Y-m-M') . '/'; + $basePath = 'uploads/files/' . date('Y-m-M') . '/'; - $uploadFileName = str_random(16) . '.' . $uploadedFile->getClientOriginalExtension(); + $uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension(); while ($storage->exists($basePath . $uploadFileName)) { - $uploadFileName = str_random(3) . $uploadFileName; + $uploadFileName = Str::random(3) . $uploadFileName; } $attachmentPath = $basePath . $uploadFileName; + try { $storage->put($attachmentPath, $attachmentData); } catch (Exception $e) { + Log::error('Error when attempting file upload:' . $e->getMessage()); + throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath])); }