|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Command; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
10
|
|
|
use Chamilo\CoreBundle\Helpers\TempUploadHelper; |
|
11
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
12
|
|
|
use Symfony\Component\Console\Command\Command; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
17
|
|
|
|
|
18
|
|
|
#[AsCommand( |
|
19
|
|
|
name: 'cache:clear-uploads', |
|
20
|
|
|
description: 'Clear temporary uploaded files (async upload chunks/cache).', |
|
21
|
|
|
)] |
|
22
|
|
|
final class ClearTempUploadsCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
private readonly TempUploadHelper $helper |
|
26
|
|
|
) { |
|
27
|
|
|
parent::__construct(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function configure(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this |
|
33
|
|
|
->addOption('older-than', null, InputOption::VALUE_REQUIRED, 'Minutes threshold (0 = delete all)', '60') |
|
34
|
|
|
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Preview only, do not delete') |
|
35
|
|
|
->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Override temp upload directory') |
|
36
|
|
|
->setHelp( |
|
37
|
|
|
<<<'HELP' |
|
38
|
|
|
Clears the configured temporary uploads directory (async upload chunks/cache). |
|
39
|
|
|
Options: |
|
40
|
|
|
--older-than=MIN Only delete files older than MIN minutes (default: 60). Use 0 to delete all files. |
|
41
|
|
|
--dry-run Report what would be deleted without deleting. |
|
42
|
|
|
--dir=PATH Override the configured temp directory for this run. |
|
43
|
|
|
|
|
44
|
|
|
Examples: |
|
45
|
|
|
php bin/console cache:clear-uploads --older-than=60 |
|
46
|
|
|
php bin/console cache:clear-uploads --dry-run |
|
47
|
|
|
php bin/console cache:clear-uploads --dir=/var/www/chamilo/var/uploads_tmp |
|
48
|
|
|
HELP |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
53
|
|
|
{ |
|
54
|
|
|
$kernelContainer = $this->getApplication()?->getKernel()?->getContainer(); |
|
55
|
|
|
if ($kernelContainer) { |
|
56
|
|
|
Container::setContainer($kernelContainer); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$io = new SymfonyStyle($input, $output); |
|
60
|
|
|
$olderThan = (int) $input->getOption('older-than'); |
|
61
|
|
|
$dryRun = (bool) $input->getOption('dry-run'); |
|
62
|
|
|
$dir = $input->getOption('dir'); |
|
63
|
|
|
|
|
64
|
|
|
if ($olderThan < 0) { |
|
65
|
|
|
$io->error('Option --older-than must be >= 0.'); |
|
66
|
|
|
return Command::INVALID; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Select helper (allow --dir override) |
|
70
|
|
|
$targetHelper = $this->helper; |
|
71
|
|
|
if (!empty($dir)) { |
|
72
|
|
|
// quick override instance (no service registration needed) |
|
73
|
|
|
$targetHelper = new TempUploadHelper($dir); |
|
74
|
|
|
if (!is_dir($targetHelper->getTempDir()) || !is_readable($targetHelper->getTempDir())) { |
|
75
|
|
|
$io->error(sprintf('Directory not readable: %s', $targetHelper->getTempDir())); |
|
76
|
|
|
return Command::FAILURE; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$tempDir = $targetHelper->getTempDir(); |
|
81
|
|
|
|
|
82
|
|
|
// Run purge |
|
83
|
|
|
$stats = $targetHelper->purge($olderThan, $dryRun); |
|
84
|
|
|
|
|
85
|
|
|
$mb = $stats['bytes'] / 1048576; |
|
86
|
|
|
if ($dryRun) { |
|
87
|
|
|
$io->note(sprintf( |
|
88
|
|
|
'DRY RUN: %d files (%.2f MB) would be removed in %s', |
|
89
|
|
|
$stats['files'], |
|
90
|
|
|
$mb, |
|
91
|
|
|
$tempDir |
|
92
|
|
|
)); |
|
93
|
|
|
} else { |
|
94
|
|
|
$io->success(sprintf( |
|
95
|
|
|
'CLEANED: %d files removed (%.2f MB) in %s', |
|
96
|
|
|
$stats['files'], |
|
97
|
|
|
$mb, |
|
98
|
|
|
$tempDir |
|
99
|
|
|
)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return Command::SUCCESS; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|