]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
[MEDIA][OEMBED] Fixed regression in OEmbed, because it relied on accessing the files...
authorMiguel Dantas <biodantasgs@gmail.com>
Sat, 29 Jun 2019 19:10:20 +0000 (20:10 +0100)
committerDiogo Cordeiro <diogo@fc.up.pt>
Sat, 3 Aug 2019 16:31:42 +0000 (17:31 +0100)
actions/attachment.php
actions/attachment_download.php
actions/attachment_thumbnail.php
actions/attachment_view.php
classes/File.php
lib/attachmentlist.php
lib/attachmentlistitem.php
lib/imagefile.php
plugins/Oembed/OembedPlugin.php

index 0f4153cbcf93cbcba7feb80a53c114bb69aad659..0851f1ee4e440686f37a7391c00a086b96c1e1c5 100644 (file)
@@ -71,7 +71,11 @@ class AttachmentAction extends ManagedAction
         if (!$this->attachment instanceof File) {
             // TRANS: Client error displayed trying to get a non-existing attachment.
             $this->clientError(_('No such attachment.'), 404);
-        } elseif (empty($this->attachment->filename)) {
+        }
+
+        $filename = $this->attachment->getFileOrThumbnailPath();
+
+        if (empty($filename)) {
             $this->clientError(_('Requested local URL for a file that is not stored locally.'), 404);
         }
         return true;
@@ -100,7 +104,7 @@ class AttachmentAction extends ManagedAction
 
     public function showPage()
     {
-        if (empty($this->attachment->filename)) {
+        if (empty($this->attachment->getFileOrThumbnailPath())) {
             // if it's not a local file, gtfo
             common_redirect($this->attachment->getUrl(), 303);
         }
@@ -150,9 +154,10 @@ class AttachmentAction extends ManagedAction
         if (common_config('site', 'use_x_sendfile')) {
             return null;
         }
-        try {
-            return filemtime($this->attachment->getPath());
-        } catch (InvalidFilenameException $e) {
+        $path = $this->attachment->getFileOrThumbnailPath();
+        if (!empty($path)) {
+            return filemtime($path);
+        } else {
             return null;
         }
     }
@@ -168,26 +173,32 @@ class AttachmentAction extends ManagedAction
     function etag()
     {
         if (common_config('site', 'use_x_sendfile')) {
+
             return null;
         }
 
+        $path = $this->attachment->getFileOrThumbnailPath();
+
         $cache = Cache::instance();
         if($cache) {
-            try {
-                $key = Cache::key('attachments:etag:' . $this->attachment->getPath());
-                $etag = $cache->get($key);
-                if($etag === false) {
-                    $etag = crc32(file_get_contents($this->attachment->getPath()));
-                    $cache->set($key,$etag);
-                }
-            } catch (InvalidFilenameException $e) {
+            if (empty($path)) {
                 return null;
             }
+            $key = Cache::key('attachments:etag:' . $path);
+            $etag = $cache->get($key);
+            if($etag === false) {
+                $etag = crc32(file_get_contents($path));
+                $cache->set($key,$etag);
+            }
             return $etag;
         }
 
-        $stat = stat($this->path);
-        return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
+        if (!empty($path)) {
+            $stat = stat($path);
+            return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
+        } else {
+            return null;
+        }
     }
 
     /**
@@ -205,7 +216,7 @@ class AttachmentAction extends ManagedAction
 
             $ret = @readfile($filepath);
 
-            if (ret === false) {
+            if ($ret === false) {
                 common_log(LOG_ERR, "Couldn't read file at {$filepath}.");
             } elseif ($ret !== $filesize) {
                 common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
index 6a42c29350e9fe875fd5e254954fb32db18dd06b..ebd5e5d956808f1cde67648eb703de6893eeb9c3 100644 (file)
@@ -15,9 +15,15 @@ class Attachment_downloadAction extends AttachmentAction
 {
     public function showPage()
     {
-        // Checks file exists or throws FileNotStoredLocallyException
-        $filepath = $this->attachment->getPath();
-        $filesize = $this->attachment->size;
+        // Checks file exists or throws FileNotFoundException
+        $filepath = $this->attachment->getFileOrThumbnailPath();
+        $filesize = $this->attachment->size ?: 0;
+        $mimetype = $this->attachment->getFileOrThumbnailMimetype();
+
+        if (empty($filepath)) {
+            $thiis->clientError(_('No such attachment'), 404);
+        }
+
         $filename = MediaFile::getDisplayName($this->attachment);
 
         // Disable errors, to not mess with the file contents (suppress errors in case access to this
@@ -26,7 +32,7 @@ class Attachment_downloadAction extends AttachmentAction
         @ini_set('display_errors', 0);
 
         header("Content-Description: File Transfer");
-        header("Content-Type: {$this->attachment->mimetype}");
+        header("Content-Type: {$mimetype}");
         header("Content-Disposition: attachment; filename=\"{$filename}\"");
         header('Expires: 0');
         header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
index f0a54883b12528e256666b6815d9f8012a5dc063..e54e99998329f02964020417cdea3b861d5b203d 100644 (file)
@@ -55,31 +55,22 @@ class Attachment_thumbnailAction extends AttachmentAction
 
     public function showPage()
     {
+        // Checks file exists or throws FileNotFoundException
+        $size = $this->attachment->size ?: 0;
+
         // Returns a File_thumbnail object or throws exception if not available
         try {
-            $thumb = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
-            $file = $thumb->getFile();
+            $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
+            $file = $thumbnail->getFile();
         } catch (UseFileAsThumbnailException $e) {
             // With this exception, the file exists locally
             $file = $e->file;
+        } catch(FileNotFoundException $e) {
+            $this->clientError(_('No such attachment'), 404);
         }
 
-        try {
-            if (!empty($thumb)) {
-                $filepath = $thumb->getPath();
-                $size = 0;
-            } elseif ($file->isLocal()) {
-                $filepath = $file->getPath();
-                $size = $file->size;
-            }
-            // XXX PHP: Upgrade to PHP 7.1
-            // FileNotFoundException | InvalidFilenameException
-        } catch (Exception $e) {
-            // We don't have a file to display
-            $this->clientError(_('No such attachment.'), 404);
-            return false;
-        }
-
+        $filepath = $file->getFileOrThumbnailPath();
+        $mimetype = $file->getFileOrThumbnailMimetype();
         $filename = MediaFile::getDisplayName($file);
 
         // Disable errors, to not mess with the file contents (suppress errors in case access to this
@@ -88,7 +79,7 @@ class Attachment_thumbnailAction extends AttachmentAction
         @ini_set('display_errors', 0);
 
         header("Content-Description: File Transfer");
-        header("Content-Type: {$file->mimetype}");
+        header("Content-Type: {$mimetype}");
         header("Content-Disposition: inline; filename=\"{$filename}\"");
         header('Expires: 0');
         header('Content-Transfer-Encoding: binary');
index d75a45cb768065f1f66410fd1f193385ae9f8248..61aba9bd0f9867c8a7353f6edaef65fbdb4e12c0 100644 (file)
@@ -13,9 +13,14 @@ class Attachment_viewAction extends AttachmentAction
 {
     public function showPage()
     {
-        // Checks file exists or throws FileNotStoredLocallyException
-        $filepath = $this->attachment->getPath();
-        $filesize = $this->attachment->size;
+        // Checks file exists or throws FileNotFoundException
+        $filepath = $this->attachment->getFileOrThumbnailPath();
+        $filesize = $this->attachment->size ?: 0;
+        $mimetype = $this->attachment->getFileOrThumbnailMimetype();
+
+        if (empty($filepath)) {
+            $thiis->clientError(_('No such attachment'), 404);
+        }
 
         $filename = MediaFile::getDisplayName($this->attachment);
 
@@ -25,8 +30,8 @@ class Attachment_viewAction extends AttachmentAction
         @ini_set('display_errors', 0);
 
         header("Content-Description: File Transfer");
-        header("Content-Type: {$this->attachment->mimetype}");
-        if (in_array(common_get_mime_media($this->attachment->mimetype), ['image', 'video'])) {
+        header("Content-Type: {$mimetype}");
+        if (in_array(common_get_mime_media($mimetype), ['image', 'video'])) {
             header("Content-Disposition: inline; filename=\"{$filename}\"");
         } else {
             header("Content-Disposition: attachment; filename=\"{$filename}\"");
index 2d27115678e49705e50a8c8c67ed20c4d9176dee..c6e9b40ac719b63b60d88836412f924d1c077439 100644 (file)
@@ -589,6 +589,58 @@ class File extends Managed_DataObject
         return $filepath;
     }
 
+    /**
+     * Returns the path to either a file, or it's thumbnail if the file doesn't exist.
+     * This is useful in case the original file is deleted, or, as is the case for Embed
+     * thumbnails, we only have a thumbnail and not a file
+     * @return string Path
+     * @throws FileNotFoundException
+     * @throws FileNotStoredLocallyException
+     * @throws InvalidFilenameException
+     * @throws ServerException
+     */
+    public function getFileOrThumbnailPath() : string
+    {
+        if (!empty($this->filename)) {
+            $filepath = self::path($this->filename);
+            if (file_exists($filepath)) {
+                return $filepath;
+            } else {
+                throw new FileNotFoundException($filepath);
+            }
+        } else {
+            try {
+                return File_thumbnail::byFile($this)->getPath();
+            } catch (NoResultException $e) {
+                // File not stored locally
+                throw new FileNotStoredLocallyException($this);
+            }
+        }
+    }
+
+    /**
+     * Return the mime type of the thumbnail if we have it, or, if not, of the File
+     * @return string
+     * @throws FileNotFoundException
+     * @throws NoResultException
+     * @throws ServerException
+     * @throws UnsupportedMediaException
+     */
+    public function getFileOrThumbnailMimetype() : string
+    {
+        if (empty($this->filename)) {
+            $filepath = File_thumbnail::byFile($this)->getPath();
+            $info = @getimagesize($filepath);
+            if ($info !== false) {
+                return $info['mime'];
+            } else {
+                throw new UnsupportedMediaException(_("Thumbnail is not an image."));
+            }
+        } else {
+            return $this->mimetype;
+        }
+    }
+
     public function getAttachmentUrl()
     {
         return common_local_url('attachment', array('attachment'=>$this->getID()));
index 0ce19b0b1ed38c66859d34113a18932ba269ce4d..8c52d7a86ea5d2bf66b0773e754e526c8c4d8ffe 100644 (file)
@@ -77,7 +77,7 @@ class AttachmentList extends Widget
        $attachments = $this->notice->attachments();
         foreach ($attachments as $key=>$att) {
             // Remove attachments which are not representable with neither a title nor thumbnail
-            if ($att->getTitle() === null && !$att->hasThumbnail()) {
+            if ($att->getTitle() === _('Untitled attachment') && !$att->hasThumbnail()) {
                 unset($attachments[$key]);
             }
         }
index 390c450088211125f3a8fd1fcc4d33d5c50d93e2..af44f7aada33199b0e14e3e8119556747cb0f3dd 100644 (file)
@@ -146,7 +146,9 @@ class AttachmentListItem extends Widget
                     } else {
                         try {
                             // getUrl(true) because we don't want to hotlink, could be made configurable
-                            $this->out->element('img', ['class'=>'u-photo', 'src'=>$this->attachment->getUrl(true), 'alt' => $this->attachment->getTitle()]);
+                            $this->out->element('img', ['class'=>'u-photo',
+                                                        'src'=>$this->attachment->getUrl(true),
+                                                        'alt' => $this->attachment->getTitle()]);
                         } catch (FileNotStoredLocallyException $e) {
                             $url = $e->file->getUrl(false);
                             $this->out->element('a', ['href'=>$url, 'rel'=>'external'], $url);
index 1bbadd0b244b42e3f810d2c2a5dfe9b5daf0b19b..85747072a5a898c3cd71c8d3135bfdf3b773e859 100644 (file)
@@ -535,10 +535,8 @@ class ImageFile extends MediaFile
             throw new ServerException('No File object attached to this ImageFile object.');
         }
 
-        // File not stored locally, can't generate a thumbnail
-        if (empty($this->fileRecord->filename)) {
-            throw new FileNotStoredLocallyException($this->fileRecord);
-        }
+        // Throws FileNotFoundException or FileNotStoredLocallyException
+        $this->filepath = $this->fileRecord->getFileOrThumbnailPath();
 
         if ($width === null) {
             $width  = common_config('thumbnail', 'width');
@@ -575,7 +573,7 @@ class ImageFile extends MediaFile
             return $thumb;
         }
 
-        $filename = $this->fileRecord->filehash ?: $this->filename;    // Remote files don't have $this->filehash
+        $filename = basename($this->filepath);
         $extension = File::guessMimeExtension($this->mimetype);
         $outname = "thumb-{$this->fileRecord->getID()}-{$width}x{$height}-{$filename}." . $extension;
         $outpath = File_thumbnail::path($outname);
index 93fafe361d663a42ebcded2923ec4dd478154bda..707fe1de8f3742c6ed839bd7e4f4a86cb6447002 100644 (file)
@@ -411,7 +411,7 @@ class OembedPlugin extends Plugin
      * states where it isn't oEmbed data, so it doesn't mess up the event handle
      * for other things hooked into it), or the exception if it fails.
      */
-    public function onCreateFileImageThumbnailSource(File $file, &$imgPath)
+    public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media)
     {
         // If we are on a private node, we won't do any remote calls (just as a precaution until
         // we can configure this from config.php for the private nodes)