1 | <?php |
||||
2 | |||||
3 | namespace Jobtech\LaravelChunky; |
||||
4 | |||||
5 | use Illuminate\Http\UploadedFile; |
||||
0 ignored issues
–
show
|
|||||
6 | use Illuminate\Support\Collection; |
||||
7 | use Jobtech\LaravelChunky\Concerns\ChunkyRequestHelpers; |
||||
8 | use Jobtech\LaravelChunky\Contracts\ChunkyManager as ChunkyManagerContract; |
||||
9 | use Jobtech\LaravelChunky\Contracts\MergeHandler; |
||||
10 | use Jobtech\LaravelChunky\Exceptions\ChunksIntegrityException; |
||||
11 | use Jobtech\LaravelChunky\Http\Requests\AddChunkRequest; |
||||
12 | use Jobtech\LaravelChunky\Support\ChunksFilesystem; |
||||
13 | use Jobtech\LaravelChunky\Support\MergeFilesystem; |
||||
14 | |||||
15 | /** |
||||
16 | * @method int lastIndexFrom(AddChunkRequest $request) |
||||
17 | * @method bool isLastIndex(AddChunkRequest $request) |
||||
18 | */ |
||||
19 | class ChunkyManager implements ChunkyManagerContract |
||||
20 | { |
||||
21 | use ChunkyRequestHelpers; |
||||
22 | |||||
23 | /** @var \Jobtech\LaravelChunky\ChunkySettings */ |
||||
24 | private ChunkySettings $settings; |
||||
25 | |||||
26 | /** @var \Jobtech\LaravelChunky\Support\ChunksFilesystem */ |
||||
27 | protected ChunksFilesystem $chunksFilesystem; |
||||
28 | |||||
29 | /** @var \Jobtech\LaravelChunky\Support\MergeFilesystem */ |
||||
30 | private MergeFilesystem $mergeFilesystem; |
||||
31 | |||||
32 | /** @var \Jobtech\LaravelChunky\Contracts\MergeHandler */ |
||||
33 | private MergeHandler $mergeHandler; |
||||
0 ignored issues
–
show
|
|||||
34 | |||||
35 | public function __construct(ChunkySettings $settings) |
||||
36 | { |
||||
37 | $this->settings = $settings; |
||||
38 | |||||
39 | $this->setChunksFilesystem($settings->chunksDisk(), $settings->chunksFolder()); |
||||
0 ignored issues
–
show
It seems like
$settings->chunksDisk() can also be of type null ; however, parameter $disk of Jobtech\LaravelChunky\Ch...::setChunksFilesystem() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
40 | $this->setMergeFilesystem($settings->mergeDisk(), $settings->mergeFolder()); |
||||
0 ignored issues
–
show
It seems like
$settings->mergeDisk() can also be of type null ; however, parameter $disk of Jobtech\LaravelChunky\Ch...r::setMergeFilesystem() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * {@inheritdoc} |
||||
45 | */ |
||||
46 | public function settings(): ChunkySettings |
||||
47 | { |
||||
48 | return $this->settings; |
||||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * {@inheritdoc} |
||||
53 | */ |
||||
54 | public function setChunksFilesystem(string $disk, string $folder): ChunkyManager |
||||
55 | { |
||||
56 | $this->chunksFilesystem = ChunksFilesystem::instance( |
||||
57 | compact('disk', 'folder') |
||||
58 | ); |
||||
59 | |||||
60 | return $this; |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * {@inheritdoc} |
||||
65 | */ |
||||
66 | public function chunksFilesystem(): ChunksFilesystem |
||||
67 | { |
||||
68 | return $this->chunksFilesystem; |
||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * {@inheritdoc} |
||||
73 | */ |
||||
74 | public function setMergeFilesystem(string $disk, string $folder): ChunkyManager |
||||
75 | { |
||||
76 | $this->mergeFilesystem = MergeFilesystem::instance( |
||||
77 | compact('disk', 'folder') |
||||
78 | ); |
||||
79 | |||||
80 | return $this; |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * {@inheritdoc} |
||||
85 | */ |
||||
86 | public function mergeFilesystem(): MergeFilesystem |
||||
87 | { |
||||
88 | return $this->mergeFilesystem; |
||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * {@inheritdoc} |
||||
93 | * |
||||
94 | * @codeCoverageIgnore |
||||
95 | */ |
||||
96 | public function mergeHandler(): MergeHandler |
||||
97 | { |
||||
98 | return $this->settings->mergeHandler()->setManager($this); |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * {@inheritdoc} |
||||
103 | */ |
||||
104 | public function chunksDisk(): ?string |
||||
105 | { |
||||
106 | return $this->settings |
||||
107 | ->chunksDisk(); |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * {@inheritdoc} |
||||
112 | */ |
||||
113 | public function mergeDisk(): ?string |
||||
114 | { |
||||
115 | return $this->settings |
||||
116 | ->mergeDisk(); |
||||
117 | } |
||||
118 | |||||
119 | /** |
||||
120 | * {@inheritdoc} |
||||
121 | */ |
||||
122 | public function chunksFolder(): string |
||||
123 | { |
||||
124 | return $this->settings |
||||
125 | ->chunksFolder(); |
||||
126 | } |
||||
127 | |||||
128 | /** |
||||
129 | * {@inheritdoc} |
||||
130 | */ |
||||
131 | public function mergeFolder(): string |
||||
132 | { |
||||
133 | return $this->settings |
||||
134 | ->mergeFolder(); |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * {@inheritdoc} |
||||
139 | */ |
||||
140 | public function chunksOptions(): array |
||||
141 | { |
||||
142 | return array_merge([ |
||||
143 | 'disk' => $this->chunksDisk(), |
||||
144 | ], $this->settings->additionalChunksOptions()); |
||||
145 | } |
||||
146 | |||||
147 | /** |
||||
148 | * {@inheritdoc} |
||||
149 | */ |
||||
150 | public function mergeOptions(): array |
||||
151 | { |
||||
152 | return $this->settings->additionalMergeOptions(); |
||||
153 | } |
||||
154 | |||||
155 | /** |
||||
156 | * {@inheritdoc} |
||||
157 | */ |
||||
158 | public function listChunks(?string $folder = null): Collection |
||||
159 | { |
||||
160 | return $this->chunksFilesystem()->listChunks($folder); |
||||
0 ignored issues
–
show
It seems like
$folder can also be of type null ; however, parameter $folder of Jobtech\LaravelChunky\Su...ilesystem::listChunks() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
161 | } |
||||
162 | |||||
163 | /** |
||||
164 | * {@inheritdoc} |
||||
165 | */ |
||||
166 | public function readChunk($chunk) |
||||
167 | { |
||||
168 | if ($chunk instanceof Chunk) { |
||||
169 | $chunk = $chunk->getPath(); |
||||
170 | } |
||||
171 | |||||
172 | return $this->chunksFilesystem() |
||||
173 | ->readChunk($chunk); |
||||
174 | } |
||||
175 | |||||
176 | /** |
||||
177 | * {@inheritdoc} |
||||
178 | */ |
||||
179 | public function addChunk(UploadedFile $file, int $index, string $folder): Chunk |
||||
180 | { |
||||
181 | // Check integrity |
||||
182 | if (! $this->checkChunks($folder, $index)) { |
||||
183 | throw new ChunksIntegrityException("Uploaded chunk with index {$index} violates the integrity"); |
||||
184 | } |
||||
185 | |||||
186 | $chunk = Chunk::create($file, $index, $this->chunksOptions()); |
||||
187 | |||||
188 | // Store chunk |
||||
189 | return $this->chunksFilesystem() |
||||
190 | ->store($chunk, $folder, $this->chunksOptions()); |
||||
191 | } |
||||
192 | |||||
193 | /** |
||||
194 | * {@inheritdoc} |
||||
195 | */ |
||||
196 | public function deleteChunks(string $folder): bool |
||||
197 | { |
||||
198 | return $this->chunksFilesystem()->delete($folder); |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * @param string $folder |
||||
203 | * @return bool |
||||
204 | */ |
||||
205 | public function isValidChunksFolder(string $folder): bool |
||||
206 | { |
||||
207 | return $this->chunksFilesystem()->exists($folder); |
||||
208 | } |
||||
209 | |||||
210 | /** |
||||
211 | * {@inheritdoc} |
||||
212 | */ |
||||
213 | public function checkChunks(string $folder, int $index): bool |
||||
214 | { |
||||
215 | $default = $this->settings->defaultIndex(); |
||||
216 | |||||
217 | if (! $this->chunksFilesystem()->exists($folder) && $index != $default) { |
||||
218 | return false; |
||||
219 | } |
||||
220 | |||||
221 | if ($this->chunksFilesystem()->exists($folder)) { |
||||
222 | if (ChunkySettings::INDEX_ZERO != $default) { |
||||
223 | $index -= $default; |
||||
224 | } |
||||
225 | |||||
226 | return $this->chunksFilesystem()->chunksCount($folder) == $index; |
||||
227 | } |
||||
228 | |||||
229 | if ($index == $default) { |
||||
230 | if (! $this->chunksFilesystem()->makeDirectory($folder)) { |
||||
231 | throw new ChunksIntegrityException("Cannot create chunks folder $folder"); |
||||
232 | } |
||||
233 | } |
||||
234 | |||||
235 | return true; |
||||
236 | } |
||||
237 | |||||
238 | /** |
||||
239 | * {@inheritdoc} |
||||
240 | */ |
||||
241 | public function checkChunksIntegrity(string $folder, int $chunk_size, int $total_size): bool |
||||
242 | { |
||||
243 | $total = 0; |
||||
244 | $chunks = $this->listChunks($folder); |
||||
245 | |||||
246 | foreach ($chunks as $chunk) { |
||||
247 | $size = $this->chunksFilesystem()->chunkSize($chunk->getPath()); |
||||
248 | |||||
249 | if ($size < $chunk_size && ! $chunk->isLast()) { |
||||
250 | return false; |
||||
251 | } |
||||
252 | |||||
253 | $total += $size; |
||||
254 | } |
||||
255 | |||||
256 | return $total >= $total_size; |
||||
257 | } |
||||
258 | |||||
259 | /** |
||||
260 | * {@inheritdoc} |
||||
261 | */ |
||||
262 | public function handle(AddChunkRequest $request, ?string $folder = null): Chunk |
||||
263 | { |
||||
264 | // Store chunk |
||||
265 | $folder = $this->checkFolder($request, $folder); |
||||
266 | $chunk = $this->addChunk( |
||||
267 | $request->fileInput(), |
||||
268 | $request->indexInput(), |
||||
269 | $folder |
||||
270 | ); |
||||
271 | |||||
272 | $chunk->setLast( |
||||
273 | $this->isLastIndex($request) |
||||
274 | ); |
||||
275 | |||||
276 | // Check merge |
||||
277 | if ($chunk->isLast() && $this->settings->autoMerge()) { |
||||
278 | $this->mergeHandler()->dispatchMerge($request, $folder); |
||||
279 | } |
||||
280 | |||||
281 | return $chunk; |
||||
282 | } |
||||
283 | |||||
284 | /** |
||||
285 | * {@inheritdoc} |
||||
286 | */ |
||||
287 | public function merge(string $chunks_folder, string $merge_path): string |
||||
288 | { |
||||
289 | return $this->mergeHandler()->merge($chunks_folder, $merge_path); |
||||
290 | } |
||||
291 | } |
||||
292 |
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