From 81398f65a738b38f0e372aa772bb148660d6744f Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Sun, 5 May 2024 15:12:12 +0200 Subject: [PATCH] Use checksums provided by attachments themselves --- app/Classes/Traits/Repairs/RepairsImages.php | 23 ++++++++++++++++++-- app/Classes/Traits/Repairs/RepairsSounds.php | 13 +++++++++++ app/Classes/Traits/Repairs/RepairsVideos.php | 13 +++++++++++ app/View/Components/BaseMediaComponent.php | 20 +++++++++-------- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/Classes/Traits/Repairs/RepairsImages.php b/app/Classes/Traits/Repairs/RepairsImages.php index 93f3289..ff0e2dc 100644 --- a/app/Classes/Traits/Repairs/RepairsImages.php +++ b/app/Classes/Traits/Repairs/RepairsImages.php @@ -5,6 +5,7 @@ use App\Classes\AttachmentsManager; use App\Exceptions\AttachmentNotFound; use Carbon\Carbon; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Laravel\Facades\Image; @@ -115,6 +116,18 @@ private function repairKnownImage(string $ref, array $data) } } + $checksum = $this->disk->checksum($expectedFullPath); + + if (!empty($data['checksum']) && $data['checksum'] !== $checksum) { + Log::warning('File checksum has changed', [ + 'file' => $expectedFullPath, + 'oldChecsum' => $data['checksum'], + 'newChecksum' => $checksum, + ]); + } + + $data['checksum'] = $checksum; + $this->manager->set(sprintf('files.%s', $ref), $data); $this->syncImageVariants($ref); @@ -158,10 +171,15 @@ private function syncImageVariant(string $ref, string $filter) return; } - $variantData = $this->getVariantData($ref, $filter); - + $variantData = $this->getVariantData($ref, $filter); $variantFilepath = $this->getVariantFullPath($ref, $filter); + if (empty($variantData['checksum'])) { + $variantData['checksum'] = $this->disk->checksum($variantFilepath); + + $this->manager->set(sprintf('variants.%s.%s', $ref, $filter), $variantData); + } + if (!$this->disk->exists($variantFilepath)) { $this->createImageVariant($ref, $filter); } else { @@ -214,6 +232,7 @@ private function createImageVariant(string $ref, string $filter) $variantData['filename'] = $this->getVariantRelativePath($ref, $filter); $variantData['last_modified'] = now(); + $variantData['checksum'] = $this->disk->checksum($target); $this->manager->set(sprintf('variants.%s.%s', $ref, $filter), $variantData); } diff --git a/app/Classes/Traits/Repairs/RepairsSounds.php b/app/Classes/Traits/Repairs/RepairsSounds.php index b2f37c9..1d0b9ab 100644 --- a/app/Classes/Traits/Repairs/RepairsSounds.php +++ b/app/Classes/Traits/Repairs/RepairsSounds.php @@ -3,6 +3,7 @@ namespace App\Classes\Traits\Repairs; use Carbon\Carbon; +use Illuminate\Support\Facades\Log; /** * Trait for AttachmentsManager @@ -76,6 +77,18 @@ private function repairKnownSound(string $ref, array $data) } } + $checksum = $this->disk->checksum($expectedFullPath); + + if (!empty($data['checksum']) && $data['checksum'] !== $checksum) { + Log::warning('File checksum has changed', [ + 'file' => $expectedFullPath, + 'oldChecsum' => $data['checksum'], + 'newChecksum' => $checksum, + ]); + } + + $data['checksum'] = $checksum; + $this->manager->set(sprintf('files.%s', $ref), $data); } } diff --git a/app/Classes/Traits/Repairs/RepairsVideos.php b/app/Classes/Traits/Repairs/RepairsVideos.php index c8bf884..4d4f1f1 100644 --- a/app/Classes/Traits/Repairs/RepairsVideos.php +++ b/app/Classes/Traits/Repairs/RepairsVideos.php @@ -3,6 +3,7 @@ namespace App\Classes\Traits\Repairs; use Carbon\Carbon; +use Illuminate\Support\Facades\Log; /** * Trait for AttachmentsManager @@ -76,6 +77,18 @@ private function repairKnownVideo(string $ref, array $data) } } + $checksum = $this->disk->checksum($expectedFullPath); + + if (!empty($data['checksum']) && $data['checksum'] !== $checksum) { + Log::warning('File checksum has changed', [ + 'file' => $expectedFullPath, + 'oldChecsum' => $data['checksum'], + 'newChecksum' => $checksum, + ]); + } + + $data['checksum'] = $checksum; + $this->manager->set(sprintf('files.%s', $ref), $data); } } diff --git a/app/View/Components/BaseMediaComponent.php b/app/View/Components/BaseMediaComponent.php index e061c11..d1b2055 100644 --- a/app/View/Components/BaseMediaComponent.php +++ b/app/View/Components/BaseMediaComponent.php @@ -24,8 +24,8 @@ public function __construct(protected array $data, protected ?array $variant = [ */ public function render(): View|Closure|string { - $originalUrl = $this->copyFile($this->data['filename']); - $variantUrl = $this->variant ? $this->copyFile($this->variant['filename']) : null; + $originalUrl = $this->copyFile($this->data); + $variantUrl = $this->variant ? $this->copyFile($this->variant) : null; return view($this->view, [ 'originalUrl' => $originalUrl, @@ -39,13 +39,15 @@ public function render(): View|Closure|string /** * Copy a file to public disk and return relative url to use */ - protected function copyFile(string $path) + protected function copyFile(array $data) { - $content = Storage::disk(env('CONTENT_DISK'))->get($path); - $md5 = md5($content); - $targetPath = $this->buildTargetFilePath($path, $md5); + $path = $data['filename']; + $checksum = $data['checksum']; + $targetPath = $this->buildTargetFilePath($path, $checksum); if (!Storage::disk('public')->exists($targetPath)) { + $content = Storage::disk(env('CONTENT_DISK'))->get($path); + Storage::disk('public')->put($targetPath, $content); } @@ -55,11 +57,11 @@ protected function copyFile(string $path) /** * Return a path for the target file */ - protected function buildTargetFilePath(string $originalPath, string $md5) + protected function buildTargetFilePath(string $originalPath, string $checksum) { $extension = pathinfo($originalPath, PATHINFO_EXTENSION); - $pathParts = str_split($md5, 4); - $pathParts[] = sprintf('%s.%s', $md5, $extension); + $pathParts = str_split($checksum, 4); + $pathParts[] = sprintf('%s.%s', $checksum, $extension); $targetPath = implode('/', $pathParts); return $targetPath;