1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jobtech\LaravelChunky; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
7
|
|
|
use Illuminate\Contracts\Support\Responsable; |
8
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
|
|
|
9
|
|
|
use Illuminate\Http\UploadedFile; |
|
|
|
|
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
13
|
|
|
use Jobtech\LaravelChunky\Http\Resources\ChunkResource; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
|
|
|
15
|
|
|
|
16
|
|
|
class Chunk implements Arrayable, Jsonable, Responsable |
17
|
|
|
{ |
18
|
|
|
use ForwardsCalls; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
private $show_file_info = true; |
22
|
|
|
|
23
|
|
|
/** @var int */ |
24
|
|
|
private $index; |
25
|
|
|
|
26
|
|
|
/** @var \Symfony\Component\HttpFoundation\File\File|string */ |
27
|
|
|
private $path; |
28
|
|
|
|
29
|
|
|
/** @var string|null */ |
30
|
|
|
private $disk; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
private $last; |
34
|
|
|
|
35
|
|
|
public function __construct(int $index, $path, $disk = null, $last = false) |
36
|
|
|
{ |
37
|
|
|
$this->index = $index; |
38
|
|
|
$this->path = $path; |
39
|
|
|
$this->disk = $disk; |
40
|
|
|
$this->last = $last; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getIndex(): int |
47
|
|
|
{ |
48
|
|
|
return $this->index; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getPath(): string |
55
|
|
|
{ |
56
|
|
|
if ($this->path instanceof File) { |
57
|
|
|
return $this->path->getRealPath(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->path; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function getOriginalPath() |
67
|
|
|
{ |
68
|
|
|
return $this->path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $path |
73
|
|
|
*/ |
74
|
|
|
public function setPath($path) |
75
|
|
|
{ |
76
|
|
|
$this->path = $path; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|null $suffix |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getFilename($suffix = null): string |
84
|
|
|
{ |
85
|
|
|
if ($this->path instanceof UploadedFile) { |
86
|
|
|
return basename($this->path->getClientOriginalName(), $suffix); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->path instanceof File) { |
|
|
|
|
90
|
|
|
return $this->path->getBasename($suffix); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return basename($this->path, $suffix); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getName(): string |
100
|
|
|
{ |
101
|
|
|
return pathinfo( |
102
|
|
|
$this->getFilename($this->getExtension()), |
|
|
|
|
103
|
|
|
PATHINFO_FILENAME |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string|string[] |
109
|
|
|
*/ |
110
|
|
|
public function getExtension() |
111
|
|
|
{ |
112
|
|
|
return pathinfo($this->getFilename(), PATHINFO_EXTENSION); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getSlug(): string |
119
|
|
|
{ |
120
|
|
|
return $this->index.'_'.Str::slug($this->getName()).'.'.$this->getExtension(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve the chunk file disk. |
125
|
|
|
* |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function getDisk(): ?string |
129
|
|
|
{ |
130
|
|
|
return $this->disk; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the chunk file disk. |
135
|
|
|
* |
136
|
|
|
* @param string|null $disk |
137
|
|
|
*/ |
138
|
|
|
public function setDisk($disk = null) |
139
|
|
|
{ |
140
|
|
|
$this->disk = $disk; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isLast(): bool |
147
|
|
|
{ |
148
|
|
|
return $this->last; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param bool $last |
153
|
|
|
*/ |
154
|
|
|
public function setLast(bool $last): void |
155
|
|
|
{ |
156
|
|
|
$this->last = $last; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* If this method is called, when a chunk is turned to array, the file path and real path |
161
|
|
|
* will be omitted. |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function hideFileInfo() |
166
|
|
|
{ |
167
|
|
|
$this->show_file_info = false; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* If this method is called, when a chunk is turned to array, the file path and real path |
174
|
|
|
* will be included. |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function showFileInfo() |
179
|
|
|
{ |
180
|
|
|
$this->show_file_info = true; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function toArray(): array |
189
|
|
|
{ |
190
|
|
|
$extension = $this->getExtension(); |
191
|
|
|
|
192
|
|
|
$data = [ |
193
|
|
|
'name' => $this->getName(), |
194
|
|
|
'extension' => $extension, |
195
|
|
|
'index' => $this->getIndex(), |
196
|
|
|
'last' => $this->isLast(), |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
if ($this->show_file_info) { |
200
|
|
|
$data['file'] = $this->getFilename(); |
201
|
|
|
$data['path'] = $this->getPath(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $data; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function toJson($options = 0): string |
211
|
|
|
{ |
212
|
|
|
return json_encode($this->toArray(), $options); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function toResponse($request) |
219
|
|
|
{ |
220
|
|
|
return $this->toResource() |
221
|
|
|
->toResponse($request); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Transforms the current model into a json resource. |
226
|
|
|
*/ |
227
|
|
|
public function toResource(): JsonResource |
228
|
|
|
{ |
229
|
|
|
/** @var \Illuminate\Http\Resources\Json\JsonResource $resource */ |
230
|
|
|
$resource = config('chunky.resource', ChunkResource::class); |
|
|
|
|
231
|
|
|
|
232
|
|
|
return new $resource($this); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param \Symfony\Component\HttpFoundation\File\File|string $file |
237
|
|
|
* @param int $index |
238
|
|
|
* @param array $options |
239
|
|
|
* |
240
|
|
|
* @return \Jobtech\LaravelChunky\Chunk |
241
|
|
|
*/ |
242
|
|
|
public static function create($file, int $index, $options = []) |
243
|
|
|
{ |
244
|
|
|
return new static($index, $file, Arr::pull($options, 'disk')); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
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