[StoreRemoteMedia] Remote images are now stored exactly on the necessary size for the thumb

This commit is contained in:
Diogo Peralta Cordeiro 2021-02-17 09:48:24 +00:00
parent 5077b65d71
commit cab2112f1c
3 changed files with 18 additions and 17 deletions

View File

@ -100,8 +100,9 @@ class File_thumbnail extends Managed_DataObject
case 'image/svg+xml': case 'image/svg+xml':
throw new UseFileAsThumbnailException($file); throw new UseFileAsThumbnailException($file);
} }
} else {
throw new ServerException("This remote file has no local thumbnail.");
} }
throw new ServerException("This remote file has no local thumbnail.");
} }
$image = ImageFile::fromFileObject($file); $image = ImageFile::fromFileObject($file);
$imgPath = $image->getPath(); $imgPath = $image->getPath();

View File

@ -122,13 +122,8 @@ class AttachmentListItem extends Widget
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 $e) { } catch (UseFileAsThumbnailException|UnsupportedMediaException|FileNotFoundException|ServerException $e) {
$thumb = null; // This remote file has no local thumbnail.
} catch (UnsupportedMediaException $e) {
// FIXME: Show a good representation of unsupported/unshowable images
$thumb = null;
} catch (FileNotFoundException $e) {
// Remote file
$thumb = null; $thumb = null;
} }

View File

@ -252,17 +252,22 @@ class StoreRemoteMediaPlugin extends Plugin
if ($info[0] > $this->thumbnail_width || $info[1] > $this->thumbnail_height) { if ($info[0] > $this->thumbnail_width || $info[1] > $this->thumbnail_height) {
// Temporary object, not stored in DB // Temporary object, not stored in DB
$img = new ImageFile(-1, $filepath); $img = new ImageFile(-1, $filepath);
$box = $img->scaleToFit($this->thumbnail_width, $this->thumbnail_height, $this->thumbnail_crop); // Get proper aspect ratio width and height before lookup
// We have to do it through an ImageFile object because of orientation etc.
// Only other solution would've been to rotate + rewrite uploaded files
// which we don't want to do because we like original, untouched data!
list($width, $height, $x, $y, $w, $h) = $img->scaleToFit($this->thumbnail_width, $this->thumbnail_height, $this->thumbnail_crop);
// The boundary box for our resizing
$box = [
'width' => $width, 'height' => $height,
'x' => $x, 'y' => $y,
'w' => $w, 'h' => $h,
];
$width = $box['width']; $width = $box['width'];
$height = $box['height']; $height = $box['height'];
$outpath = $img->resizeTo($filepath, $box); $img->resizeTo($filepath, $box);
$result = rename($outpath, $filepath);
if (!$result) {
// TRANS: Client exception thrown when a file upload operation fails because the file could
// TRANS: not be moved from the temporary folder to the permanent file location.
// UX: too specific
throw new ClientException(_m('File could not be moved to destination directory.'));
}
} }
} else { } else {
throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file' . throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file' .