rebuilding thumbnails command

This commit is contained in:
2024-09-27 15:32:35 +03:00
parent 63232585ab
commit 6458c1e71d
2 changed files with 15 additions and 41 deletions
+14 -38
View File
@@ -7,6 +7,9 @@ use Exception;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\File\FileService;
use Lucent\Query\Query;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema; use Lucent\Schema\Schema;
use Lucent\Schema\Type; use Lucent\Schema\Type;
@@ -18,7 +21,10 @@ class RebuildThumbnails extends Command
protected $description = 'Rebuilds thumbnails for path'; protected $description = 'Rebuilds thumbnails for path';
public function __construct(public ImageManager $imageManager) public function __construct(
public Query $query,
public FileService $fileService,
)
{ {
parent::__construct(); parent::__construct();
} }
@@ -27,49 +33,19 @@ class RebuildThumbnails extends Command
public function handle(ChannelService $channelService): int public function handle(ChannelService $channelService): int
{ {
$channelService->channel->schemas $channelService->channel->schemas
->where("type", Type::FILES)->values() ->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)
->map([$this, 'rebuildThumbnails']); ->map([$this, 'rebuildThumbnails']);
return 0; return 0;
} }
public function rebuildThumbnails(Schema $schema): void public function rebuildThumbnails(FilesSchema $schema): void
{ {
$this->info("Rebuilding thumbnails for ". $schema->name);
$filesDir = storage_path("app/public/" . $schema->path . "/"); $records = $this->query->filter(["schema" => $schema->name])->run()->records;
$thumbDir = storage_path("app/public/thumbs/" . $schema->path . "/"); $disk = $this->fileService->loadDisk($schema->disk);
if (!file_exists($thumbDir)) { foreach ($records as $record) {
make_dir_r($thumbDir); $this->fileService->createTemplates($disk, $record->_file->path);
}
if (!file_exists($filesDir)) {
make_dir_r($filesDir);
}
$filesDirIterator = new DirectoryIterator($filesDir);
foreach ($filesDirIterator as $file) {
if ($file->isDot()) {
continue;
}
try {
$image = $this->imageManager->make($filesDir . $file->getFilename());
} catch (Exception $e) {
$this->error($e->getMessage());
continue;
}
$image->fit(300, 300);
try {
$image->encode('webp', 75)->save($thumbDir . $file->getFilename());
} catch (Exception $e) {
$this->error($e->getMessage());
continue;
}
$this->info($file->getFilename());
} }
} }
+1 -3
View File
@@ -79,7 +79,6 @@ class FileService
throw new LucentException("File $filename not uploaded"); throw new LucentException("File $filename not uploaded");
} }
// $this->createThumbnail($disk, $schema->path, $filename, $file);
$this->createTemplates($disk, $path, $file); $this->createTemplates($disk, $path, $file);
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0]; list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
@@ -130,9 +129,8 @@ class FileService
return $record->id ?? ""; return $record->id ?? "";
} }
private function createTemplates(Filesystem $disk, string $path, UploadedFile $file): void public function createTemplates(Filesystem $disk, string $path): void
{ {
$originalImage = $this->imageManager->make($disk->get($path)); $originalImage = $this->imageManager->make($disk->get($path));
foreach (config("lucent.imageFilters") as $preset => $filterClass) { foreach (config("lucent.imageFilters") as $preset => $filterClass) {
$image = $originalImage->filter(new $filterClass); $image = $originalImage->filter(new $filterClass);