]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
File class no longer depends on MIME
authorMikael Nordfeldth <mmn@hethane.se>
Sat, 8 Mar 2014 02:51:47 +0000 (03:51 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Sat, 8 Mar 2014 02:51:47 +0000 (03:51 +0100)
+ minor tweaks to MediaFile

classes/File.php
lib/mediafile.php
lib/util.php

index db80159d3ba181b80276f8eb5d072942a241f94b..5f03e8bbd3e1771a7205f04ec8facb697785e13b 100644 (file)
@@ -265,20 +265,13 @@ class File extends Managed_DataObject
 
     static function filename($profile, $basename, $mimetype)
     {
-        require_once 'MIME/Type/Extension.php';
-
-        // We have to temporarily disable auto handling of PEAR errors...
-        PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
-
-        $mte = new MIME_Type_Extension();
-        $ext = $mte->getExtension($mimetype);
-        if (PEAR::isError($ext)) {
-            $ext = strtolower(preg_replace('/\W/', '', $mimetype));
+        try {
+            $ext = common_supported_mime_to_ext($mimetype);
+        } catch (Exception $e) {
+            // We don't support this mimetype, but let's guess the extension
+            $ext = substr(strrchr($mimetype, '/'), 1);
         }
 
-        // Restore error handling.
-        PEAR::staticPopErrorHandling();
-
         $nickname = $profile->nickname;
         $datestamp = strftime('%Y%m%dT%H%M%S', time());
         $random = strtolower(common_confirmation_code(32));
index c2da82caa8dcacd3163c27364bacb3e200591f3b..07d5c2dd2614b62fa791511e22e35143f20662fa 100644 (file)
@@ -298,9 +298,11 @@ class MediaFile
 
         // If we didn't match, or it is an unclear match
         if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
-            $type = common_supported_ext_to_mime($originalFilename);
-            if (!empty($type)) {
+            try {
+                $type = common_supported_ext_to_mime($originalFilename);
                 return $type;
+            } catch (Exception $e) {
+                // Extension not found, so $mimetype is our best guess
             }
         }
 
index 5f9c69dde2339d8dbe77b5f054a666294d9c856a..269089db8bbcda45d89ff781324762a17c27b2d0 100644 (file)
@@ -1812,7 +1812,20 @@ function common_supported_ext_to_mime($fileext)
         }
     }
 
-    return false;
+    throw new ServerException('Unsupported file extension');
+}
+
+// Match by our supported mime types
+function common_supported_mime_to_ext($mimetype)
+{
+    $supported = common_config('attachments', 'supported');
+    foreach($supported as $type => $ext) {
+        if ($mimetype === $type) {
+            return $ext;
+        }
+    }
+
+    throw new ServerException('Unsupported MIME type');
 }
 
 // The MIME "media" is the part before the slash (video in video/webm)