From bfad915966d78f129f92014b80ac8903ec600029 Mon Sep 17 00:00:00 2001 From: Tomas Hermanek Date: Tue, 19 Nov 2024 11:38:30 +0100 Subject: [PATCH 1/2] clean --- src/Domain/AssetFile/FileStash.php | 5 ----- src/Domain/Image/Crop/CropProcessor.php | 1 - 2 files changed, 6 deletions(-) diff --git a/src/Domain/AssetFile/FileStash.php b/src/Domain/AssetFile/FileStash.php index 62aa7bf6..0e3cdddb 100644 --- a/src/Domain/AssetFile/FileStash.php +++ b/src/Domain/AssetFile/FileStash.php @@ -15,11 +15,6 @@ class FileStash */ private array $fileDeleteStash = []; - /** - * @var array> - */ - private array $backup = []; - public function __construct( private readonly FileSystemProvider $fileSystemProvider, ) { diff --git a/src/Domain/Image/Crop/CropProcessor.php b/src/Domain/Image/Crop/CropProcessor.php index b8f83ddb..0c774435 100644 --- a/src/Domain/Image/Crop/CropProcessor.php +++ b/src/Domain/Image/Crop/CropProcessor.php @@ -48,7 +48,6 @@ public function applyCrop(ImageFile $image, ImageCropDto $imageCrop): string optimalResize: $optimalResize, imageCrop: $imageCrop ); - // dd($optimalResize->getFilePath()); $fileStream = $this->fileSystemProvider ->getFilesystemByStorable($image) From f31892ffe4eeebc496dc60d932139282addc44f2 Mon Sep 17 00:00:00 2001 From: Tomas Hermanek Date: Tue, 19 Nov 2024 15:40:40 +0100 Subject: [PATCH 2/2] Extended command attrs. --- .../ReprocessImageOptimalCropCommand.php | 70 +++++++++++++++---- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/src/Command/ReprocessImageOptimalCropCommand.php b/src/Command/ReprocessImageOptimalCropCommand.php index 87c3e420..709594aa 100644 --- a/src/Command/ReprocessImageOptimalCropCommand.php +++ b/src/Command/ReprocessImageOptimalCropCommand.php @@ -6,12 +6,16 @@ use AnzuSystems\CoreDamBundle\Command\Traits\OutputUtilTrait; use AnzuSystems\CoreDamBundle\Domain\Image\ImageFacade; +use AnzuSystems\CoreDamBundle\Entity\ImageFile; use AnzuSystems\CoreDamBundle\Repository\ImageFileRepository; use Exception; +use Generator; +use SplFileObject; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand( @@ -21,7 +25,9 @@ final class ReprocessImageOptimalCropCommand extends Command { use OutputUtilTrait; - private const string IMAGE_FILE_ARG = 'image'; + private const string IMAGE_FILE_OPT = 'image'; + + private const string IMAGE_ID_FILE_PATH = 'file'; private const int MAX_ASSETS_PER_JOB = 20; public function __construct( @@ -34,9 +40,13 @@ public function __construct( public function configure(): void { $this - ->addArgument( - name: self::IMAGE_FILE_ARG, - mode: InputArgument::REQUIRED, + ->addOption( + name: self::IMAGE_FILE_OPT, + mode: InputOption::VALUE_REQUIRED, + ) + ->addOption( + name: self::IMAGE_ID_FILE_PATH, + mode: InputOption::VALUE_REQUIRED, ) ; } @@ -46,17 +56,53 @@ public function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - $imageId = $input->getArgument(self::IMAGE_FILE_ARG); - $image = $this->imageFileRepository->find($imageId); + $progressBar = new ProgressBar($output); + $progressBar->start(); + foreach ($this->getImages($input) as $image) { + $this->imageFacade->reprocessOptimalCrop($image); + } + + $progressBar->finish(); + $output->writeln(''); + + return Command::SUCCESS; + } + + /** + * @return Generator + */ + private function getImages(InputInterface $input): Generator + { + $imageId = $input->getOption(self::IMAGE_FILE_OPT); - if (null === $image) { - $output->writeln("Image not found: ({$imageId})"); + if (is_string($imageId)) { + $image = $this->imageFileRepository->find($imageId); - return Command::FAILURE; + if ($image instanceof ImageFile) { + yield $image; + } } - $this->imageFacade->reprocessOptimalCrop($image); + $filePath = $input->getOption(self::IMAGE_ID_FILE_PATH); + if (null === $filePath || false === file_exists($filePath)) { + return; + } - return Command::SUCCESS; + $csv = new SplFileObject($filePath); + $csv->setFlags(SplFileObject::SKIP_EMPTY); + + while (false === $csv->eof()) { + $row = $csv->fgetcsv(); + + if (false === is_array($row) || false === isset($row[0])) { + continue; + } + + $image = $this->imageFileRepository->find($imageId); + + if ($image instanceof ImageFile) { + yield $image; + } + } } }