improve fileservice
This commit is contained in:
+1
-1
@@ -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"
|
||||
},
|
||||
|
||||
+35
-15
@@ -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}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$thumbDir =
|
||||
$thumbUri =
|
||||
"lucent/thumbs/" . substr($path, 0, strrpos($path, ".")) . ".webp";
|
||||
|
||||
$image = $originalImage->cover(300, 300);
|
||||
$disk->put(
|
||||
$thumbDir,
|
||||
$image->encodeUsingFormat(
|
||||
$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}");
|
||||
}
|
||||
|
||||
return Success::create(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user