[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,20 +88,21 @@ 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)) { // If StoreRemoteMedia or Embed are enabled (they will only act if appropriate btw)...
throw new FileNotFoundException($imgPath); $media = common_get_mime_media($file->mimetype);
Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media]);
// If it was stored remotely, we can now assume it was sufficiently retrieved
if ($was_stored_remotely) {
$file = File::getById($file->getID());
} }
// First some mimetype specific exceptions if (file_exists($imgPath)) {
switch ($file->mimetype) { $image = new ImageFile($file->getID(), $imgPath, null, $file->getUrl(false));
case 'image/svg+xml': } else {
throw new UseFileAsThumbnailException($file);
}
}
}
try { try {
$image = ImageFile::fromFileObject($file); $image = ImageFile::fromFileObject($file);
} catch (InvalidFilenameException $e) { } catch (InvalidFilenameException $e) {
@ -109,18 +110,6 @@ class File_thumbnail extends Managed_DataObject
$existing_thumb = File_thumbnail::byFile($file); $existing_thumb = File_thumbnail::byFile($file);
$image = new ImageFile($file->getID(), $existing_thumb->getPath(), null, $existing_thumb->url); $image = new ImageFile($file->getID(), $existing_thumb->getPath(), null, $existing_thumb->url);
} }
$imgPath = $image->getPath();
$media = common_get_mime_media($file->mimetype);
if (Event::handle('CreateFileImageThumbnailSource', [$file, &$imgPath, $media])) {
if (!file_exists($imgPath)) {
throw new FileNotFoundException($imgPath);
}
// First some mimetype specific exceptions
switch ($file->mimetype) {
case 'image/svg+xml':
throw new UseFileAsThumbnailException($file);
}
} }
if ($image->animated && !common_config('thumbnail', 'animated')) { if ($image->animated && !common_config('thumbnail', 'animated')) {

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