[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 f088a3d54f
commit b4b71f7626
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 $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);
}
}

View File

@ -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;
}

View File

@ -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;
}
}
/**