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 Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\File\FileService;
use Lucent\Query\Query;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema;
use Lucent\Schema\Type;
@@ -18,7 +21,10 @@ class RebuildThumbnails extends Command
protected $description = 'Rebuilds thumbnails for path';
public function __construct(public ImageManager $imageManager)
public function __construct(
public Query $query,
public FileService $fileService,
)
{
parent::__construct();
}
@@ -27,49 +33,19 @@ class RebuildThumbnails extends Command
public function handle(ChannelService $channelService): int
{
$channelService->channel->schemas
->where("type", Type::FILES)->values()
->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)
->map([$this, 'rebuildThumbnails']);
return 0;
}
public function rebuildThumbnails(Schema $schema): void
public function rebuildThumbnails(FilesSchema $schema): void
{
$filesDir = storage_path("app/public/" . $schema->path . "/");
$thumbDir = storage_path("app/public/thumbs/" . $schema->path . "/");
if (!file_exists($thumbDir)) {
make_dir_r($thumbDir);
}
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());
$this->info("Rebuilding thumbnails for ". $schema->name);
$records = $this->query->filter(["schema" => $schema->name])->run()->records;
$disk = $this->fileService->loadDisk($schema->disk);
foreach ($records as $record) {
$this->fileService->createTemplates($disk, $record->_file->path);
}
}