@@ -30,6 +30,13 @@ abstract class Bitmap extends ProviderV2 {
3030 */
3131 abstract protected function getAllowedMimeTypes (): string ;
3232
33+ /**
34+ * @return list<string>
35+ */
36+ abstract protected function getMagicStrings (): array ;
37+
38+ abstract protected function getImagickFormatHint (): string ;
39+
3340 /**
3441 * {@inheritDoc}
3542 */
@@ -62,7 +69,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
6269 //new bitmap image object
6370 $ image = new \OCP \Image ();
6471 $ image ->loadFromData ((string )$ bp );
65- //check if image object is valid
72+ // Check if image object is valid
6673 return $ image ->valid () ? $ image : null ;
6774 }
6875
@@ -85,15 +92,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8592 private function getResizedPreview ($ tmpPath , $ maxX , $ maxY ) {
8693 $ bp = new Imagick ();
8794
95+ if (!$ this ->isMagicStringSupported ($ tmpPath )) {
96+ throw new \Exception ('Invalid image type: magic string not recognized ' );
97+ }
98+
8899 // Validate mime type
89- $ bp ->pingImage ($ tmpPath . '[0] ' );
100+ $ bp ->pingImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
90101 $ mimeType = $ bp ->getImageMimeType ();
91102 if (!preg_match ($ this ->getAllowedMimeTypes (), $ mimeType )) {
92103 throw new \Exception ('File mime type does not match the preview provider: ' . $ mimeType );
93104 }
94105
95106 // Layer 0 contains either the bitmap or a flat representation of all vector layers
96- $ bp ->readImage ($ tmpPath . '[0] ' );
107+ $ bp ->readImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
97108
98109 $ bp = $ this ->resize ($ bp , $ maxX , $ maxY );
99110
@@ -102,6 +113,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
102113 return $ bp ;
103114 }
104115
116+ private function isMagicStringSupported (string $ filepath ): bool {
117+ $ signatures = $ this ->getMagicStrings ();
118+ if (empty ($ signatures )) {
119+ return true ;
120+ }
121+ $ length = array_reduce ($ signatures , static fn (int $ carry , string $ signature ) => max ($ carry , strlen ($ signature )), 0 );
122+ $ firstBytes = file_get_contents ($ filepath , false , null , 0 , $ length );
123+ foreach ($ signatures as $ signature ) {
124+ if (str_starts_with ($ firstBytes , $ signature )) {
125+ return true ;
126+ }
127+ }
128+
129+ return false ;
130+ }
131+
105132 /**
106133 * Returns a resized \Imagick object
107134 *
0 commit comments