From 85f7e114b02e2badc08fd410f20cb33bebc25dad Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 11 May 2026 13:38:17 +0900 Subject: [PATCH] Add throw exception to get mime info if there is no mime type found Instead of returning empty --- .phive/phars.xml | 2 +- www/admin/class_test.file.php | 5 +++++ www/lib/CoreLibs/Check/File.php | 12 +++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.phive/phars.xml b/.phive/phars.xml index 614d987e..f71aeb33 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -3,7 +3,7 @@ - + diff --git a/www/admin/class_test.file.php b/www/admin/class_test.file.php index 890adad6..52050903 100644 --- a/www/admin/class_test.file.php +++ b/www/admin/class_test.file.php @@ -38,6 +38,11 @@ print "GETFILENAMEENDING: $file: " . File::getFilenameEnding($file) . "
"; $file = getcwd() . DIRECTORY_SEPARATOR . 'class_test.file.php'; print "GETLINESFROMFILE: $file: " . File::getLinesFromFile($file) . "
"; print "MIMEINFO: $file: " . File::getMimeType($file) . "
"; +try { + print "MIMEINFO: $file: " . File::getMimeType("does_not_exists.txt") . "
"; +} catch (\Exception $e) { + print "Error: " . get_class($e) . ": " . $e->getMessage() . "
"; +} print ""; diff --git a/www/lib/CoreLibs/Check/File.php b/www/lib/CoreLibs/Check/File.php index c8bdc531..a3f42445 100644 --- a/www/lib/CoreLibs/Check/File.php +++ b/www/lib/CoreLibs/Check/File.php @@ -58,15 +58,21 @@ class File * else returns '' for any other finfo read problem * * @param string $read_file File to read, relative or absolute path - * @return string + * @return string mime type + * @throws \UnexpectedValueException if file cannot be read or is not a file + * @throws \RangeException if we cannot get a mime type and throw exception is on */ public static function getMimeType(string $read_file): string { $finfo = new \finfo(FILEINFO_MIME_TYPE); - if (!is_file($read_file)) { + if (!is_file($read_file) || !is_readable($read_file)) { throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file); } - return $finfo->file($read_file) ?: ''; + $mime_type = $finfo->file($read_file); + if ($mime_type === false) { + throw new \RangeException('[getMimeType] Cannot get mime type for: ' . $read_file); + } + return $mime_type; } }