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);
}
}
+1 -3
View File
@@ -79,7 +79,6 @@ class FileService
throw new LucentException("File $filename not uploaded");
}
// $this->createThumbnail($disk, $schema->path, $filename, $file);
$this->createTemplates($disk, $path, $file);
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
@@ -130,9 +129,8 @@ class FileService
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));
foreach (config("lucent.imageFilters") as $preset => $filterClass) {
$image = $originalImage->filter(new $filterClass);