-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
BaseMediaFile.php
373 lines (342 loc) · 10.3 KB
/
BaseMediaFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
namespace App\Image\Files;
use App\Contracts\Image\MediaFile;
use App\Contracts\Image\StreamStats;
use App\Exceptions\MediaFileOperationException;
use App\Exceptions\MediaFileUnsupportedException;
use App\Models\Configs;
/**
* Class `MediaFile` provides the common interface of all file-like classes.
*
* This class abstracts from the differences of files which are provided
* through a Flysystem adapter and files outside Flysystem.
* In particular, this abstraction provides a unified copy-mechanism
* between different {@link BinaryBlob}using streams.
*
* This stream-based approach is the same which is also used by
* {@link \Illuminate\Http\UploadedFile::storeAs()} under the hood and avoids certain problems
* which are may be caused by PHP method like `rename`, `move` or `copy`.
* Firstly, these methods need a file path and thus do not work, if a file
* resides on a Flysystem disk for which PHP has no native handler (e.g.
* AWS S3 storage).
* Secondly, `rename` struggles with filesystem permissions and ownership, if
* the file is moved within the same path namespace but across mount points.
* Copying via streams avoids issues like
* [LycheeOrg/Lychee#1198](https://github.com/LycheeOrg/Lychee/issues/1198).
*/
abstract class BaseMediaFile extends AbstractBinaryBlob implements MediaFile
{
public const SUPPORTED_PHP_EXIF_IMAGE_TYPES = [
IMAGETYPE_GIF,
IMAGETYPE_JPEG,
IMAGETYPE_PNG,
IMAGETYPE_WEBP,
];
public const SUPPORTED_IMAGE_FILE_EXTENSIONS = [
'.jpg',
'.jpeg',
'.png',
'.gif',
'.webp',
];
public const SUPPORTED_VIDEO_FILE_EXTENSIONS = [
'.avi',
'.m4v',
'.mov',
'.mp4',
'.mpg',
'.ogv',
'.webm',
'.wmv',
];
public const SUPPORTED_IMAGE_MIME_TYPES = [
'image/gif',
'image/jpeg',
'image/png',
'image/webp',
];
public const SUPPORTED_VIDEO_MIME_TYPES = [
'video/mp4',
'video/mpeg',
'image/x-tga', // mpg; will be corrected by the metadata extractor
'video/ogg',
'video/webm',
'video/quicktime',
'video/x-ms-asf', // wmv file
'video/x-ms-wmv', // wmv file
'video/x-msvideo', // Avi
'video/x-m4v', // Avi
'application/octet-stream', // Some mp4 files; will be corrected by the metadata extractor
];
public const MIME_TYPES_TO_FILE_EXTENSIONS = [
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
'image/png' => '.png',
'image/webp' => '.webp',
'video/mp4' => '.mp4',
'video/mpeg' => '.mpg',
'image/x-tga' => '.mpg',
'video/ogg' => '.ogv',
'video/webm' => '.webm',
'video/quicktime' => '.mov',
'video/x-ms-asf' => '.wmv',
'video/x-ms-wmv' => '.wmv',
'video/x-msvideo' => '.avi',
'video/x-m4v' => '.avi',
'application/octet-stream' => '.mp4',
];
/** @var string[] the accepted raw file extensions minus supported extensions */
private static array $cachedAcceptedRawFileExtensions = [];
/**
* Writes the content of the provided stream into the file.
*
* Any previous content of the file is overwritten.
* The new content is buffered in memory and may not be synced to disk
* until {@link MediaFile::close()} is called.
* If you want to be sure, that the content is really written to disk,
* explicitly call {@link MediaFile::close()}.
* The freshly written content can immediately be read back via
* {@link MediaFile::read} without closing the file in between.
*
* @param resource $stream the input stream which provides the input to write
* @param bool $collectStatistics if true, the method returns statistics about the stream
*
* @return ?StreamStats optional statistics about the stream, if requested
*
* @throws MediaFileOperationException
*/
abstract public function write($stream, bool $collectStatistics = false): ?StreamStats;
/**
* Deletes the file.
*
* In case the file does not exist, the method is a silent no-op.
*
* @return void
*
* @throws MediaFileOperationException
*/
abstract public function delete(): void;
/**
* Moves the file to the new location efficiently.
*
* Basically the file is renamed; however, this kind of "renaming" also
* may change the path of the file.
* Note, that the path is interpreted relative to the "mount" point of
* the underlying filesystem implementation.
*
* @param string $newPath
*
* @return void
*
* @throws MediaFileOperationException
*/
abstract public function move(string $newPath): void;
/** Checks if the file exists.
*
* @return bool true, if the file exists
*/
abstract public function exists(): bool;
/**
* Returns the time of last modification as UNIX timestamp.
*
* @return int the time of last modification since epoch
*
* @throws MediaFileOperationException
*/
abstract public function lastModified(): int;
/**
* Returns the size of the file in bytes.
*
* @return int the file size in bytes
*
* @throws MediaFileOperationException
*/
abstract public function getFilesize(): int;
/**
* Returns the extension of the file incl. a preceding dot.
*
* @return string
*/
abstract public function getExtension(): string;
/**
* Returns the original extension of the file incl. the preceding dot.
*
* Normally, the original extension equals the extension.
* However, for temporary copies of downloaded or uploaded files the
* original extension is the extension as used by the source while the
* actual, physical extension is typically random.
*
* @return string
*/
public function getOriginalExtension(): string
{
return $this->getExtension();
}
/**
* Returns the basename of the file.
*
* The basename of a file is the name of the file without any
* preceding path and without a file extension.
* For example, the basename of the file `/path/to/my-image.jpg` is
* `my-image`.
* Note, this terminology conflicts how the term "basename" is used in
* the PHP documentation.
*
* @return string
*/
abstract public function getBasename(): string;
/**
* Returns the original basename of the file.
*
* Normally, the original basename equals the basename.
* However, for temporary copies of downloaded or uploaded files the
* original basename is the basename as used by the source while the
* actual, physical basename is typically random.
*
* @return string
*/
public function getOriginalBasename(): string
{
return $this->getBasename();
}
/**
* Checks if the given MIME type designates a supported image type.
*
* @param string $mimeType the MIME type
*
* @return bool
*/
public static function isSupportedImageMimeType(string $mimeType): bool
{
return in_array($mimeType, self::SUPPORTED_IMAGE_MIME_TYPES, true);
}
/**
* Checks if the given MIME type designates a supported video type.
*
* @param string $mimeType the MIME type
*
* @return bool
*/
public static function isSupportedVideoMimeType(string $mimeType): bool
{
return in_array($mimeType, self::SUPPORTED_VIDEO_MIME_TYPES, true);
}
/**
* Checks if the given file extension is a supported image extension.
*
* @param string $extension the file extension
*
* @return bool
*/
public static function isSupportedImageFileExtension(string $extension): bool
{
return in_array(strtolower($extension), self::SUPPORTED_IMAGE_FILE_EXTENSIONS, true);
}
/**
* Checks if the given file extension is a supported image extension.
*
* @param string $extension the file extension
*
* @return bool
*/
public static function isSupportedVideoFileExtension(string $extension): bool
{
return in_array(strtolower($extension), self::SUPPORTED_VIDEO_FILE_EXTENSIONS, true);
}
/**
* Checks if the given file extension is supported.
*
* @param string $extension the file extension
*
* @return bool
*/
public static function isSupportedFileExtension(string $extension): bool
{
return
self::isSupportedImageFileExtension($extension) ||
self::isSupportedVideoFileExtension($extension);
}
/**
* Returns {@link MediaFile::$cachedAcceptedRawFileExtensions} and creates it, if necessary.
*
* @return string[]
*/
protected static function getSanitizedAcceptedRawFileExtensions(): array
{
if (count(self::$cachedAcceptedRawFileExtensions) === 0) {
$tmp = explode('|', strtolower(Configs::getValueAsString('raw_formats')));
// Explode may return `false` on error
// Our supported file extensions always take precedence over any
// custom configured extension
self::$cachedAcceptedRawFileExtensions = array_diff($tmp, self::SUPPORTED_IMAGE_FILE_EXTENSIONS, self::SUPPORTED_VIDEO_FILE_EXTENSIONS);
}
return self::$cachedAcceptedRawFileExtensions;
}
/**
* Checks if the given extension is accepted as raw.
*
* @param string $extension the file extension
*
* @return bool
*/
public static function isAcceptedRawFileExtension(string $extension): bool
{
return in_array(
strtolower($extension),
self::getSanitizedAcceptedRawFileExtensions(),
true
);
}
/**
* Check if the given extension is supported or accepted.
*
* @param string $extension the file extension
*
* @return bool
*/
public static function isSupportedOrAcceptedFileExtension(string $extension): bool
{
return
self::isSupportedFileExtension($extension) ||
self::isAcceptedRawFileExtension($extension);
}
/**
* Asserts that the given extension is supported or accepted.
*
* @param string $extension the file extension
*
* @return void
*
* @throws MediaFileUnsupportedException
*/
public static function assertIsSupportedOrAcceptedFileExtension(string $extension): void
{
if (!self::isSupportedOrAcceptedFileExtension($extension)) {
throw new MediaFileUnsupportedException(MediaFileUnsupportedException::DEFAULT_MESSAGE . ' (bad extension: ' . $extension . ')');
}
}
/**
* Check if the given mimetype is supported or accepted.
*
* @param ?string $mimeType the file mimetype
*
* @return bool
*/
public static function isSupportedMimeType(?string $mimeType): bool
{
return
self::isSupportedImageMimeType($mimeType) ||
self::isSupportedVideoMimeType($mimeType);
}
/**
* Returns the default file extension for the given MIME type or an empty string if there is no default extension.
*
* @param string $mimeType a MIME type
*
* @return string the default file extension for the given MIME type
*/
public static function getDefaultFileExtensionForMimeType(string $mimeType): string
{
return self::MIME_TYPES_TO_FILE_EXTENSIONS[strtolower($mimeType)] ?? '';
}
}