X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/32f6ea946f00d25b3e70166d4e1bd3ef27d64a33..refs/pull/5689/head:/app/Uploads/AttachmentService.php diff --git a/app/Uploads/AttachmentService.php b/app/Uploads/AttachmentService.php index d530d8fbe..dabd53729 100644 --- a/app/Uploads/AttachmentService.php +++ b/app/Uploads/AttachmentService.php @@ -4,73 +4,31 @@ namespace BookStack\Uploads; use BookStack\Exceptions\FileUploadException; use Exception; -use Illuminate\Contracts\Filesystem\Factory as FileSystem; -use Illuminate\Contracts\Filesystem\FileNotFoundException; -use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Str; -use League\Flysystem\Util; use Symfony\Component\HttpFoundation\File\UploadedFile; class AttachmentService { - protected $fileSystem; - - /** - * AttachmentService constructor. - */ - public function __construct(FileSystem $fileSystem) - { - $this->fileSystem = $fileSystem; - } - - /** - * Get the storage that will be used for storing files. - */ - protected function getStorage(): FileSystemInstance - { - return $this->fileSystem->disk($this->getStorageDiskName()); + public function __construct( + protected FileStorage $storage, + ) { } /** - * Get the name of the storage disk to use. - */ - protected function getStorageDiskName(): string - { - $storageType = config('filesystems.attachments'); - - // Change to our secure-attachment disk if any of the local options - // are used to prevent escaping that location. - if ($storageType === 'local' || $storageType === 'local_secure') { - $storageType = 'local_secure_attachments'; - } - - return $storageType; - } - - /** - * Change the originally provided path to fit any disk-specific requirements. - * This also ensures the path is kept to the expected root folders. + * Stream an attachment from storage. + * + * @return resource|null */ - protected function adjustPathForStorageDisk(string $path): string + public function streamAttachmentFromStorage(Attachment $attachment) { - $path = Util::normalizePath(str_replace('uploads/files/', '', $path)); - - if ($this->getStorageDiskName() === 'local_secure_attachments') { - return $path; - } - - return 'uploads/files/' . $path; + return $this->storage->getReadStream($attachment->path); } /** - * Get an attachment from storage. - * - * @throws FileNotFoundException + * Read the file size of an attachment from storage, in bytes. */ - public function getAttachmentFromStorage(Attachment $attachment): string + public function getAttachmentFileSize(Attachment $attachment): int { - return $this->getStorage()->get($this->adjustPathForStorageDisk($attachment->path)); + return $this->storage->getSize($attachment->path); } /** @@ -158,20 +116,23 @@ class AttachmentService */ public function updateFile(Attachment $attachment, array $requestData): Attachment { - $attachment->name = $requestData['name']; - $link = trim($requestData['link'] ?? ''); + if (isset($requestData['name'])) { + $attachment->name = $requestData['name']; + } + $link = trim($requestData['link'] ?? ''); if (!empty($link)) { - $attachment->path = $requestData['link']; if (!$attachment->external) { $this->deleteFileInStorage($attachment); $attachment->external = true; + $attachment->extension = ''; } + $attachment->path = $link; } $attachment->save(); - return $attachment; + return $attachment->refresh(); } /** @@ -192,15 +153,9 @@ class AttachmentService * Delete a file from the filesystem it sits on. * Cleans any empty leftover folders. */ - protected function deleteFileInStorage(Attachment $attachment) + public function deleteFileInStorage(Attachment $attachment): void { - $storage = $this->getStorage(); - $dirPath = $this->adjustPathForStorageDisk(dirname($attachment->path)); - - $storage->delete($this->adjustPathForStorageDisk($attachment->path)); - if (count($storage->allFiles($dirPath)) === 0) { - $storage->deleteDirectory($dirPath); - } + $this->storage->delete($attachment->path); } /** @@ -210,26 +165,21 @@ class AttachmentService */ protected function putFileInStorage(UploadedFile $uploadedFile): string { - $attachmentData = file_get_contents($uploadedFile->getRealPath()); - - $storage = $this->getStorage(); $basePath = 'uploads/files/' . date('Y-m-M') . '/'; - $uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension(); - while ($storage->exists($this->adjustPathForStorageDisk($basePath . $uploadFileName))) { - $uploadFileName = Str::random(3) . $uploadFileName; - } - - $attachmentPath = $basePath . $uploadFileName; - - try { - $storage->put($this->adjustPathForStorageDisk($attachmentPath), $attachmentData); - } catch (Exception $e) { - Log::error('Error when attempting file upload:' . $e->getMessage()); - - throw new FileUploadException(trans('errors.path_not_writable', ['filePath' => $attachmentPath])); - } + return $this->storage->uploadFile( + $uploadedFile, + $basePath, + $uploadedFile->getClientOriginalExtension(), + '' + ); + } - return $attachmentPath; + /** + * Get the file validation rules for attachments. + */ + public static function getFileValidationRules(): array + { + return ['file', 'max:' . (config('app.upload_limit') * 1000)]; } }