-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathFileProcessor.class.php
More file actions
497 lines (414 loc) · 16.4 KB
/
FileProcessor.class.php
File metadata and controls
497 lines (414 loc) · 16.4 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
namespace wcf\system\file\processor;
use wcf\data\file\File;
use wcf\data\file\FileEditor;
use wcf\data\file\thumbnail\FileThumbnailEditor;
use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\event\file\GenerateThumbnail;
use wcf\event\file\GenerateWebpVariant;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\event\EventHandler;
use wcf\system\exception\SystemException;
use wcf\system\file\command\ReplaceFileSource;
use wcf\system\file\command\ReplaceWithWebpVariant;
use wcf\system\file\processor\exception\DamagedImage;
use wcf\system\image\adapter\exception\ImageNotProcessable;
use wcf\system\image\adapter\exception\ImageNotReadable;
use wcf\system\image\ImageHandler;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\ExifUtil;
use wcf\util\FileUtil;
use wcf\util\JSON;
use wcf\util\StringUtil;
use function wcf\functions\exception\logThrowable;
/**
* @author Alexander Ebert
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
* @phpstan-type Context array<string, mixed>
*/
final class FileProcessor extends SingletonFactory
{
public const MAXIMUM_NUMBER_OF_CHUNKS = 255;
public const MAXIMUM_CHUNK_SIZE = 99_000_000;
/**
* @var array<string, ObjectType>
*/
private array $objectTypes;
#[\Override]
public function init(): void
{
$this->objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.file');
}
public function getProcessorByName(string $objectType): ?IFileProcessor
{
return $this->getObjectType($objectType)?->getProcessor();
}
public function getProcessorById(?int $objectTypeID): ?IFileProcessor
{
if ($objectTypeID === null) {
return null;
}
foreach ($this->objectTypes as $objectType) {
if ($objectType->objectTypeID === $objectTypeID) {
return $objectType->getProcessor();
}
}
return null;
}
public function getObjectType(string $objectType): ?ObjectType
{
return $this->objectTypes[$objectType] ?? null;
}
/**
* @param Context $context
*/
public function getHtmlElement(IFileProcessor $fileProcessor, array $context): string
{
$allowedFileExtensions = $fileProcessor->getAllowedFileExtensions($context);
if (\in_array('*', $allowedFileExtensions)) {
$allowedFileExtensions = '';
} else {
// The `accept` attribute of `input[type="file"]` is a bit weird and
// only validates against the string to the right of the last
// period. This means an extension `.tar.gz` can never match.
$allowedFileExtensions = \array_unique(
\array_map(
static fn(string $fileExtension) => \preg_replace('~.*?([^.]+)$~', '\\1', $fileExtension),
$allowedFileExtensions,
),
);
$allowedFileExtensions = \implode(
',',
\array_map(
static fn(string $fileExtension) => ".{$fileExtension}",
$allowedFileExtensions
)
);
}
$maximumCount = $fileProcessor->getMaximumCount($context);
if ($maximumCount === null) {
$maximumCount = -1;
}
$maximumSize = $fileProcessor->getMaximumSize($context);
if ($maximumSize === null) {
$maximumSize = -1;
}
$cropperConfiguration = $fileProcessor->getImageCropperConfiguration();
return \sprintf(
<<<'HTML'
<woltlab-core-file-upload
data-object-type="%s"
data-context="%s"
data-file-extensions="%s"
data-resize-configuration="%s"
%s
data-maximum-count="%d"
data-maximum-size="%d"
></woltlab-core-file-upload>
HTML,
StringUtil::encodeHTML($fileProcessor->getObjectTypeName()),
StringUtil::encodeHTML(JSON::encode($context)),
StringUtil::encodeHTML($allowedFileExtensions),
StringUtil::encodeHTML(JSON::encode($fileProcessor->getResizeConfiguration())),
$cropperConfiguration === null ? ''
: 'data-cropper-configuration="' . StringUtil::encodeHTML(JSON::encode($cropperConfiguration)) . '"',
$maximumCount,
$maximumSize,
);
}
/**
* @param Context $context
*/
public function canAdopt(IFileProcessor $fileProcessor, File $file, array $context): bool
{
$objectType = $this->getObjectType($fileProcessor->getObjectTypeName());
if ($objectType->objectTypeID !== $file->objectTypeID) {
return false;
}
return $fileProcessor->canAdopt($file, $context);
}
public function generateWebpVariant(File $file): File
{
$canGenerateThumbnail = match ($file->mimeType) {
'image/jpeg', 'image/png' => true,
default => false,
};
if (!$canGenerateThumbnail) {
if ($file->fileHashWebp !== null) {
(new FileEditor($file))->update([
'fileHashWebp' => null,
]);
}
return $file;
}
if ($file->fileHashWebp !== null) {
$pathname = $file->getPathnameWebp();
if (\file_exists($pathname) && \hash_file('sha256', $pathname) === $file->fileHashWebp) {
return $file;
}
}
$event = new GenerateWebpVariant($file);
EventHandler::getInstance()->fire($event);
if ($event->sourceIsMarkedAsDamaged()) {
throw new DamagedImage($file->fileID);
}
$filename = $event->getPathname();
if ($filename === null) {
$imageAdapter = ImageHandler::getInstance()->getAdapter();
if (!$imageAdapter->checkMemoryLimit($file->width, $file->height, $file->mimeType)) {
return $file;
}
try {
$imageAdapter->loadSingleFrameFromFile($file->getPathname());
} catch (SystemException | ImageNotReadable) {
throw new DamagedImage($file->fileID);
} catch (ImageNotProcessable $e) {
logThrowable($e);
return $file;
}
$filename = FileUtil::getTemporaryFilename(extension: 'webp');
try {
$imageAdapter->saveImageAs($imageAdapter->getImage(), $filename, 'webp', 80);
} catch (\Throwable $e) {
// Ignore any errors trying to save the file unless in debug mode.
if (\ENABLE_DEBUG_MODE) {
throw $e;
}
return $file;
}
}
(new FileEditor($file))->update([
'fileHashWebp' => \hash_file('sha256', $filename),
]);
$file = new File($file->fileID);
$pathname = $file->getPathnameWebp();
\assert($pathname !== null);
\rename($filename, $pathname);
return $file;
}
public function generateThumbnails(File $file): void
{
if (!$file->isImage()) {
return;
}
$processor = $file->getProcessor();
if ($processor === null) {
return;
}
$formats = $processor->getThumbnailFormats();
if ($formats === []) {
return;
}
$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add("fileID = ?", [$file->fileID]);
$thumbnailList->readObjects();
$existingThumbnails = [];
foreach ($thumbnailList as $thumbnail) {
$existingThumbnails[$thumbnail->identifier] = $thumbnail;
}
$imageAdapter = null;
foreach ($formats as $format) {
$existingThumbnail = $existingThumbnails[$format->identifier] ?? null;
// Check if we the source image is larger than the dimensions of the
// requested thumbnails.
if ($format->width > $file->width && $format->height > $file->height) {
// There currently is a thumbnail for this format but the
// conditions for its existence are no longer met.
if ($existingThumbnail !== null) {
FileThumbnailEditor::deleteAll([$existingThumbnail->thumbnailID]);
}
continue;
}
if ($existingThumbnail !== null) {
if ($existingThumbnail->needsRebuild($format)) {
// There currently is a thumbnail but it is no longer valid.
FileThumbnailEditor::deleteAll([$existingThumbnail->thumbnailID]);
} else {
// This thumbnail is still fine.
continue;
}
}
$event = new GenerateThumbnail($file, $format);
EventHandler::getInstance()->fire($event);
if ($event->sourceIsMarkedAsDamaged()) {
throw new DamagedImage($file->fileID);
}
$filename = $event->getPathname();
if ($filename === null) {
if ($imageAdapter === null) {
$imageAdapter = ImageHandler::getInstance()->getAdapter();
if (!$imageAdapter->checkMemoryLimit($file->width, $file->height, $file->mimeType)) {
return;
}
try {
$imageAdapter->loadSingleFrameFromFile($file->getPathname());
} catch (SystemException | ImageNotReadable $e) {
throw new DamagedImage($file->fileID, $e);
} catch (ImageNotProcessable $e) {
logThrowable($e);
return;
}
}
try {
$image = $imageAdapter->createThumbnail($format->width, $format->height, $format->retainDimensions);
} catch (\Throwable $e) {
logThrowable($e);
continue;
}
$filename = FileUtil::getTemporaryFilename(extension: 'webp');
$imageAdapter->saveImageAs($image, $filename, 'webp', 80);
}
$fileThumbnail = FileThumbnailEditor::createFromTemporaryFile($file, $format, $filename);
$processor->adoptThumbnail($fileThumbnail);
}
}
/**
* @param File[] $files
*/
public function delete(array $files): void
{
$fileIDs = \array_column($files, 'fileID');
$conditions = new PreparedStatementConditionBuilder();
$conditions->add('fileID IN (?)', [$fileIDs]);
$sql = "SELECT thumbnailID
FROM wcf1_file_thumbnail
{$conditions}";
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
$thumbnailIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
foreach ($this->objectTypes as $objectType) {
$objectType->getProcessor()->delete($fileIDs, $thumbnailIDs);
}
}
/**
* @param Context $context
*/
public function hasReachedUploadLimit(IFileProcessor $fileProcessor, array $context): bool
{
$isReplacement = $context['__replace'] ?? false;
if ($isReplacement) {
return false;
}
$existingFiles = $fileProcessor->countExistingFiles($context);
if ($existingFiles === null) {
return false;
}
$maximumCount = $fileProcessor->getMaximumCount($context);
$sql = "SELECT COUNT(*)
FROM wcf1_file_temporary
WHERE objectTypeID = ?
AND context = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([
$this->getObjectType($fileProcessor->getObjectTypeName())->objectTypeID,
JSON::encode($context),
]);
$numberOfTemporaryFiles = $statement->fetchSingleColumn();
if ($existingFiles + $numberOfTemporaryFiles >= $maximumCount) {
return true;
}
return false;
}
public function getOptimalChunkSize(): int
{
$postMaxSize = \ini_parse_quantity(\ini_get('post_max_size'));
if ($postMaxSize === 0) {
// Disabling it is fishy, assume a more reasonable limit of 99 MB.
return self::MAXIMUM_CHUNK_SIZE;
}
// 99 MB is a reasonable upper limit that also plays nice with services
// like Cloudflare that usually come with a 100 MB request limit.
return \min(self::MAXIMUM_CHUNK_SIZE, $postMaxSize);
}
public function getMaximumFileSize(): int
{
$maximumFileSize = $this->getOptimalChunkSize() * self::MAXIMUM_NUMBER_OF_CHUNKS;
if (\defined('ENTERPRISE_MODE_MAXIMUM_FILE_SIZE')) {
$maximumFileSize = \min($maximumFileSize, \constant('ENTERPRISE_MODE_MAXIMUM_FILE_SIZE'));
}
return $maximumFileSize;
}
public function copy(File $oldFile, string $objectType): File
{
$objectTypeObj = $this->getObjectType($objectType);
if ($objectTypeObj === null) {
throw new \InvalidArgumentException("The object type '{$objectType}' is invalid.");
}
$newFile = FileEditor::create([
'filename' => $oldFile->filename,
'fileSize' => $oldFile->fileSize,
'fileHash' => $oldFile->fileHash,
'fileExtension' => $oldFile->fileExtension,
'objectTypeID' => $objectTypeObj->objectTypeID,
'mimeType' => $oldFile->mimeType,
'width' => $oldFile->width,
'height' => $oldFile->height,
'fileHashWebp' => $oldFile->fileHashWebp,
]);
\copy($oldFile->getPathname(), $newFile->getPathname());
if ($oldFile->fileHashWebp !== null) {
\copy($oldFile->getPathnameWebp(), $newFile->getPathnameWebp());
}
$this->copyThumbnails($oldFile->fileID, $newFile->fileID);
return $newFile;
}
#[\NoDiscard("as the file itself could change")]
public function convertImageFormat(File $file): File
{
switch (\IMAGE_CONVERT_FORMAT) {
case 'keep':
return $file;
case 'webp':
$command = new ReplaceWithWebpVariant($file);
$newFile = $command();
// The files identity differs if the file has been replaced.
if ($file !== $newFile) {
$processor = $newFile->getProcessor();
$processor?->replacedWithWebpVariant($newFile);
}
return $newFile;
default:
throw new \LogicException("Unreachable");
}
}
#[\NoDiscard("as the file itself could change")]
public function stripExif(File $file): File
{
if (!\IMAGE_STRIP_EXIF) {
return $file;
}
$fileWithoutExif = ExifUtil::getFileWithoutExifData($file->getPathname());
if ($fileWithoutExif === null) {
return $file;
}
$command = new ReplaceFileSource($file, $fileWithoutExif, $file->filename);
$newFile = $command();
return $newFile;
}
private function copyThumbnails(int $oldFileID, int $newFileID): void
{
$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add("fileID = ?", [$oldFileID]);
$thumbnailList->readObjects();
foreach ($thumbnailList as $oldThumbnail) {
$newThumbnail = FileThumbnailEditor::create([
'fileID' => $newFileID,
'identifier' => $oldThumbnail->identifier,
'fileHash' => $oldThumbnail->fileHash,
'fileExtension' => $oldThumbnail->fileExtension,
'width' => $oldThumbnail->width,
'height' => $oldThumbnail->height,
'formatChecksum' => $oldThumbnail->formatChecksum,
]);
\copy(
$oldThumbnail->getPath() . $oldThumbnail->getSourceFilename(),
$newThumbnail->getPath() . $newThumbnail->getSourceFilename(),
);
}
}
}