summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/utils/FileUtils.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/application/mystic/forum/utils/FileUtils.php')
-rw-r--r--src/application/mystic/forum/utils/FileUtils.php33
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;
+ }
+}