diff --git a/src/Commands/RebuildThumbnails.php b/src/Commands/RebuildThumbnails.php index b18d3b2..90cd803 100644 --- a/src/Commands/RebuildThumbnails.php +++ b/src/Commands/RebuildThumbnails.php @@ -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); } } diff --git a/src/File/FileService.php b/src/File/FileService.php index 676b437..b12e74f 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -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);