- /**
- * Generates a random string of characters, checks if it exists in the database,
- * and if it does, it generates another one
- *
- * @param $extension string The file extension.
- *
- * @return string A string
- * @throws \Exception
- */
- public function generateName(string $extension): string
- {
- do {
- if ($this->Connector->CONFIG['FILES_RETRIES'] === 0) {
- throw new Exception('Gave up trying to find an unused name!', 500);
- }
- $NEW_NAME = '';
- $count = strlen($this->Connector->CONFIG['ID_CHARSET']);
- while ($this->Connector->CONFIG['NAME_LENGTH']--) {
- $NEW_NAME .= $this->Connector->CONFIG['ID_CHARSET'][mt_rand(0, $count - 1)];
- }
- if (!empty($extension)) {
- $NEW_NAME .= '.' . $extension;
+ 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.');