diff options
author | Joas Schilling <coding@schilljs.com> | 2019-11-28 15:24:57 +0100 |
---|---|---|
committer | Backportbot <backportbot-noreply@rullzer.com> | 2019-12-12 22:06:06 +0000 |
commit | a52c84b5b8b322bb4d2ed677fd3625a4297153bb (patch) | |
tree | fc032d2482a1c726a528dfd27e6beb10cc9ec36b | |
parent | f3b3df2dd110d74dca9239eff2f421fb60aa4e42 (diff) | |
download | nextcloud-server-a52c84b5b8b322bb4d2ed677fd3625a4297153bb.tar.gz nextcloud-server-a52c84b5b8b322bb4d2ed677fd3625a4297153bb.zip |
Allow to check for the mimetype by content only
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/private/Files/Type/Detection.php | 28 | ||||
-rw-r--r-- | lib/public/Files/IMimeTypeDetector.php | 10 |
2 files changed, 30 insertions, 8 deletions
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php index 32075627632..3db880538a3 100644 --- a/lib/private/Files/Type/Detection.php +++ b/lib/private/Files/Type/Detection.php @@ -192,12 +192,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)) { @@ -205,9 +205,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)); @@ -219,7 +217,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); } @@ -243,6 +241,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 diff --git a/lib/public/Files/IMimeTypeDetector.php b/lib/public/Files/IMimeTypeDetector.php index f66eb4e0b9f..63ac8854f15 100644 --- a/lib/public/Files/IMimeTypeDetector.php +++ b/lib/public/Files/IMimeTypeDetector.php @@ -39,10 +39,18 @@ interface IMimeTypeDetector { * @param string $path * @return string * @since 8.2.0 - **/ + */ public function detectPath($path); /** + * detect mimetype only based on the content of file + * @param string $path + * @return string + * @since 18.0.0 + */ + public function detectContent(string $path): string; + + /** * detect mimetype based on both filename and content * * @param string $path |