Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions Command/Catalog/Media/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/
namespace MagentoCode\CliMediaTool\Command\Catalog\Media;

use mysql_xdevapi\Exception;
use MagentoCode\CliMediaTool\Command\CatalogAbstract;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use MagentoCode\CliMediaTool\Command\CatalogAbstract;

class Cleanup extends CatalogAbstract
{
Expand Down Expand Up @@ -38,15 +37,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
$filePaths = $this->getFilePaths(false);
$mediaGalleryPaths = $this->getMediaGalleryPaths(false);
$output->writeln(
sprintf(
' Files: %d
Media Gallery Entries: %d
===============================================',
count($filePaths),
count($mediaGalleryPaths)
)
);
$output->writeln(sprintf(' Files: %d', count($filePaths)));
$output->writeln(sprintf(' Media Gallery Entries: %d', count($mediaGalleryPaths)));
$output->writeln('===============================================');

/*
* Cleanup orphaned media gallery entries that no longer have products using them
Expand All @@ -68,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(
sprintf(
'Removed %d media gallery paths that no longer have images on the filesystem.',
count($orphanedMediaGalleryPaths)
count($missingFilePaths)
)
);

Expand All @@ -80,7 +73,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(
sprintf(
'Removed %d files which are no longer present in the media gallery database table.',
count($orphanedMediaGalleryPaths)
count($unusedFilePaths)
)
);

/*
* Remove temporary files
*/
$maxLifetimeInHours = Temporary::DEFAULT_MAX_LIFETIME;
$temporaryFilePaths = $this->getTemporaryFilePaths(false, $maxLifetimeInHours);
$this->deleteFilePaths($temporaryFilePaths);
$output->writeln(
sprintf(
'Removed %d temporary files older that %d hour(s).',
count($unusedFilePaths),
$maxLifetimeInHours
)
);

Expand All @@ -92,15 +99,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('===============================================');
$output->writeln('=== Done ===');
$output->writeln('===============================================');
$output->writeln(
sprintf(
' Files: %d
Media Gallery Entries: %d
===============================================',
count($filePaths),
count($mediaGalleryPaths)
)
);
$output->writeln(sprintf(' Files: %d', count($filePaths)));
$output->writeln(sprintf(' Media Gallery Entries: %d', count($mediaGalleryPaths)));
$output->writeln('===============================================');
} catch (\Exception $exception) {
$output->writeln(['Exception: ', $exception->getMessage()]);
}
Expand Down
5 changes: 3 additions & 2 deletions Command/Catalog/Media/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/
namespace MagentoCode\CliMediaTool\Command\Catalog\Media;

use mysql_xdevapi\Exception;
use MagentoCode\CliMediaTool\Command\CatalogAbstract;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use MagentoCode\CliMediaTool\Command\CatalogAbstract;

class Info extends CatalogAbstract
{
Expand All @@ -36,6 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$cacheFilePaths = $this->getCacheFilePaths();
$unusedFiles = $this->getUnusedFilePaths();
$missingFiles = $this->getMissingFilePaths();
$temporaryFiles = $this->getTemporaryFilePaths();

$output->writeln('===============================================');
$output->writeln(sprintf('Media Gallery entries: %s.', count($mediaGalleryPaths)));
Expand All @@ -45,6 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf('Unused files on filesystem: %s.', count($unusedFiles)));
$output->writeln(sprintf('Missing files on filesystem: %s.', count($missingFiles)));
$output->writeln(sprintf('Orphaned files on filesystem: %s.', count($orphanedFilePaths)));
$output->writeln(sprintf('Temporary files on filesystem: %s.', count($temporaryFiles)));
$output->writeln('===============================================');
$output->writeln('To automatically clean up the media gallery, run catalog:media:cleanup');
} catch (\Exception $exception) {
Expand Down
51 changes: 51 additions & 0 deletions Command/Catalog/Media/Temporary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @package MagentoCode\CliMediaTool
* @author Brian Neff <bneff84@gmail.com>
* @license MIT
*/
namespace MagentoCode\CliMediaTool\Command\Catalog\Media;

use MagentoCode\CliMediaTool\Command\CatalogAbstract;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Temporary extends CatalogAbstract
{
public const OPTION_MAX_LIFETIME = 'max_lifetime';
public const DEFAULT_MAX_LIFETIME = 24;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('catalog:media:temporary')
->addOption(
self::OPTION_MAX_LIFETIME,
'm',
InputArgument::OPTIONAL,
'Max temporary file lifetime in hours?',
self::DEFAULT_MAX_LIFETIME
)
->setDescription(
'Get a list of product media temporary images which exist on the filesystem.'
);
parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$maxLifetimeInHours = (int)$input->getOption(self::OPTION_MAX_LIFETIME);
$temporaryFiles = $this->getTemporaryFilePaths(true, $maxLifetimeInHours);
$output->writeln($temporaryFiles);
} catch (\Exception $exception) {
$output->writeln(['Exception: ', $exception->getMessage()]);
}
}
}
55 changes: 55 additions & 0 deletions Command/Catalog/Media/Temporary/Remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @package MagentoCode\CliMediaTool
* @author Brian Neff <bneff84@gmail.com>
* @license MIT
*/
namespace MagentoCode\CliMediaTool\Command\Catalog\Media\Temporary;

use Exception;
use MagentoCode\CliMediaTool\Command\Catalog\Media\Temporary;
use MagentoCode\CliMediaTool\Command\CatalogAbstract;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Remove extends CatalogAbstract
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('catalog:media:temporary:remove')
->addOption(
Temporary::OPTION_MAX_LIFETIME,
'm',
InputArgument::OPTIONAL,
'Max temporary file lifetime in hours?',
Temporary::DEFAULT_MAX_LIFETIME
)
->setDescription(
'Remove all temporary product media images which exist on the filesystem.'
);
parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$maxLifetimeInHours = (int)$input->getOption(Temporary::OPTION_MAX_LIFETIME);
$temporaryFilePaths = $this->getTemporaryFilePaths(true, $maxLifetimeInHours);
$this->deleteFilePaths($temporaryFilePaths);
$output->writeln(sprintf(
'Removed %d temporary files older that %d hour(s).',
count($temporaryFilePaths),
$maxLifetimeInHours
));
} catch (Exception $exception) {
$output->writeln(['Exception: ', $exception->getMessage()]);
}
}
}
Loading