diff options
author | Jonas Kohl | 2024-09-13 19:57:43 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-09-13 19:57:43 +0200 |
commit | 93817fef3ead7cfd4fcde25ea2bcec02d01310a4 (patch) | |
tree | 60f6ab69bbf2360d7cc518023090b8bf61bfd5b1 /src/application/mystic/forum/utils/FileUtils.php | |
parent | 086e2d2668784469ec114f6e6fd2b3dace3d7c3b (diff) |
A lot of changes again
Diffstat (limited to 'src/application/mystic/forum/utils/FileUtils.php')
-rw-r--r-- | src/application/mystic/forum/utils/FileUtils.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/application/mystic/forum/utils/FileUtils.php b/src/application/mystic/forum/utils/FileUtils.php new file mode 100644 index 0000000..1910f0c --- /dev/null +++ b/src/application/mystic/forum/utils/FileUtils.php @@ -0,0 +1,33 @@ +<?php +declare(strict_types=1); + +namespace mystic\forum\utils; + +final class FileUtils { + use StaticClass; + + private const MIME_FILE = __DIR__ . "/../../../assets/mimetypes"; + public const DEFAULT_MIME_TYPE = "application/octet-stream"; + + private static ?array $extToMime = null; + + private static function ensureMimeMapping(): void { + if (self::$extToMime !== null) + return; + + self::$extToMime = []; + + $hFile = fopen(self::MIME_FILE, "r"); + while (($ln = fgets($hFile)) !== false) { + [$mime, $extensions] = preg_split('/\t+/', $ln, 2); + $extensions = explode(" ", strtolower($extensions)); + foreach ($extensions as $ext) + self::$extToMime[$ext] = $mime; + } + } + + public static function getMimeTypeForExtension(string $extension, string $defaultMimeType = self::DEFAULT_MIME_TYPE): string { + self::ensureMimeMapping(); + return self::$extToMime[strtolower($extension)] ?? $defaultMimeType; + } +} |