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\Response; |
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
|
|
|
} elseif ($this->path instanceof File) { |
88
|
|
|
return $this->path->getBasename($suffix); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return basename($this->path, $suffix); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getName(): string |
98
|
|
|
{ |
99
|
|
|
return pathinfo( |
100
|
|
|
$this->getFilename($this->getExtension()), |
|
|
|
|
101
|
|
|
PATHINFO_FILENAME |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string|string[] |
107
|
|
|
*/ |
108
|
|
|
public function getExtension() |
109
|
|
|
{ |
110
|
|
|
return pathinfo($this->getFilename(), PATHINFO_EXTENSION); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getSlug(): string |
117
|
|
|
{ |
118
|
|
|
return $this->index.'_'.Str::slug($this->getName()).'.'.$this->getExtension(); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve the chunk file disk. |
123
|
|
|
* |
124
|
|
|
* @return string|null |
125
|
|
|
*/ |
126
|
|
|
public function getDisk(): ?string |
127
|
|
|
{ |
128
|
|
|
return $this->disk; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the chunk file disk. |
133
|
|
|
* |
134
|
|
|
* @param string|null $disk |
135
|
|
|
*/ |
136
|
|
|
public function setDisk($disk = null) |
137
|
|
|
{ |
138
|
|
|
$this->disk = $disk; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isLast(): bool |
145
|
|
|
{ |
146
|
|
|
return $this->last; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param bool $last |
151
|
|
|
*/ |
152
|
|
|
public function setLast(bool $last): void |
153
|
|
|
{ |
154
|
|
|
$this->last = $last; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* If this method is called, when a chunk is turned to array, the file path and real path |
159
|
|
|
* will be omitted. |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function hideFileInfo() |
164
|
|
|
{ |
165
|
|
|
$this->show_file_info = false; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* If this method is called, when a chunk is turned to array, the file path and real path |
172
|
|
|
* will be included. |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function showFileInfo() |
177
|
|
|
{ |
178
|
|
|
$this->show_file_info = true; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function toArray(): array |
187
|
|
|
{ |
188
|
|
|
$extension = $this->getExtension(); |
189
|
|
|
|
190
|
|
|
$data = [ |
191
|
|
|
'name' => $this->getName(), |
192
|
|
|
'extension' => $extension, |
193
|
|
|
'index' => $this->getIndex(), |
194
|
|
|
'last' => $this->isLast(), |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
if ($this->show_file_info) { |
198
|
|
|
$data['file'] = $this->getFilename(); |
199
|
|
|
$data['path'] = $this->getPath(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $data; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function toJson($options = 0): string |
209
|
|
|
{ |
210
|
|
|
return json_encode($this->toArray(), $options); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function toResponse($request) |
217
|
|
|
{ |
218
|
|
|
if ($request->wantsJson()) { |
219
|
|
|
return $this->toResource(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return new Response( |
223
|
|
|
$this->toJson(), |
224
|
|
|
Response::HTTP_CREATED |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Transforms the current model into a json resource. |
230
|
|
|
*/ |
231
|
|
|
public function toResource() |
232
|
|
|
{ |
233
|
|
|
/** @var \Illuminate\Http\Resources\Json\JsonResource $resource */ |
234
|
|
|
$resource = config('chunky.resource', ChunkResource::class); |
235
|
|
|
|
236
|
|
|
return new $resource($this); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param \Symfony\Component\HttpFoundation\File\File|string $file |
241
|
|
|
* @param int $index |
242
|
|
|
* @param array $options |
243
|
|
|
* |
244
|
|
|
* @return \Jobtech\LaravelChunky\Chunk |
245
|
|
|
*/ |
246
|
|
|
public static function create($file, int $index, $options = []) |
247
|
|
|
{ |
248
|
|
|
return new static($index, $file, Arr::pull($options, 'disk')); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|