[ATTACHMENT] Some attachments don't have thumbnails and that's okay

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-14 15:07:17 +01:00 committed by Hugo Sales
parent b4a03b814f
commit f690bc06ae
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 8 additions and 0 deletions

View File

@ -221,6 +221,7 @@ class Attachment extends Entity
} else {
$this->setFilename(null);
$this->setSize(null);
// Important not to null neither width nor height
DB::persist($this);
DB::flush();
}

View File

@ -163,12 +163,19 @@ class AttachmentThumbnail extends Entity
$predicted_width = null;
$predicted_height = null;
try {
if (is_null($attachment->getWidth()) || is_null($attachment->getHeight())) {
// TODO: check if we can generate from an existing thumbnail
throw new ClientException(_m('Invalid dimensions requested for thumbnail.'));
}
return Cache::get('thumb-' . $attachment->getId() . "-{$width}x{$height}",
function () use ($crop, $attachment, $width, $height, &$predicted_width, &$predicted_height) {
[$predicted_width, $predicted_height] = self::predictScalingValues($attachment->getWidth(), $attachment->getHeight(), $width, $height, $crop);
return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'width' => $predicted_width, 'height' => $predicted_height]);
});
} catch (NotFoundException $e) {
if (is_null($attachment->getPath())) {
throw new NotFoundException('Can\'t generate a thumbnail for this attachment given the requested dimensions');
}
$thumbnail = self::create(['attachment_id' => $attachment->getId()]);
$event_map = [];
Event::handle('ResizerAvailable', [&$event_map]);