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;
|
use Vichan\Functions\Metadata;
|
||||||
|
|
||||||
|
|
||||||
class FallbackMediaHandler implements MediaHandler {
|
class MimeMapMediaHandler implements MediaHandler {
|
||||||
use MediaHandlerTrait;
|
use MediaHandlerTrait;
|
||||||
|
|
||||||
private string $path;
|
/**
|
||||||
private int $width;
|
* @var callable(string): ?string
|
||||||
private int $height;
|
*/
|
||||||
private string $mime;
|
private mixed $mime_mapper;
|
||||||
|
|
||||||
|
|
||||||
private static function getShape(string $file_path) {
|
private static function getShape(string $file_path) {
|
||||||
$ret = \getimagesize($file_path);
|
$ret = \getimagesize($file_path);
|
||||||
|
@ -29,16 +28,28 @@ class FallbackMediaHandler implements MediaHandler {
|
||||||
return [ $width, $height, $mime ];
|
return [ $width, $height, $mime ];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(string $default_thumb_path) {
|
private function generateThumbImpl(
|
||||||
list($width, $height, $mime) = self::getShape($default_thumb_path);
|
string $source_file_mime,
|
||||||
$this->path = $default_thumb_path;
|
int $max_width,
|
||||||
$this->width = $width;
|
int $max_height
|
||||||
$this->height = $height;
|
): ThumbGenerationResult {
|
||||||
$this->mime = $mime;
|
$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 {
|
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 {
|
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);
|
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
|
||||||
|
|
||||||
$thumb = $this->generateThumb(
|
$thumb = $this->generateThumbImpl(
|
||||||
$handle,
|
$source_file_mime,
|
||||||
$thumb_preferred_out_file_basepath,
|
|
||||||
$thumb_max_width,
|
$thumb_max_width,
|
||||||
$thumb_max_height
|
$thumb_max_height
|
||||||
);
|
);
|
||||||
|
@ -76,11 +86,6 @@ class FallbackMediaHandler implements MediaHandler {
|
||||||
int $max_width,
|
int $max_width,
|
||||||
int $max_height
|
int $max_height
|
||||||
): ThumbGenerationResult {
|
): ThumbGenerationResult {
|
||||||
return new ThumbGenerationResult(
|
return $this->generateThumbImpl($handle[1], $max_width, $max_height);
|
||||||
$this->path,
|
|
||||||
$this->mime,
|
|
||||||
\min($this->width, $max_width),
|
|
||||||
\min($this->height, $max_height)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue