From 55d4802ca742b9b7c55b3a3360e3def57a5ae1b7 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Fri, 28 Mar 2025 15:26:06 +0100 Subject: [PATCH] MimeMapMediaHandler.php: rework FallbackMediaHandler into a mapper --- ...diaHandler.php => MimeMapMediaHandler.php} | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) rename inc/Service/Media/{FallbackMediaHandler.php => MimeMapMediaHandler.php} (68%) diff --git a/inc/Service/Media/FallbackMediaHandler.php b/inc/Service/Media/MimeMapMediaHandler.php similarity index 68% rename from inc/Service/Media/FallbackMediaHandler.php rename to inc/Service/Media/MimeMapMediaHandler.php index 31395089..22550e04 100644 --- a/inc/Service/Media/FallbackMediaHandler.php +++ b/inc/Service/Media/MimeMapMediaHandler.php @@ -5,14 +5,13 @@ use Vichan\Data\{MediaInstallResult, ThumbGenerationResult}; use Vichan\Functions\Metadata; -class FallbackMediaHandler implements MediaHandler { +class MimeMapMediaHandler implements MediaHandler { use MediaHandlerTrait; - private string $path; - private int $width; - private int $height; - private string $mime; - + /** + * @var callable(string): ?string + */ + private mixed $mime_mapper; private static function getShape(string $file_path) { $ret = \getimagesize($file_path); @@ -29,16 +28,28 @@ class FallbackMediaHandler implements MediaHandler { return [ $width, $height, $mime ]; } - public function __construct(string $default_thumb_path) { - list($width, $height, $mime) = self::getShape($default_thumb_path); - $this->path = $default_thumb_path; - $this->width = $width; - $this->height = $height; - $this->mime = $mime; + private function generateThumbImpl( + string $source_file_mime, + int $max_width, + int $max_height + ): ThumbGenerationResult { + $thumb_path = ($this->mime_mapper)($source_file_mime); + list($thumb_width, $thumb_height, $thumb_mime) = self::getShape($thumb_path); + + return new ThumbGenerationResult( + $thumb_path, + $thumb_mime, + \min($thumb_width, $max_width), + \min($thumb_height, $max_height) + ); + } + + public function __construct(callable $mime_mapper) { + $this->mime_mapper = $mime_mapper; } public function supportsMime(string $mime): bool { - return true; + return ($this->mime_mapper)($mime) !== null; } public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed { @@ -61,9 +72,8 @@ class FallbackMediaHandler implements MediaHandler { $this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path); - $thumb = $this->generateThumb( - $handle, - $thumb_preferred_out_file_basepath, + $thumb = $this->generateThumbImpl( + $source_file_mime, $thumb_max_width, $thumb_max_height ); @@ -76,11 +86,6 @@ class FallbackMediaHandler implements MediaHandler { int $max_width, int $max_height ): ThumbGenerationResult { - return new ThumbGenerationResult( - $this->path, - $this->mime, - \min($this->width, $max_width), - \min($this->height, $max_height) - ); + return $this->generateThumbImpl($handle[1], $max_width, $max_height); } }