From 6fe9928b4d13fb1a64092a1a6a6e62f49d1a0b65 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Fri, 3 May 2024 16:45:42 +0200 Subject: [PATCH] Delete invalid attachments all together --- app/Classes/Traits/Repairs/RepairsImages.php | 37 ++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/app/Classes/Traits/Repairs/RepairsImages.php b/app/Classes/Traits/Repairs/RepairsImages.php index feffaf2..93f3289 100644 --- a/app/Classes/Traits/Repairs/RepairsImages.php +++ b/app/Classes/Traits/Repairs/RepairsImages.php @@ -3,8 +3,10 @@ namespace App\Classes\Traits\Repairs; use App\Classes\AttachmentsManager; +use App\Exceptions\AttachmentNotFound; use Carbon\Carbon; use Illuminate\Support\Str; +use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Laravel\Facades\Image; use League\Flysystem\StorageAttributes; @@ -148,8 +150,15 @@ private function syncImageVariants(string $ref) */ private function syncImageVariant(string $ref, string $filter) { - $originalData = $this->getAttachmentData($ref); - $variantData = $this->getVariantData($ref, $filter); + try { + $originalData = $this->getAttachmentData($ref); + } catch (AttachmentNotFound $ex) { + $this->deleteAttachment($ref); + + return; + } + + $variantData = $this->getVariantData($ref, $filter); $variantFilepath = $this->getVariantFullPath($ref, $filter); @@ -171,10 +180,26 @@ private function syncImageVariant(string $ref, string $filter) private function createImageVariant(string $ref, string $filter) { $filterClass = config(sprintf('imagefilters.%s', $filter)); - $original = $this->getAttachmentFullPath($ref); - $target = $this->getVariantFullPath($ref, $filter); - $contents = $this->disk->get($original); - $image = Image::read($contents); + + try { + $original = $this->getAttachmentFullPath($ref); + } catch (AttachmentNotFound $ex) { + $this->deleteAttachment($ref); + + return; + } + + $target = $this->getVariantFullPath($ref, $filter); + $contents = $this->disk->get($original); + + try { + $image = Image::read($contents); + } catch (DecoderException $ex) { + $this->deleteAttachment($ref); + + return; + } + $variantData = $this->getVariantData($ref, $filter); $image->modify(new $filterClass());