]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow to check for the mimetype by content only
authorJoas Schilling <coding@schilljs.com>
Thu, 28 Nov 2019 14:24:57 +0000 (15:24 +0100)
committerBackportbot <backportbot-noreply@rullzer.com>
Thu, 12 Dec 2019 22:05:17 +0000 (22:05 +0000)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Files/Type/Detection.php
lib/public/Files/IMimeTypeDetector.php

index 3207562763288e1a2db95507c8a21ed850b7ce16..3db880538a3ecf6366bc174e4707e0e908ada772 100644 (file)
@@ -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);
                }
@@ -242,6 +240,22 @@ class Detection implements IMimeTypeDetector {
                return $mimeType;
        }
 
+       /**
+        * 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
         *
index f66eb4e0b9f5d5d68a22b8b82f788c2fc6fd0fcb..63ac8854f152a8446f94d8c0e85417d7aad34aec 100644 (file)
@@ -39,9 +39,17 @@ 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
         *