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