From c1f070a1368ed49ce1e5b362fce4a88ad8077d00 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 15 May 2021 17:25:51 +0100 Subject: [PATCH] Handle acl set of images differently for s3 and s3-like Related to #2739 --- app/Uploads/ImageService.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/Uploads/ImageService.php b/app/Uploads/ImageService.php index 7793aaa01..7e8eedada 100644 --- a/app/Uploads/ImageService.php +++ b/app/Uploads/ImageService.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Filesystem\Factory as FileSystem; use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance; use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Contracts\Filesystem\Filesystem as Storage; use Illuminate\Support\Str; use Intervention\Image\Exception\NotSupportedException; use Intervention\Image\ImageManager; @@ -106,7 +107,7 @@ class ImageService } try { - $storage->put($fullPath, $imageData, ['visibility' => 'public']); + $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])); @@ -131,6 +132,24 @@ class ImageService return $image; } + /** + * Save image data for the given path in the public space, if possible, + * for the provided storage mechanism. + */ + protected function saveImageDataInPublicSpace(Storage $storage, string $path, string $data) + { + $storage->put($path, $data); + + // Set visibility if using s3 without an endpoint set. + // Done since this call can break s3-like services but desired for actual + // AWS s3 usage. Attempting to set ACL during above put request requires + // different permissions hence would technically be a breaking change. + $usingS3 = strtolower(config('filesystems.images')) === 's3'; + if ($usingS3 && is_null(config('filesystems.disks.s3.endpoint'))) { + $storage->setVisibility($path, 'public'); + } + } + /** * Clean up an image file name to be both URL and storage safe. */ @@ -190,7 +209,7 @@ class ImageService $thumbData = $this->resizeImage($storage->get($imagePath), $width, $height, $keepRatio); - $storage->put($thumbFilePath, $thumbData, ['visibility' => 'public']); + $this->saveImageDataInPublicSpace($storage, $thumbFilePath, $thumbData); $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 60 * 72);