[StoreRemoteMedia] Fix failing to show remote thumbnails on first load

imgPath onCreateFileImageThumbnailSource would throw FileNotFoundException
This commit is contained in:
Diogo Peralta Cordeiro 2021-04-12 22:42:45 +01:00
parent 0eb96d4805
commit 761ce7fd81
3 changed files with 26 additions and 33 deletions

View File

@ -88,38 +88,27 @@ class File_thumbnail extends Managed_DataObject
bool $force_still = true, bool $force_still = true,
?bool $upscale = null ?bool $upscale = null
): File_thumbnail { ): File_thumbnail {
if ($file->isStoredRemotely()) { // Remote file // Is file stored remotely only?
// If StoreRemoteMedia or Embed are enabled... $was_stored_remotely = $file->isStoredRemotely();
if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, 'image'])) {
if (!file_exists($imgPath)) {
throw new FileNotFoundException($imgPath);
}
// First some mimetype specific exceptions // If StoreRemoteMedia or Embed are enabled (they will only act if appropriate btw)...
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();
$media = common_get_mime_media($file->mimetype); $media = common_get_mime_media($file->mimetype);
if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media])) { Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media]);
if (!file_exists($imgPath)) {
throw new FileNotFoundException($imgPath);
}
// First some mimetype specific exceptions // If it was stored remotely, we can now assume it was sufficiently retrieved
switch ($file->mimetype) { if ($was_stored_remotely) {
case 'image/svg+xml': $file = File::getById($file->getID());
throw new UseFileAsThumbnailException($file); }
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);
} }
} }

View File

@ -118,11 +118,11 @@ class AttachmentListItem extends Widget
try { try {
if (!empty($enclosure->mimetype)) { if (!empty($enclosure->mimetype)) {
// First, prepare a thumbnail if it exists. // First, prepare a thumbnail if it exists.
$thumb = null;
try { try {
// Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still") // 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); $thumb = File_thumbnail::fromFileObject($this->attachment, null, null, false, false);
} catch (UseFileAsThumbnailException|UnsupportedMediaException|FileNotFoundException|ServerException $e) { } 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. // This remote file has no local thumbnail.
$thumb = null; $thumb = null;
} }

View File

@ -159,6 +159,7 @@ class StoreRemoteMediaPlugin extends Plugin
} }
} }
$ft = null;
if ($this->store_original) { if ($this->store_original) {
try { try {
// Update our database for the file record // Update our database for the file record
@ -194,9 +195,12 @@ class StoreRemoteMediaPlugin extends Plugin
} }
// Out // Out
$imgPath = $file->getPath(); try {
$imgPath = $file->getFileOrThumbnailPath($ft);
return !file_exists($imgPath); return !file_exists($imgPath);
} catch (Exception $e) {
return true;
}
} }
/** /**