setName('mautic:assets:cleanup')
->setHelp(
<<<'EOT'
The %command.name% command is used to clean up obsolete files in the media folder that are present in the app/assets folder.
php %command.full_name%
EOT
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$assetsPath = $this->pathsHelper->getAssetsPath();
$mediaPath = $this->pathsHelper->getMediaPath();
$finder = new Finder();
$finder->files()->in($assetsPath)->path(['images', 'dashboards'])->notName('.htaccess');
$files_to_delete = [];
foreach ($finder as $file) {
$absoluteFilePath = $file->getRealPath();
$relativeFilePath = $file->getRelativePathname();
$md5_source = md5_file($absoluteFilePath);
$mediaOverride = $mediaPath.'/'.$relativeFilePath;
if (file_exists($mediaOverride)) {
$md5_override = md5_file($mediaOverride);
if ($md5_source == $md5_override) {
$files_to_delete[] = $mediaOverride;
}
}
}
$output->writeln(''.count($files_to_delete).' obsolete files found');
if (count($files_to_delete)) {
foreach ($files_to_delete as $file) {
$output->writeln(' - '.$file.'');
}
$output->writeln('');
/** @var \Symfony\Component\Console\Helper\SymfonyQuestionHelper $helper */
$helper = $this->getHelperSet()->get('question');
$question = new ConfirmationQuestion(
'delete files? ', false
);
if ($helper->ask($input, $output, $question)) {
foreach ($files_to_delete as $file) {
unlink($file);
}
}
}
return Command::SUCCESS;
}
protected static $defaultDescription = 'Cleans up obsolete files in the media folder that are present in the app/assets folder';
}