[ImageEncoder] Handle VIPS unsupported image type

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

View File

@ -0,0 +1,19 @@
<?php
namespace Plugin\ImageEncoder\Exception;
use App\Util\Exception\ClientException;
use Throwable;
class UnsupportedFileTypeException extends ClientException
{
/**
* @param string $message
* @param int $code
* @param null|Throwable $previous
*/
public function __construct(string $message = '', int $code = 418, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@ -25,29 +25,36 @@ use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Entity\Attachment; use App\Entity\Attachment;
use App\Entity\AttachmentThumbnail;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\ServerException;
use App\Util\Exception\TemporaryFileException; use App\Util\Exception\TemporaryFileException;
use App\Util\Formatting; use App\Util\Formatting;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Exception; use Exception;
use Jcupitt\Vips; use Jcupitt\Vips;
use Plugin\ImageEncoder\Exception\UnsupportedFileTypeException;
use SplFileInfo; use SplFileInfo;
/** /**
* Create thumbnails and validate image attachments * Create thumbnails and validate image attachments
* *
* @package GNUsocial * @package GNUsocial
* @ccategory Attachment * @category Attachment
* *
* @author Diogo Peralta Cordeiro <mail@diogo.site> * @author Diogo Peralta Cordeiro <mail@diogo.site>
* @authir Hugo Sales <hugo@hsal.es> * @author Hugo Sales <hugo@hsal.es>
* *
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class ImageEncoder extends Plugin class ImageEncoder extends Plugin
{ {
public function version(): string
{
return '3.0.0';
}
/** /**
* Several obscure file types should be normalized to WebP on resize. * Several obscure file types should be normalized to WebP on resize.
*/ */
@ -62,12 +69,13 @@ class ImageEncoder extends Plugin
* *
* @param SplFileInfo $file * @param SplFileInfo $file
* @param null|string $mimetype in/out * @param null|string $mimetype in/out
* @param null|string $title in/out
* @param null|int $width out * @param null|int $width out
* @param null|int $height out * @param null|int $height out
* *
* @throws Vips\Exception * @throws ClientException When vips doesn't understand the given mimetype
* @throws ServerException
* @throws TemporaryFileException * @throws TemporaryFileException
* @throws Vips\Exception
* *
* @return bool * @return bool
*/ */
@ -81,19 +89,24 @@ class ImageEncoder extends Plugin
// Try to maintain original mimetype extension, otherwise default to preferred. // Try to maintain original mimetype extension, otherwise default to preferred.
$extension = image_type_to_extension($this->preferredType(), include_dot: true); $extension = image_type_to_extension($this->preferredType(), include_dot: true);
GSFile::ensureFilenameWithProperExtension( $extension = GSFile::ensureFilenameWithProperExtension(
title: $file->getFilename(), title: $file->getFilename(),
mimetype: $original_mimetype, mimetype: $original_mimetype,
ext: $extension, ext: $extension,
force: false force: false
); ) ?? $extension;
// TemporaryFile handles deleting the file if some error occurs // TemporaryFile handles deleting the file if some error occurs
// IMPORTANT: We have to specify the extension for the temporary file // IMPORTANT: We have to specify the extension for the temporary file
// in order to have a format conversion // in order to have a format conversion
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]); $temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']); try {
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
} catch (Vips\Exception $e) {
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
}
$width = Common::clamp($image->width, 0, Common::config('attachments', 'max_width')); $width = Common::clamp($image->width, 0, Common::config('attachments', 'max_width'));
$height = Common::clamp($image->height, 0, Common::config('attachments', 'max_height')); $height = Common::clamp($image->height, 0, Common::config('attachments', 'max_height'));
$image = $image->crop(0, 0, $width, $height); $image = $image->crop(0, 0, $width, $height);
@ -102,6 +115,7 @@ class ImageEncoder extends Plugin
// Replace original file with the sanitized one // Replace original file with the sanitized one
$temp->commit($file->getRealPath()); $temp->commit($file->getRealPath());
// Only one plugin can handle sanitization
return Event::stop; return Event::stop;
} }
@ -143,13 +157,14 @@ class ImageEncoder extends Plugin
* enable the handling of bigger images, which can cause a peak of memory consumption, while * enable the handling of bigger images, which can cause a peak of memory consumption, while
* encoding * encoding
* *
* @param Attachment $attachment * @param string $source
* @param AttachmentThumbnail $thumbnail * @param null|TemporaryFile $destination
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param bool $crop * @param bool $smart_crop
* @param null|string $mimetype
* *
* @throws Exception * @throws TemporaryFileException
* @throws Vips\Exception * @throws Vips\Exception
* *
* @return bool * @return bool
@ -193,4 +208,25 @@ class ImageEncoder extends Plugin
} }
return Event::stop; return Event::stop;
} }
/**
* Event raised when GNU social polls the plugin for information about it.
* Adds this plugin's version information to $versions array
*
* @param &$versions array inherited from parent
*
* @return bool true hook value
*/
public function onPluginVersion(array &$versions): bool
{
$versions[] = [
'name' => 'ImageEncoder',
'version' => $this->version(),
'author' => 'Hugo Sales, Diogo Peralta Cordeiro',
'homepage' => GNUSOCIAL_PROJECT_URL,
'description' => // TRANS: Plugin description.
_m('Use VIPS for some additional image support.'),
];
return Event::next;
}
} }