diff options
author | Josh <josh.t.richards@gmail.com> | 2025-07-24 12:42:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-24 12:42:06 -0400 |
commit | 20b174806f4c9a632a9d79d4fc5904911aa3b8a4 (patch) | |
tree | 44cb85a43052fdc22a8c02e103c50203a67f5f7a | |
parent | cb410cd3682e581eb010c0d0830e7e92c4810286 (diff) | |
download | nextcloud-server-jtr-files-detection-refactor-finfo.tar.gz nextcloud-server-jtr-files-detection-refactor-finfo.zip |
refactor(IMimeTypeDetector): streamline finfo usage + misc tidyingjtr-files-detection-refactor-finfo
Signed-off-by: Josh <josh.t.richards@gmail.com>
-rw-r--r-- | lib/private/Files/Type/Detection.php | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php index d5810a90868..6af6ce1a0b1 100644 --- a/lib/private/Files/Type/Detection.php +++ b/lib/private/Files/Type/Detection.php @@ -55,7 +55,8 @@ class Detection implements IMimeTypeDetector { * @param string $mimeType * @param string|null $secureMimeType */ - public function registerType(string $extension, + public function registerType( + string $extension, string $mimeType, ?string $secureMimeType = null): void { // Make sure the extension is a string @@ -217,14 +218,10 @@ class Detection implements IMimeTypeDetector { return 'httpd/unix-directory'; } - if (function_exists('finfo_open') - && function_exists('finfo_file') - && $finfo = finfo_open(FILEINFO_MIME)) { - $info = @finfo_file($finfo, $path); - finfo_close($finfo); - if ($info) { - $info = strtolower($info); - $mimeType = str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info; + if (class_exists(finfo::class)) { + $finfo = new finfo(FILEINFO_MIME_TYPE); + $mimeType = @$finfo->file($path); + if ($mimeType) { $mimeType = $this->getSecureMimeType($mimeType); if ($mimeType !== 'application/octet-stream') { return $mimeType; @@ -240,7 +237,7 @@ class Detection implements IMimeTypeDetector { if (function_exists('mime_content_type')) { // use mime magic extension if available $mimeType = mime_content_type($path); - if ($mimeType !== false) { + if ($mimeType) { $mimeType = $this->getSecureMimeType($mimeType); if ($mimeType !== 'application/octet-stream') { return $mimeType; @@ -258,7 +255,7 @@ class Detection implements IMimeTypeDetector { if ($fp !== false) { $mimeType = fgets($fp); pclose($fp); - if ($mimeType !== false) { + if ($mimeType) { //trim the newline $mimeType = trim($mimeType); $mimeType = $this->getSecureMimeType($mimeType); @@ -293,19 +290,21 @@ class Detection implements IMimeTypeDetector { * @return string */ public function detectString($data): string { - if (function_exists('finfo_open') && function_exists('finfo_file')) { - $finfo = finfo_open(FILEINFO_MIME); - $info = finfo_buffer($finfo, $data); - return str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info; + if (class_exists(finfo::class)) { + $finfo = new finfo(FILEINFO_MIME_TYPE); + $mimeType = $finfo->buffer($data); + if ($mimeType) { + return $mimeType; + } } $tmpFile = \OCP\Server::get(ITempManager::class)->getTemporaryFile(); $fh = fopen($tmpFile, 'wb'); fwrite($fh, $data, 8024); fclose($fh); - $mime = $this->detect($tmpFile); + $mimeType = $this->detect($tmpFile); unset($tmpFile); - return $mime; + return $mimeType; } /** |