-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathClearChunksCommand.php
68 lines (56 loc) · 1.76 KB
/
ClearChunksCommand.php
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
<?php
namespace Pion\Laravel\ChunkUpload\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Pion\Laravel\ChunkUpload\ChunkFile;
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
class ClearChunksCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'uploads:clear';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clears the chunks upload directory. Deletes only .part objects.';
/**
* Clears the chunks upload directory.
*
* @param ChunkStorage $storage injected chunk storage
*/
public function handle(ChunkStorage $storage)
{
$verbouse = OutputInterface::VERBOSITY_VERBOSE;
// try to get the old chunk files
$oldFiles = $storage->oldChunkFiles();
if ($oldFiles->isEmpty()) {
$this->warn('Chunks: no old files');
return;
}
$this->info(sprintf('Found %d chunk files', $oldFiles->count()), $verbouse);
$deleted = 0;
/** @var ChunkFile $file */
foreach ($oldFiles as $file) {
// debug the file info
$this->comment('> '.$file, $verbouse);
// delete the file
try{
if ($file->delete()) {
++$deleted;
} else {
$this->error('> chunk not deleted: '.$file);
}
}catch(RuntimeException $err){
$this->error('> chunk not deleted: '.$file);
}
}
$this->info('Chunks: cleared '.$deleted.' '.Str::plural('file', $deleted));
}
}