]> jfr.im git - uguu.git/commitdiff
add extension determination when its not provided
authorJohn Runyon <redacted>
Wed, 3 May 2023 11:13:54 +0000 (06:13 -0500)
committerJohn Runyon <redacted>
Wed, 3 May 2023 11:30:53 +0000 (06:30 -0500)
src/Classes/Upload.php

index e568d50a435172e43abe102f82b6c1ea8cd33f88..8821dbc0028cd7620145b55b7cb6a1f135e64bfc 100644 (file)
             $extension = explode('.', $file['name']);
             $dotCount = substr_count($file['name'], '.');
             return match ($dotCount) {
-                0 => null,
+                0 => $this->lookupExtension($file['type']),
                 1 => end($extension),
                 2 => $this->doubleDotExtension($extension),
                 default => end($extension)
             } while ($this->Connector->dbCheckNameExists($NEW_NAME));
             return $NEW_NAME;
         }
+
+        private function lookupExtension(string $mimetype):string
+        {
+            $types = [
+                'image/gif' => 'gif',
+                'image/jpeg' => 'jpg',
+                'image/avif' => 'avif',
+                'image/png' => 'png',
+                'image/tiff' => 'tiff',
+                'image/vnd.wap.wbmp' => 'wbmp',
+                'image/webp' => 'webp',
+                'image/x-icon' => 'ico',
+                'image/x-jng' => 'jng',
+                'image/x-ms-bmp' => 'bmp',
+                'application/pdf' => 'pdf',
+                'application/postscript' => 'ps',
+                'application/x-7z-compressed' => '7z',
+                'application/zip' => 'zip',
+                'audio/midi' => 'mid',
+                'audio/mpeg' => 'mp3',
+                'audio/ogg' => 'ogg',
+                'audio/x-m4a' => 'm4a',
+                'audio/x-realaudio' => 'ra',
+                'video/3gpp' => '3gpp',
+                'video/mp2t' => 'ts',
+                'video/mp4' => 'mp4',
+                'video/mpeg' => 'mpeg',
+                'video/quicktime' => 'mov',
+                'video/webm' => 'webm',
+                'video/x-flv' => 'flv',
+                'video/x-m4v' => 'm4v',
+                'video/x-mng' => 'mng',
+                'video/x-ms-asf' => 'asx',
+                'video/x-ms-wmv' => 'wmv',
+                'video/x-msvideo' => 'avi',
+            ];
+            if (isset($types[$mimetype])) {
+                return $types[$mimetype];
+            } else {
+                $this->Connector->response->error(400, 'Unknown MIME type. Add a file extension to your filename.');
+            }
+        }
     }