Skip to content

Commit

Permalink
clean (#92)
Browse files Browse the repository at this point in the history
Command for recompute optimal crops.
  • Loading branch information
TomasHermanek authored Nov 19, 2024
1 parent 36db5fc commit 6dccfd6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
70 changes: 58 additions & 12 deletions src/Command/ReprocessImageOptimalCropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
)
;
}
Expand All @@ -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<ImageFile>
*/
private function getImages(InputInterface $input): Generator
{
$imageId = $input->getOption(self::IMAGE_FILE_OPT);

if (null === $image) {
$output->writeln("<error>Image not found: ({$imageId})</error>");
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;
}
}
}
}
5 changes: 0 additions & 5 deletions src/Domain/AssetFile/FileStash.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class FileStash
*/
private array $fileDeleteStash = [];

/**
* @var array<string, array<int, string>>
*/
private array $backup = [];

public function __construct(
private readonly FileSystemProvider $fileSystemProvider,
) {
Expand Down
1 change: 0 additions & 1 deletion src/Domain/Image/Crop/CropProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function applyCrop(ImageFile $image, ImageCropDto $imageCrop): string
optimalResize: $optimalResize,
imageCrop: $imageCrop
);
// dd($optimalResize->getFilePath());

$fileStream = $this->fileSystemProvider
->getFilesystemByStorable($image)
Expand Down

0 comments on commit 6dccfd6

Please sign in to comment.