X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/85db812feaae5f36ea6214931cec4adb67a9cb39..refs/pull/2902/head:/app/Uploads/ImageService.php diff --git a/app/Uploads/ImageService.php b/app/Uploads/ImageService.php index 293049f4f..51ddf9bdc 100644 --- a/app/Uploads/ImageService.php +++ b/app/Uploads/ImageService.php @@ -1,4 +1,6 @@ -saveNew($name, $data, $type, $uploadedTo); } /** * Save a new image into storage. + * * @throws ImageUploadException */ public function saveNew(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image @@ -95,7 +102,7 @@ class ImageService $secureUploads = setting('app-secure-images'); $fileName = $this->cleanImageFileName($imageName); - $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m') . '/'; + $imagePath = '/uploads/images/' . $type . '/' . date('Y-m') . '/'; while ($storage->exists($imagePath . $fileName)) { $fileName = Str::random(3) . $fileName; @@ -110,15 +117,16 @@ class ImageService $this->saveImageDataInPublicSpace($storage, $fullPath, $imageData); } catch (Exception $e) { \Log::error('Error when attempting image upload:' . $e->getMessage()); + throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $fullPath])); } $imageDetails = [ - 'name' => $imageName, - 'path' => $fullPath, - 'url' => $this->getPublicUrl($fullPath), - 'type' => $type, - 'uploaded_to' => $uploadedTo + 'name' => $imageName, + 'path' => $fullPath, + 'url' => $this->getPublicUrl($fullPath), + 'type' => $type, + 'uploaded_to' => $uploadedTo, ]; if (user()->id !== 0) { @@ -129,6 +137,7 @@ class ImageService $image = $this->image->newInstance(); $image->forceFill($imageDetails)->save(); + return $image; } @@ -181,13 +190,16 @@ class ImageService * Get the thumbnail for an image. * If $keepRatio is true only the width will be used. * Checks the cache then storage to avoid creating / accessing the filesystem on every check. + * * @param Image $image - * @param int $width - * @param int $height - * @param bool $keepRatio - * @return string + * @param int $width + * @param int $height + * @param bool $keepRatio + * * @throws Exception * @throws ImageUploadException + * + * @return string */ public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false) { @@ -213,18 +225,20 @@ class ImageService $this->saveImageDataInPublicSpace($storage, $thumbFilePath, $thumbData); $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 60 * 72); - return $this->getPublicUrl($thumbFilePath); } /** * Resize image data. + * * @param string $imageData - * @param int $width - * @param int $height - * @param bool $keepRatio - * @return string + * @param int $width + * @param int $height + * @param bool $keepRatio + * * @throws ImageUploadException + * + * @return string */ protected function resizeImage(string $imageData, $width = 220, $height = null, bool $keepRatio = true) { @@ -234,6 +248,7 @@ class ImageService if ($e instanceof ErrorException || $e instanceof NotSupportedException) { throw new ImageUploadException(trans('errors.cannot_create_thumbs')); } + throw $e; } @@ -246,7 +261,7 @@ class ImageService $thumb->fit($width, $height); } - $thumbData = (string)$thumb->encode(); + $thumbData = (string) $thumb->encode(); // Use original image data if we're keeping the ratio // and the resizing does not save any space. @@ -259,17 +274,20 @@ class ImageService /** * Get the raw data content from an image. + * * @throws FileNotFoundException */ public function getImageData(Image $image): string { $imagePath = $image->path; $storage = $this->getStorage(); + return $storage->get($imagePath); } /** * Destroy an image along with its revisions, thumbnails and remaining folders. + * * @throws Exception */ public function destroy(Image $image) @@ -314,7 +332,8 @@ class ImageService { $files = $storage->files($path); $folders = $storage->directories($path); - return (count($files) === 0 && count($folders) === 0); + + return count($files) === 0 && count($folders) === 0; } /** @@ -350,6 +369,7 @@ class ImageService } } }); + return $deletedPaths; } @@ -358,6 +378,7 @@ class ImageService * Attempts to convert the URL to a system storage url then * fetch the data from the disk or storage location. * Returns null if the image data cannot be fetched from storage. + * * @throws FileNotFoundException */ public function imageUriToBase64(string $uri): ?string @@ -400,6 +421,7 @@ class ImageService if (strpos(strtolower($url), 'uploads/images') === 0) { return trim($url, '/'); } + return null; } @@ -443,6 +465,7 @@ class ImageService } $basePath = ($this->storageUrl == false) ? url('/') : $this->storageUrl; + return rtrim($basePath, '/') . $filePath; } }