[ENTITY][AttachmentThumbnail] Improve the way EncoderPlugins participate in the thumbnail process

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-18 22:10:19 +01:00 committed by Hugo Sales
parent e4b2821657
commit f9079784c4
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -154,8 +154,9 @@ class AttachmentThumbnail extends Entity
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
* *
* @throws ClientException
* @throws NotFoundException
* @throws ServerException * @throws ServerException
* @throws \App\Util\Exception\TemporaryFileException
* *
* @return mixed * @return mixed
*/ */
@ -181,18 +182,16 @@ class AttachmentThumbnail extends Entity
throw new NotStoredLocallyException(); throw new NotStoredLocallyException();
} }
$thumbnail = self::create(['attachment_id' => $attachment->getId()]); $thumbnail = self::create(['attachment_id' => $attachment->getId()]);
$event_map = [];
Event::handle('ResizerAvailable', [&$event_map]);
$mimetype = $attachment->getMimetype(); $mimetype = $attachment->getMimetype();
$event_map[$mimetype] = [];
$major_mime = GSFile::mimetypeMajor($mimetype); $major_mime = GSFile::mimetypeMajor($mimetype);
$event_map[$major_mime] = [];
if (in_array($major_mime, array_keys($event_map))) { Event::handle('FileResizerAvailable', [&$event_map, $mimetype]);
// Always prefer specific encoders
$encoders = array_merge($event_map[$mimetype], $event_map[$major_mime]);
foreach ($encoders as $encoder) {
$temp = null; // Let the EncoderPlugin create a temporary file for us $temp = null; // Let the EncoderPlugin create a temporary file for us
if (!Event::handle( if ($encoder($attachment->getPath(), $temp, $width, $height, $crop, $mimetype)) {
$event_map[$major_mime],
[$attachment->getPath(), &$temp, &$width, &$height, $crop, &$mimetype]
)
) {
$thumbnail->setAttachment($attachment); $thumbnail->setAttachment($attachment);
$thumbnail->setWidth($predicted_width); $thumbnail->setWidth($predicted_width);
$thumbnail->setHeight($predicted_height); $thumbnail->setHeight($predicted_height);
@ -204,15 +203,9 @@ class AttachmentThumbnail extends Entity
DB::flush(); DB::flush();
$temp->move(Common::config('thumbnail', 'dir'), $filename); $temp->move(Common::config('thumbnail', 'dir'), $filename);
return $thumbnail; return $thumbnail;
} else {
// @codeCoverageIgnoreStart
Log::debug($m = ('Somehow the EncoderPlugin didn\'t handle ' . $attachment->getMimetype()));
throw new ServerException(_m($m));
} }
} else { }
throw new ClientException(_m('Can not generate thumbnail for attachment with id={id}', ['id' => $attachment->getId()])); throw new ClientException(_m('Can not generate thumbnail for attachment with id={id}', ['id' => $attachment->getId()]));
// @codeCoverageIgnoreEnd
}
} }
} }
@ -301,7 +294,7 @@ class AttachmentThumbnail extends Entity
'name' => 'attachment_thumbnail', 'name' => 'attachment_thumbnail',
'fields' => [ 'fields' => [
'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'], 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'thumbnail for what attachment'],
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'], 'mimetype' => ['type' => 'varchar', 'length' => 129, 'description' => 'resource mime type 64+1+64, images hardly will show up with long mimetypes, this is probably safe considering rfc6838#section-4.2'],
'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'], 'width' => ['type' => 'int', 'not null' => true, 'description' => 'width of thumbnail'],
'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'], 'height' => ['type' => 'int', 'not null' => true, 'description' => 'height of thumbnail'],
'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thumbnail filename'], 'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thumbnail filename'],