File size: 2,814 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Mautic\CoreBundle\Command;

use Mautic\CoreBundle\Helper\PathsHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Finder\Finder;

/**
 * CLI Command to clean up obsolete files in the media folder.
 */
class CleanupMediaAssetsCommand extends Command
{
    public function __construct(
        private PathsHelper $pathsHelper
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->setName('mautic:assets:cleanup')
          ->setHelp(
              <<<'EOT'
                The <info>%command.name%</info> command is used to clean up obsolete files in the media folder that are present in the app/assets folder.

<info>php %command.full_name%</info>
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('<info>'.count($files_to_delete).' obsolete files found</info>');

        if (count($files_to_delete)) {
            foreach ($files_to_delete as $file) {
                $output->writeln('<comment> - '.$file.'</comment>');
            }
            $output->writeln('');

            /** @var \Symfony\Component\Console\Helper\SymfonyQuestionHelper $helper */
            $helper   = $this->getHelperSet()->get('question');
            $question = new ConfirmationQuestion(
                '<question>delete files?</question> ', 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';
}