5
5
6
6
use Plan2net \Webp \Service \Configuration ;
7
7
use Plan2net \Webp \Service \Webp as WebpService ;
8
+ use TYPO3 \CMS \Core \Log \LogManager ;
8
9
use TYPO3 \CMS \Core \Resource \Driver \DriverInterface ;
9
10
use TYPO3 \CMS \Core \Resource \File ;
10
11
use TYPO3 \CMS \Core \Resource \ProcessedFile ;
21
22
class Webp
22
23
{
23
24
/**
24
- * Process a file using the specified adapter to create a webp copy.
25
+ * Process a file using the configured adapter to create a webp copy
25
26
*
26
27
* @param FileProcessingService $fileProcessingService
27
- * @param DriverInterface $driver
28
- * @param ProcessedFile $processedFile
29
- * @param File $file
30
- * @param string $taskType
31
- * @param array $configuration
28
+ * @param DriverInterface $driver
29
+ * @param ProcessedFile $processedFile
30
+ * @param File $file
31
+ * @param string $taskType
32
+ * @param array $configuration
32
33
*/
33
34
public function processFile (
34
35
FileProcessingService $ fileProcessingService ,
@@ -60,7 +61,7 @@ public function processFile(
60
61
$ service = GeneralUtility::makeInstance (WebpService::class);
61
62
$ service ->process ($ processedFile , $ processedFileWebp );
62
63
63
- // @todo using shortMD5 results in a very bad checksum,
64
+ // Be aware that using shortMD5 results in a very bad checksum,
64
65
// but TYPO3 CMS core has a limit on this field
65
66
$ processedFileWebp ->updateProperties (
66
67
[
@@ -72,7 +73,16 @@ public function processFile(
72
73
// This will add or update
73
74
$ processedFileRepository ->add ($ processedFileWebp );
74
75
} catch (\Exception $ e ) {
75
- // @todo log
76
+ $ logger = GeneralUtility::makeInstance (LogManager::class)->getLogger (__CLASS__ );
77
+ $ logger ->error (sprintf ('Failed to convert image to webp: %s ' , $ e ->getMessage ()));
78
+ try {
79
+ $ processedFile ->delete (true );
80
+ } catch (\Exception $ e ) {
81
+ $ logger ->error (sprintf ('Failed to remove processed file "%s": %s ' ,
82
+ $ processedFile ->getIdentifier (),
83
+ $ e ->getMessage ()
84
+ ));
85
+ }
76
86
}
77
87
}
78
88
}
@@ -84,26 +94,26 @@ public function processFile(
84
94
*/
85
95
protected function shouldProcess (string $ taskType , ProcessedFile $ processedFile ): bool
86
96
{
87
- $ process = true ;
88
-
89
97
if ($ taskType !== 'Image.CropScaleMask ' ) {
90
- $ process = false ;
98
+ return false ;
91
99
}
92
100
93
101
if (!WebpService::isSupportedMimeType ($ processedFile ->getMimeType ())) {
94
- $ process = false ;
102
+ return false ;
95
103
}
104
+
96
105
// Convert images in any folder or only in the _processed_ folder
97
106
$ convertAllImages = (bool )Configuration::get ('convert_all ' );
98
107
if (!$ convertAllImages && !$ this ->isFileInProcessingFolder ($ processedFile )) {
99
- $ process = false ;
108
+ return false ;
100
109
}
110
+
101
111
// Process files only in a local and writable storage
102
112
if (!$ this ->isStorageLocalAndWritable ($ processedFile )) {
103
- $ process = false ;
113
+ return false ;
104
114
}
105
115
106
- return $ process ;
116
+ return true ;
107
117
}
108
118
109
119
/**
@@ -135,6 +145,10 @@ protected function isFileInProcessingFolder($file): bool
135
145
protected function isStorageLocalAndWritable ($ file ): bool
136
146
{
137
147
$ storage = $ file ->getStorage ();
148
+ // Ignore files in fallback storage (e.g. files from extensions)
149
+ if ($ storage ->getStorageRecord ()['uid ' ] === 0 ) {
150
+ return false ;
151
+ }
138
152
139
153
return $ storage ->getDriverType () === 'Local ' && $ storage ->isWritable ();
140
154
}
0 commit comments