From 8c7f65abf511840f374c43c15113bacf363a73d6 Mon Sep 17 00:00:00 2001 From: Alex Lingris Date: Mon, 18 May 2026 19:07:47 +0300 Subject: [PATCH] improve fileservice --- composer.json | 2 +- src/File/FileService.php | 66 ++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index a73e29b..87748c6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "ext-imagick": "*", "ext-pdo": "*", "php": "^8.4", -"spatie/image-optimizer": "^1.8", + "spatie/image-optimizer": "^1.8", "staudenmeir/laravel-cte": "^1.0", "intervention/image": "^4.0" }, diff --git a/src/File/FileService.php b/src/File/FileService.php index 026cb6a..0ccbb4a 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -13,6 +13,9 @@ use Lucent\Channel\ChannelService; use Lucent\Id\Id; use Lucent\LucentException; use Lucent\Data\File as DataFile; +use Lucent\ResultType\Error; +use Lucent\ResultType\Result; +use Lucent\ResultType\Success; use Spatie\ImageOptimizer\OptimizerChainFactory; class FileService @@ -70,7 +73,10 @@ class FileService } if ($this->isImage($mimetype)) { - $this->createTemplates($disk, $path); + $result = $this->createTemplates($disk, $path); + if ($result->error()->isDefined()) { + throw new LucentException($result->error()->get()); + } } [$width, $height] = $this->isImage($mimetype) @@ -126,51 +132,65 @@ class FileService return Storage::disk(config("lucent.private_disk")); } - public function createTemplates(Filesystem $disk, string $path): void + /** @return Result */ + public function createTemplates(Filesystem $disk, string $path): Result { $filePath = "lucent/" . $path; if (!$this->loadPublicDisk()->exists($filePath)) { - return; + return Error::create("File not found: {$filePath}"); } $originalImage = $this->imageManager->decode( $this->loadPublicDisk()->get($filePath), ); - foreach ($this->channelService->channel->imageFilters as $filterClass) { - $imageClone = clone $originalImage; - $filterClassInstance = new $filterClass(); - $image = $imageClone->modify($filterClassInstance); + foreach ($this->channelService->channel->imageFilters as $filterClass) { + $filterClassInstance = new $filterClass(); $templateUri = "lucent/templates/" . $filterClassInstance->getPath() . "/" . substr($path, 0, strrpos($path, ".")) . ".webp"; - $disk->put( - $templateUri, - $image->encodeUsingFormat( + $ok = $disk->put( + $templateUri, + (clone $originalImage) + ->modify($filterClassInstance) + ->encodeUsingFormat( + Format::WEBP, + progressive: true, + quality: 80, + ), + ); + + if (!$ok) { + return Error::create( + "Failed to write template: {$templateUri}", + ); + } + } + + $thumbUri = + "lucent/thumbs/" . substr($path, 0, strrpos($path, ".")) . ".webp"; + + $ok = $disk->put( + $thumbUri, + $originalImage + ->cover(300, 300) + ->encodeUsingFormat( Format::WEBP, progressive: true, quality: 80, ), - ); + ); + + if (!$ok) { + return Error::create("Failed to write thumbnail: {$thumbUri}"); } - $thumbDir = - "lucent/thumbs/" . substr($path, 0, strrpos($path, ".")) . ".webp"; - - $image = $originalImage->cover(300, 300); - $disk->put( - $thumbDir, - $image->encodeUsingFormat( - Format::WEBP, - progressive: true, - quality: 80, - ), - ); + return Success::create(null); } /**