improve fileservice
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
"ext-imagick": "*",
|
"ext-imagick": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"php": "^8.4",
|
"php": "^8.4",
|
||||||
"spatie/image-optimizer": "^1.8",
|
"spatie/image-optimizer": "^1.8",
|
||||||
"staudenmeir/laravel-cte": "^1.0",
|
"staudenmeir/laravel-cte": "^1.0",
|
||||||
"intervention/image": "^4.0"
|
"intervention/image": "^4.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+35
-15
@@ -13,6 +13,9 @@ use Lucent\Channel\ChannelService;
|
|||||||
use Lucent\Id\Id;
|
use Lucent\Id\Id;
|
||||||
use Lucent\LucentException;
|
use Lucent\LucentException;
|
||||||
use Lucent\Data\File as DataFile;
|
use Lucent\Data\File as DataFile;
|
||||||
|
use Lucent\ResultType\Error;
|
||||||
|
use Lucent\ResultType\Result;
|
||||||
|
use Lucent\ResultType\Success;
|
||||||
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||||
|
|
||||||
class FileService
|
class FileService
|
||||||
@@ -70,7 +73,10 @@ class FileService
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isImage($mimetype)) {
|
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)
|
[$width, $height] = $this->isImage($mimetype)
|
||||||
@@ -126,51 +132,65 @@ class FileService
|
|||||||
return Storage::disk(config("lucent.private_disk"));
|
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;
|
$filePath = "lucent/" . $path;
|
||||||
|
|
||||||
if (!$this->loadPublicDisk()->exists($filePath)) {
|
if (!$this->loadPublicDisk()->exists($filePath)) {
|
||||||
return;
|
return Error::create("File not found: {$filePath}");
|
||||||
}
|
}
|
||||||
|
|
||||||
$originalImage = $this->imageManager->decode(
|
$originalImage = $this->imageManager->decode(
|
||||||
$this->loadPublicDisk()->get($filePath),
|
$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 =
|
$templateUri =
|
||||||
"lucent/templates/" .
|
"lucent/templates/" .
|
||||||
$filterClassInstance->getPath() .
|
$filterClassInstance->getPath() .
|
||||||
"/" .
|
"/" .
|
||||||
substr($path, 0, strrpos($path, ".")) .
|
substr($path, 0, strrpos($path, ".")) .
|
||||||
".webp";
|
".webp";
|
||||||
$disk->put(
|
|
||||||
$templateUri,
|
|
||||||
|
|
||||||
$image->encodeUsingFormat(
|
$ok = $disk->put(
|
||||||
|
$templateUri,
|
||||||
|
(clone $originalImage)
|
||||||
|
->modify($filterClassInstance)
|
||||||
|
->encodeUsingFormat(
|
||||||
Format::WEBP,
|
Format::WEBP,
|
||||||
progressive: true,
|
progressive: true,
|
||||||
quality: 80,
|
quality: 80,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!$ok) {
|
||||||
|
return Error::create(
|
||||||
|
"Failed to write template: {$templateUri}",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$thumbDir =
|
$thumbUri =
|
||||||
"lucent/thumbs/" . substr($path, 0, strrpos($path, ".")) . ".webp";
|
"lucent/thumbs/" . substr($path, 0, strrpos($path, ".")) . ".webp";
|
||||||
|
|
||||||
$image = $originalImage->cover(300, 300);
|
$ok = $disk->put(
|
||||||
$disk->put(
|
$thumbUri,
|
||||||
$thumbDir,
|
$originalImage
|
||||||
$image->encodeUsingFormat(
|
->cover(300, 300)
|
||||||
|
->encodeUsingFormat(
|
||||||
Format::WEBP,
|
Format::WEBP,
|
||||||
progressive: true,
|
progressive: true,
|
||||||
quality: 80,
|
quality: 80,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!$ok) {
|
||||||
|
return Error::create("Failed to write thumbnail: {$thumbUri}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success::create(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user