]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/mediafile.php
change default timeout setting for HTTPClient
[quix0rs-gnu-social.git] / lib / mediafile.php
index be97b1cc17ea8c6241e00679e6fd2ddeec102a72..803cbe0a4cd6cece1f9d1e83540138d8912ab2b8 100644 (file)
@@ -34,8 +34,6 @@ if (!defined('GNUSOCIAL')) { exit(1); }
 
 class MediaFile
 {
-    protected $scoped   = null;
-
     var $filename      = null;
     var $fileRecord    = null;
     var $fileurl       = null;
@@ -236,7 +234,20 @@ class MediaFile
         try {
             $file = File::getByHash($filehash);
             // If no exception is thrown the file exists locally, so we'll use that and just add redirections.
-            $filename = $file->filename;
+            // but if the _actual_ locally stored file doesn't exist, getPath will throw FileNotFoundException
+            $filename = basename($file->getPath());
+            $mimetype = $file->mimetype;
+
+        } catch (FileNotFoundException $e) {
+            // The file does not exist in our local filesystem, so store this upload.
+
+            if (!move_uploaded_file($_FILES[$param]['tmp_name'], $e->path)) {
+                // TRANS: Client exception thrown when a file upload operation fails because the file could
+                // TRANS: not be moved from the temporary folder to the permanent file location.
+                throw new ClientException(_('File could not be moved to destination directory.'));
+            }
+
+            $filename = basename($file->getPath());
             $mimetype = $file->mimetype;
 
         } catch (NoResultException $e) {
@@ -344,6 +355,7 @@ class MediaFile
         $unclearTypes = array('application/octet-stream',
                               'application/vnd.ms-office',
                               'application/zip',
+                              'text/plain',
                               'text/html',  // Ironically, Wikimedia Commons' SVG_logo.svg is identified as text/html
                               // TODO: for XML we could do better content-based sniffing too
                               'text/xml');
@@ -353,10 +365,12 @@ class MediaFile
         // If we didn't match, or it is an unclear match
         if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
             try {
-                $type = common_supported_ext_to_mime($originalFilename);
+                $type = common_supported_filename_to_mime($originalFilename);
                 return $type;
+            } catch (UnknownExtensionMimeException $e) {
+                // FIXME: I think we should keep the file extension here (supported should be === true here)
             } catch (Exception $e) {
-                // Extension not found, so $mimetype is our best guess
+                // Extension parsed but no connected mimetype, so $mimetype is our best guess
             }
         }