1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Upload |
5
|
|
|
* |
6
|
|
|
* Platine Upload provides a flexible file uploads with extensible |
7
|
|
|
* validation and storage strategies. |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Upload |
12
|
|
|
* |
13
|
|
|
* @author Josh Lockhart <[email protected]> |
14
|
|
|
* @copyright 2012 Josh Lockhart |
15
|
|
|
* @link http://www.joshlockhart.com |
16
|
|
|
* @version 2.0.0 |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
19
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
20
|
|
|
* in the Software without restriction, including without limitation the rights |
21
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
22
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
23
|
|
|
* furnished to do so, subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
30
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
31
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
32
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
33
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
34
|
|
|
* SOFTWARE. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @file File.php |
39
|
|
|
* |
40
|
|
|
* The Upload File class |
41
|
|
|
* |
42
|
|
|
* @package Platine\Upload\File |
43
|
|
|
* @author Platine Developers Team |
44
|
|
|
* @copyright Copyright (c) 2020 |
45
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
46
|
|
|
* @link https://www.platine-php.com |
47
|
|
|
* @version 1.0.0 |
48
|
|
|
* @filesource |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
declare(strict_types=1); |
52
|
|
|
|
53
|
|
|
namespace Platine\Upload\File; |
54
|
|
|
|
55
|
|
|
use finfo; |
56
|
|
|
use RuntimeException; |
57
|
|
|
use SplFileInfo; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @class File |
61
|
|
|
* @package Platine\Upload\File |
62
|
|
|
*/ |
63
|
|
|
class File extends SplFileInfo implements FileInterface |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* Factory used to create new instance |
67
|
|
|
* @var callable|null |
68
|
|
|
*/ |
69
|
|
|
protected static $factory = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The file name |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected string $name; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The file extension |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected string $extension; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The file mime type |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected string $mimeType = ''; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The client file name |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
protected string $clientName = ''; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The upload status |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
protected int $error = UPLOAD_ERR_OK; |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create new instance |
104
|
|
|
* @param string $filePath the file absolute path |
105
|
|
|
* @param string $clientName the client filename |
106
|
|
|
* @param string|null $name the desired new name |
107
|
|
|
* @param int $error |
108
|
|
|
*/ |
109
|
|
|
public function __construct( |
110
|
|
|
string $filePath, |
111
|
|
|
string $clientName = '', |
112
|
|
|
?string $name = null, |
113
|
|
|
int $error = UPLOAD_ERR_OK |
114
|
|
|
) { |
115
|
|
|
$this->clientName = $clientName; |
116
|
|
|
$this->error = $error; |
117
|
|
|
$newName = $name === null ? $filePath : $name; |
118
|
|
|
|
119
|
|
|
$this->setName(pathinfo($newName, PATHINFO_FILENAME)); |
|
|
|
|
120
|
|
|
$this->setExtension(pathinfo($newName, PATHINFO_EXTENSION)); |
|
|
|
|
121
|
|
|
|
122
|
|
|
parent::__construct($filePath); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function getExtension(): string |
129
|
|
|
{ |
130
|
|
|
return $this->extension; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function setExtension(string $name): self |
137
|
|
|
{ |
138
|
|
|
$this->extension = strtolower($name); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getFullName(): string |
147
|
|
|
{ |
148
|
|
|
return empty($this->extension) ? |
149
|
|
|
$this->name |
150
|
|
|
: sprintf('%s.%s', $this->name, $this->extension); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function getMD5(): string |
157
|
|
|
{ |
158
|
|
|
$hash = md5_file($this->getPathname()); |
159
|
|
|
return $hash === false ? '' : $hash; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getPathname(): string |
166
|
|
|
{ |
167
|
|
|
return parent::getPathname(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getSize(): int |
174
|
|
|
{ |
175
|
|
|
return parent::getSize(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function getMimeType(): string |
182
|
|
|
{ |
183
|
|
|
if (empty($this->mimeType)) { |
184
|
|
|
$finfo = new finfo(FILEINFO_MIME); |
185
|
|
|
$mimeType = $finfo->file($this->getPathname()); |
186
|
|
|
if ($mimeType !== false) { |
187
|
|
|
$mimetypeParts = preg_split('/\s*[;,]\s*/', $mimeType); |
188
|
|
|
if (is_array($mimetypeParts)) { |
|
|
|
|
189
|
|
|
$this->mimeType = strtolower($mimetypeParts[0]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
unset($finfo); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this->mimeType; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
|
|
public function getName(): string |
202
|
|
|
{ |
203
|
|
|
return $this->name; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
|
|
public function setName(string $name): self |
210
|
|
|
{ |
211
|
|
|
$cleanName = preg_replace('/[^A-Za-z0-9\.]+/', '_', $name); |
212
|
|
|
if ($cleanName !== null) { |
213
|
|
|
$filename = basename($cleanName); |
214
|
|
|
$this->name = $filename; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function getError(): int |
224
|
|
|
{ |
225
|
|
|
return $this->error; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return the client name |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function getClientName(): string |
233
|
|
|
{ |
234
|
|
|
return $this->clientName; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the factory used to create new instance |
240
|
|
|
* @param callable|null $callable |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public static function setFactory(?callable $callable = null): void |
244
|
|
|
{ |
245
|
|
|
static::$factory = $callable; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Create new instance of this class |
250
|
|
|
* @param string $tmpName |
251
|
|
|
* @param string $clientName |
252
|
|
|
* @param string|null $name |
253
|
|
|
* @param int $error |
254
|
|
|
* @return self |
255
|
|
|
*/ |
256
|
|
|
public static function create( |
257
|
|
|
string $tmpName, |
258
|
|
|
string $clientName = '', |
259
|
|
|
?string $name = null, |
260
|
|
|
int $error = UPLOAD_ERR_OK |
261
|
|
|
): self { |
262
|
|
|
if (static::$factory !== null) { |
263
|
|
|
$file = call_user_func_array(static::$factory, [$tmpName, $clientName, $name, $error]); |
264
|
|
|
|
265
|
|
|
if (!$file instanceof File) { |
266
|
|
|
throw new RuntimeException(sprintf( |
267
|
|
|
'The File factory must return an instance of [%s]', |
268
|
|
|
File::class |
269
|
|
|
)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $file; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return new self($tmpName, $clientName, $name, $error); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|