1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jobtech\LaravelChunky\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Jobtech\LaravelChunky\Chunk; |
9
|
|
|
use Jobtech\LaravelChunky\Events\ChunkAdded; |
10
|
|
|
use Jobtech\LaravelChunky\Events\ChunkDeleted; |
11
|
|
|
use Jobtech\LaravelChunky\Exceptions\ChunkyException; |
12
|
|
|
use Keven\Flysystem\Concatenate\Concatenate; |
13
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class ChunksFilesystem extends Filesystem |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param string $folder |
19
|
|
|
* @return \Illuminate\Support\Collection |
20
|
|
|
*/ |
21
|
|
|
public function listChunks(string $folder): Collection |
22
|
|
|
{ |
23
|
|
|
$folder = $this->path($folder); |
24
|
|
|
$files = $this->list($folder); |
25
|
|
|
|
26
|
|
|
return collect($files) |
27
|
|
|
->map(function ($path, $key) use ($folder, $files) { |
28
|
|
|
$filename = str_replace($folder.DIRECTORY_SEPARATOR, '', $path); |
29
|
|
|
$exploded_name = explode('_', $filename); |
30
|
|
|
|
31
|
|
|
$index = array_shift($exploded_name); |
32
|
|
|
$last = count($files) - 1 == $key; |
33
|
|
|
|
34
|
|
|
return new Chunk(intval($index), $path, $this->disk(), $last); |
35
|
|
|
})->sortBy(function (Chunk $chunk) { |
36
|
|
|
return $chunk->getIndex(); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function chunkFolders(): array |
44
|
|
|
{ |
45
|
|
|
return $this->filesystem()->disk( |
46
|
|
|
$this->disk() |
47
|
|
|
)->directories( |
48
|
|
|
$this->folder() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $folder |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
public function chunksCount(string $folder): int |
57
|
|
|
{ |
58
|
|
|
return count($this->list($this->path($folder))); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $path |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
public function chunkSize(string $path): int |
66
|
|
|
{ |
67
|
|
|
return $this->filesystem()->disk($this->disk())->size($this->path($path)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @return resource|null |
73
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
74
|
|
|
*/ |
75
|
|
|
public function readChunk(string $path) |
76
|
|
|
{ |
77
|
|
|
return $this->filesystem()->disk($this->disk())->readStream($this->path($path)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \Jobtech\LaravelChunky\Chunk $chunk |
82
|
|
|
* @param string $folder |
83
|
|
|
* @param array $options |
84
|
|
|
* |
85
|
|
|
* @return Chunk |
86
|
|
|
*/ |
87
|
|
|
public function store(Chunk $chunk, string $folder, $options = []): Chunk |
88
|
|
|
{ |
89
|
|
|
if (! $chunk->getOriginalPath() instanceof File) { |
90
|
|
|
throw new ChunkyException('Path must be a file'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Build destination |
94
|
|
|
$suffix = Str::endsWith($folder, DIRECTORY_SEPARATOR) ? '' : DIRECTORY_SEPARATOR; |
95
|
|
|
$destination = $this->path($folder.$suffix.$chunk->getSlug()); |
96
|
|
|
|
97
|
|
|
// Copy file |
98
|
|
|
$file = fopen($chunk->getPath(), 'r'); |
99
|
|
|
|
100
|
|
|
Arr::pull($options, 'disk'); |
101
|
|
|
$this->filesystem()->disk($this->disk())->put($destination, $file, $options); |
|
|
|
|
102
|
|
|
fclose($file); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Return chunk |
105
|
|
|
$chunk->setPath($destination); |
106
|
|
|
event(new ChunkAdded($chunk)); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return $chunk; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Delete all chunks and, once empty, delete the folder. |
113
|
|
|
* |
114
|
|
|
* @param \Jobtech\LaravelChunky\Chunk $chunk |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function deleteChunk(Chunk $chunk): bool |
118
|
|
|
{ |
119
|
|
|
if (! $this->filesystem()->disk($this->disk())->exists($chunk->getPath())) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$deleted = $this->filesystem()->disk($this->disk())->delete($chunk->getPath()); |
124
|
|
|
|
125
|
|
|
if ($deleted) { |
126
|
|
|
event(new ChunkDeleted($chunk)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $deleted; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Delete all chunks and, once empty, delete the folder. |
134
|
|
|
* |
135
|
|
|
* @param string $folder |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function delete(string $folder): bool |
140
|
|
|
{ |
141
|
|
|
$folder = $this->path($folder); |
142
|
|
|
|
143
|
|
|
if (! $this->filesystem()->disk($this->disk())->exists($folder)) { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach ($this->listChunks($folder) as $chunk) { |
148
|
|
|
$this->deleteChunk($chunk); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->filesystem()->disk($this->disk()) |
152
|
|
|
->deleteDirectory($folder); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Concatenate all chunks into final merge. |
157
|
|
|
* |
158
|
|
|
* @param string $chunk |
159
|
|
|
* @param array $chunks |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function concatenate(string $chunk, array $chunks): bool |
163
|
|
|
{ |
164
|
|
|
$this->filesystem()->disk($this->disk())->addPlugin(new Concatenate); |
|
|
|
|
165
|
|
|
|
166
|
|
|
return $this->filesystem()->disk($this->disk())->concatenate($chunk, ...$chunks); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths