|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Core\Resource\Service; |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Context\Context; |
|
19
|
|
|
use TYPO3\CMS\Core\Context\FileProcessingAspect; |
|
20
|
|
|
use TYPO3\CMS\Core\Locking\ResourceMutex; |
|
21
|
|
|
use TYPO3\CMS\Core\Resource\Exception\FileAlreadyProcessedException; |
|
22
|
|
|
use TYPO3\CMS\Core\Resource\ProcessedFile; |
|
23
|
|
|
use TYPO3\CMS\Core\Resource\ProcessedFileRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Disables deferred processing and actually processes a preprocessed processed file |
|
27
|
|
|
*/ |
|
28
|
|
|
class ImageProcessingService |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ProcessedFileRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
private $processedFileRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Context |
|
37
|
|
|
*/ |
|
38
|
|
|
private $context; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ResourceMutex |
|
42
|
|
|
*/ |
|
43
|
|
|
private $locker; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
ProcessedFileRepository $processedFileRepository, |
|
47
|
|
|
Context $context, |
|
48
|
|
|
ResourceMutex $locker |
|
49
|
|
|
) { |
|
50
|
|
|
$this->processedFileRepository = $processedFileRepository; |
|
51
|
|
|
$this->context = $context; |
|
52
|
|
|
$this->locker = $locker; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function process(int $processedFileId): ProcessedFile |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var ProcessedFile $processedFile */ |
|
58
|
|
|
$processedFile = $this->processedFileRepository->findByUid($processedFileId); |
|
59
|
|
|
try { |
|
60
|
|
|
$this->validateProcessedFile($processedFile); |
|
61
|
|
|
$this->locker->acquireLock(self::class, $processedFileId); |
|
62
|
|
|
|
|
63
|
|
|
// Fetch the processed file again, as it might have been processed by another process while waiting for the lock |
|
64
|
|
|
$processedFile = $this->processedFileRepository->findByUid($processedFileId); |
|
|
|
|
|
|
65
|
|
|
$this->validateProcessedFile($processedFile); |
|
66
|
|
|
|
|
67
|
|
|
$this->context->setAspect('fileProcessing', new FileProcessingAspect(false)); |
|
68
|
|
|
$processedFile = $processedFile->getOriginalFile()->process( |
|
69
|
|
|
$processedFile->getTaskIdentifier(), |
|
70
|
|
|
$processedFile->getProcessingConfiguration() |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$this->validateProcessedFile($processedFile); |
|
74
|
|
|
} catch (FileAlreadyProcessedException $e) { |
|
75
|
|
|
$processedFile = $e->getProcessedFile(); |
|
76
|
|
|
} finally { |
|
77
|
|
|
$this->locker->releaseLock(self::class); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $processedFile; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check whether a processed file was already processed |
|
85
|
|
|
* |
|
86
|
|
|
* @param ProcessedFile $processedFile |
|
87
|
|
|
* @throws FileAlreadyProcessedException |
|
88
|
|
|
*/ |
|
89
|
|
|
private function validateProcessedFile(ProcessedFile $processedFile): void |
|
90
|
|
|
{ |
|
91
|
|
|
if ($processedFile->isProcessed()) { |
|
92
|
|
|
throw new FileAlreadyProcessedException($processedFile, 1599395651); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|