diff options
author | Joas Schilling <coding@schilljs.com> | 2019-11-28 15:24:57 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2019-12-10 09:10:32 +0100 |
commit | b92ebb928a82df37ee0483861bdc4dbdcb38f816 (patch) | |
tree | 333d4298f9acf9a5f31356ae08a6b5abe8c8c10e /lib/private/Files | |
parent | b78a141b0b003f6de04f16863e0fb67f28658dab (diff) | |
download | nextcloud-server-b92ebb928a82df37ee0483861bdc4dbdcb38f816.tar.gz nextcloud-server-b92ebb928a82df37ee0483861bdc4dbdcb38f816.zip |
Allow to check for the mimetype by content only
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Files')
-rw-r--r-- | lib/private/Files/Type/Detection.php | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php index 8505f59bacc..60d0eec9d9e 100644 --- a/lib/private/Files/Type/Detection.php +++ b/lib/private/Files/Type/Detection.php @@ -213,12 +213,12 @@ class Detection implements IMimeTypeDetector { } /** - * detect mimetype based on both filename and content - * + * detect mimetype only based on the content of file * @param string $path * @return string + * @since 18.0.0 */ - public function detect($path) { + public function detectContent(string $path): string { $this->loadMappings(); if (@is_dir($path)) { @@ -226,9 +226,7 @@ class Detection implements IMimeTypeDetector { return "httpd/unix-directory"; } - $mimeType = $this->detectPath($path); - - if ($mimeType === 'application/octet-stream' and function_exists('finfo_open') + if (function_exists('finfo_open') and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME) ) { $info = @strtolower(finfo_file($finfo, $path)); @@ -240,7 +238,7 @@ class Detection implements IMimeTypeDetector { } $isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://'); - if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) { + if (!$isWrapped and function_exists("mime_content_type")) { // use mime magic extension if available $mimeType = mime_content_type($path); } @@ -264,6 +262,22 @@ class Detection implements IMimeTypeDetector { } /** + * detect mimetype based on both filename and content + * + * @param string $path + * @return string + */ + public function detect($path) { + $mimeType = $this->detectPath($path); + + if ($mimeType !== 'application/octet-stream') { + return $mimeType; + } + + return $this->detectContent($path); + } + + /** * detect mimetype based on the content of a string * * @param string $data |