improve fileservice

This commit is contained in:
2026-05-18 19:07:47 +03:00
parent 507d643aee
commit 8c7f65abf5
2 changed files with 44 additions and 24 deletions
+1 -1
View File
@@ -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"
},
+43 -23
View File
@@ -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<null, string> */
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);
}
/**