From b4b71f762669d815ebe29bd7d27962a01a3349ea Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Mon, 12 Apr 2021 22:42:45 +0100 Subject: [PATCH] [StoreRemoteMedia] Fix failing to show remote thumbnails on first load imgPath onCreateFileImageThumbnailSource would throw FileNotFoundException --- classes/File_thumbnail.php | 47 +++++++------------ lib/media/attachmentlistitem.php | 2 +- .../StoreRemoteMediaPlugin.php | 10 ++-- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/classes/File_thumbnail.php b/classes/File_thumbnail.php index 1a09826a1b..fd5f01effb 100644 --- a/classes/File_thumbnail.php +++ b/classes/File_thumbnail.php @@ -88,38 +88,27 @@ class File_thumbnail extends Managed_DataObject bool $force_still = true, ?bool $upscale = null ): File_thumbnail { - if ($file->isStoredRemotely()) { // Remote file - // If StoreRemoteMedia or Embed are enabled... - if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, 'image'])) { - if (!file_exists($imgPath)) { - throw new FileNotFoundException($imgPath); - } + // Is file stored remotely only? + $was_stored_remotely = $file->isStoredRemotely(); - // First some mimetype specific exceptions - switch ($file->mimetype) { - case 'image/svg+xml': - throw new UseFileAsThumbnailException($file); - } - } - } - try { - $image = ImageFile::fromFileObject($file); - } catch (InvalidFilenameException $e) { - // Not having an original local file doesn't mean we don't have a thumbnail. - $existing_thumb = File_thumbnail::byFile($file); - $image = new ImageFile($file->getID(), $existing_thumb->getPath(), null, $existing_thumb->url); - } - $imgPath = $image->getPath(); + // If StoreRemoteMedia or Embed are enabled (they will only act if appropriate btw)... $media = common_get_mime_media($file->mimetype); - if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media])) { - if (!file_exists($imgPath)) { - throw new FileNotFoundException($imgPath); - } + Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media]); - // First some mimetype specific exceptions - switch ($file->mimetype) { - case 'image/svg+xml': - throw new UseFileAsThumbnailException($file); + // If it was stored remotely, we can now assume it was sufficiently retrieved + if ($was_stored_remotely) { + $file = File::getById($file->getID()); + } + + if (file_exists($imgPath)) { + $image = new ImageFile($file->getID(), $imgPath, null, $file->getUrl(false)); + } else { + try { + $image = ImageFile::fromFileObject($file); + } catch (InvalidFilenameException $e) { + // Not having an original local file doesn't mean we don't have a thumbnail. + $existing_thumb = File_thumbnail::byFile($file); + $image = new ImageFile($file->getID(), $existing_thumb->getPath(), null, $existing_thumb->url); } } diff --git a/lib/media/attachmentlistitem.php b/lib/media/attachmentlistitem.php index 496e2b2447..ef33e6b20b 100644 --- a/lib/media/attachmentlistitem.php +++ b/lib/media/attachmentlistitem.php @@ -118,11 +118,11 @@ class AttachmentListItem extends Widget try { if (!empty($enclosure->mimetype)) { // First, prepare a thumbnail if it exists. - $thumb = null; try { // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still") $thumb = File_thumbnail::fromFileObject($this->attachment, null, null, false, false); } catch (UseFileAsThumbnailException|UnsupportedMediaException|FileNotFoundException|ServerException $e) { + common_debug("AttachmentListItem couldn't find a thumbnail for {$this->attachment->getID()} because {$e->getMessage()}"); // This remote file has no local thumbnail. $thumb = null; } diff --git a/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php b/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php index c5f6399a89..a987024b42 100644 --- a/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php +++ b/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php @@ -159,6 +159,7 @@ class StoreRemoteMediaPlugin extends Plugin } } + $ft = null; if ($this->store_original) { try { // Update our database for the file record @@ -194,9 +195,12 @@ class StoreRemoteMediaPlugin extends Plugin } // Out - $imgPath = $file->getPath(); - - return !file_exists($imgPath); + try { + $imgPath = $file->getFileOrThumbnailPath($ft); + return !file_exists($imgPath); + } catch (Exception $e) { + return true; + } } /**