From: Miguel Dantas <biodantasgs@gmail.com>
Date: Thu, 27 Jun 2019 01:20:39 +0000 (+0100)
Subject: [CORE] Fixed bug where all thumbnails were using the original file
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=04d1caff78dd3d6771fb2c4be006a91671ceb266;p=quix0rs-gnu-social.git

[CORE] Fixed bug where all thumbnails were using the original file
---

diff --git a/actions/attachment.php b/actions/attachment.php
index 2a967c72b7..04b261506b 100644
--- a/actions/attachment.php
+++ b/actions/attachment.php
@@ -179,18 +179,17 @@ class AttachmentAction extends ManagedAction
     }
 
     /**
-     * Include $this as a file read from $filepath, for viewing and downloading
+     * Include $filepath in the response, for viewing and downloading
      */
-    public function sendFile(string $filepath) {
+    static function sendFile(string $filepath, int $size) {
         if (common_config('site', 'use_x_sendfile')) {
             header('X-Sendfile: ' . $filepath);
         } else {
-            $filesize = $this->attachment->size;
-            // 'if available', it says, so ensure we have it
-            if (empty($filesize)) {
-                $filesize = filesize($filepath);
+            // ensure we have a file size
+            if (empty($size)) {
+                $size = filesize($filepath);
             }
-            header("Content-Length: {$filesize}");
+            header("Content-Length: {$size}");
             // header('Cache-Control: private, no-transform, no-store, must-revalidate');
 
             $ret = @readfile($filepath);
diff --git a/actions/attachment_download.php b/actions/attachment_download.php
index 35c0ecb5d5..b4b01389d8 100644
--- a/actions/attachment_download.php
+++ b/actions/attachment_download.php
@@ -17,7 +17,7 @@ class Attachment_downloadAction extends AttachmentAction
     {
         // Checks file exists or throws FileNotStoredLocallyException
         $filepath = $this->attachment->getPath();
-
+        $filesize = $this->attachment->size;
         $filename = MediaFile::getDisplayName($this->attachment);
 
         // Disable errors, to not mess with the file contents (suppress errors in case access to this
@@ -31,6 +31,6 @@ class Attachment_downloadAction extends AttachmentAction
         header('Expires: 0');
         header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
 
-        $this->sendFile($filepath);
+        parent::sendFile($filepath, $filesize);
     }
 }
diff --git a/actions/attachment_thumbnail.php b/actions/attachment_thumbnail.php
index 85ffb7e5df..f7cc17f347 100644
--- a/actions/attachment_thumbnail.php
+++ b/actions/attachment_thumbnail.php
@@ -57,13 +57,21 @@ class Attachment_thumbnailAction extends AttachmentAction
     {
         // Returns a File_thumbnail object or throws exception if not available
         try {
-            $file = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c)->getFile();
+            $thumb = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
+            $file = $thumb->getFile();
         } catch (UseFileAsThumbnailException $e) {
             // With this exception, the file exists locally
             $file = $e->file;
         }
 
-        $filepath = $this->attachment->getPath();
+        if (!empty($thumb)) {
+            $filepath = $thumb->getPath();
+            $filesize = 0;
+        } else {
+            $filepath = $file->getPath();
+            $filesize = $file->size;
+        }
+
         $filename = MediaFile::getDisplayName($file);
 
         // Disable errors, to not mess with the file contents (suppress errors in case access to this
@@ -76,19 +84,7 @@ class Attachment_thumbnailAction extends AttachmentAction
         header("Content-Disposition: inline; filename=\"{$filename}\"");
         header('Expires: 0');
         header('Content-Transfer-Encoding: binary');
-        $filesize = $this->file->size;
-        // 'if available', it says, so ensure we have it
-        if (empty($filesize)) {
-            $filesize = filesize($this->attachment->filename);
-        }
-        header("Content-Length: {$filesize}");
-        // header('Cache-Control: private, no-transform, no-store, must-revalidate');
 
-        $ret = @readfile($filepath);
-
-        if ($ret === false || $ret !== $filesize) {
-            common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
-                       "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user.");
-        }
+        parent::sendFile($filepath, $filesize);
     }
 }
diff --git a/actions/attachment_view.php b/actions/attachment_view.php
index d6a7c1c50d..71e1e0fd6c 100644
--- a/actions/attachment_view.php
+++ b/actions/attachment_view.php
@@ -15,6 +15,7 @@ class Attachment_viewAction extends AttachmentAction
     {
         // Checks file exists or throws FileNotStoredLocallyException
         $filepath = $this->attachment->getPath();
+        $filesize = $this->attachment->size;
 
         $filename = MediaFile::getDisplayName($this->attachment);
 
@@ -33,6 +34,6 @@ class Attachment_viewAction extends AttachmentAction
         header('Expires: 0');
         header('Content-Transfer-Encoding: binary');
 
-        $this->sendFile($filepath);
+        parrent::sendFile($filepath, $filesize);
     }
 }