forked from leftypol/leftypol
MimeMapMediaHandler.php: rework FallbackMediaHandler into a mapper
This commit is contained in:
parent
991ddc9001
commit
55d4802ca7
1 changed files with 27 additions and 22 deletions
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue