|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jobtech\LaravelChunky\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Jobtech\LaravelChunky\Events\ChunkDeleted; |
|
7
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
8
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
|
9
|
|
|
|
|
10
|
|
|
trait ChunksHelpers |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* This method checks if the application is running in console and, if output style is not null, it creates a new |
|
14
|
|
|
* progress bar instance, otherwise returns null. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Symfony\Component\Console\Style\OutputStyle|null $output |
|
17
|
|
|
* @param int $count |
|
18
|
|
|
* |
|
19
|
|
|
* @return \Symfony\Component\Console\Helper\ProgressBar|null |
|
20
|
|
|
*/ |
|
21
|
|
|
public function hasProgressBar(?OutputStyle $output, int $count): ?ProgressBar |
|
22
|
|
|
{ |
|
23
|
|
|
if ($output !== null && app()->runningInConsole()) { |
|
|
|
|
|
|
24
|
|
|
return $output->createProgressBar($count); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Check if the given chunks folder exists. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $folder |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function chunksFolderExists($folder = ''): bool |
|
38
|
|
|
{ |
|
39
|
|
|
if (! Str::startsWith($folder, $this->getChunksFolder())) { |
|
|
|
|
|
|
40
|
|
|
$folder = $this->getChunksFolder().$folder; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->chunksFilesystem() |
|
|
|
|
|
|
44
|
|
|
->exists($folder); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Delete all chunks folders and their content. |
|
49
|
|
|
* |
|
50
|
|
|
* @param null $output |
|
|
|
|
|
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function deleteAllChunks($output = null): bool |
|
54
|
|
|
{ |
|
55
|
|
|
$folders = $this->chunksFilesystem() |
|
56
|
|
|
->directories( |
|
57
|
|
|
$this->getChunksFolder() |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$progress_bar = $this->hasProgressBar($output, count($folders)); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
foreach ($folders as $folder) { |
|
63
|
|
|
if (! $this->deleteChunks($folder)) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($progress_bar !== null) { |
|
68
|
|
|
$progress_bar->advance(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Delete all chunks and, once empty, delete the folder. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $folder |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function deleteChunks(string $folder): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (! $this->chunksFolderExists($folder)) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$files = $this->chunks($folder); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
foreach ($files as $chunk) { |
|
91
|
|
|
$deleted = $this->chunksFilesystem() |
|
92
|
|
|
->delete($chunk->getPath()); |
|
93
|
|
|
|
|
94
|
|
|
if (! $deleted) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
event(new ChunkDeleted($chunk)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->chunksFilesystem() |
|
102
|
|
|
->deleteDirectory($folder); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|