- if ($this->Connector->antiDupe($hash)) {
- do {
- if ($this->Connector->CONFIG['FILES_RETRIES'] === 0) {
- throw new Exception('Gave up trying to find an unused name!', 500);
- }
- $NEW_NAME = '';
- for ($i = 0; $i < $this->Connector->CONFIG['NAME_LENGTH']; ++$i) {
- $NEW_NAME .= $this->Connector->CONFIG['ID_CHARSET']
- [mt_rand(0, strlen($this->Connector->CONFIG['ID_CHARSET']))];
- }
- if (!empty($extension)) {
- $NEW_NAME .= '.' . $extension;
- }
- } while ($this->Connector->dbCheckNameExists($NEW_NAME) > 0);
- return $NEW_NAME;
+ do {
+ if ($this->Connector->CONFIG['FILES_RETRIES'] === 0) {
+ $this->Connector->response->error(500, 'Gave up trying to find an unused name!');
+ }
+ $NEW_NAME = '';
+ for ($i = 0; $i < $this->Connector->CONFIG['NAME_LENGTH']; $i++) {
+ $index = rand(0, strlen($this->Connector->CONFIG['ID_CHARSET']) - 1);
+ $NEW_NAME .= $this->Connector->CONFIG['ID_CHARSET'][$index];
+ }
+ if (!empty($extension)) {
+ $NEW_NAME .= '.' . $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];