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
91
inc/Service/Media/MimeMapMediaHandler.php
Normal file
91
inc/Service/Media/MimeMapMediaHandler.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
namespace Vichan\Service\Media;
|
||||
|
||||
use Vichan\Data\{MediaInstallResult, ThumbGenerationResult};
|
||||
use Vichan\Functions\Metadata;
|
||||
|
||||
|
||||
class MimeMapMediaHandler implements MediaHandler {
|
||||
use MediaHandlerTrait;
|
||||
|
||||
/**
|
||||
* @var callable(string): ?string
|
||||
*/
|
||||
private mixed $mime_mapper;
|
||||
|
||||
private static function getShape(string $file_path) {
|
||||
$ret = \getimagesize($file_path);
|
||||
if ($ret === false) {
|
||||
throw new MediaException("Could not read image sizes of '$file_path'", MediaException::ERR_NO_OPEN);
|
||||
}
|
||||
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
|
||||
throw new \RuntimeException("Error '$file_path' is not an image", MediaException::ERR_BAD_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
$width = $ret[0];
|
||||
$height = $ret[1];
|
||||
$mime = $ret['mime'];
|
||||
return [ $width, $height, $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 ($this->mime_mapper)($mime) !== null;
|
||||
}
|
||||
|
||||
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
|
||||
return [ $file_path, $file_mime, $file_kind ];
|
||||
}
|
||||
|
||||
public function closeHandle(mixed $handle) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
public function installMediaAndGenerateThumb(
|
||||
mixed $handle,
|
||||
string $media_preferred_out_file_basepath,
|
||||
string $thumb_preferred_out_file_basepath,
|
||||
int $thumb_max_width,
|
||||
int $thumb_max_height
|
||||
): MediaInstallResult {
|
||||
list($source_file_path, $source_file_mime, $source_file_kind) = $handle;
|
||||
$out_path = $media_preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime);
|
||||
|
||||
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
|
||||
|
||||
$thumb = $this->generateThumbImpl(
|
||||
$source_file_mime,
|
||||
$thumb_max_width,
|
||||
$thumb_max_height
|
||||
);
|
||||
return new MediaInstallResult($thumb, $out_path);
|
||||
}
|
||||
|
||||
public function generateThumb(
|
||||
mixed $handle,
|
||||
string $preferred_out_file_basepath,
|
||||
int $max_width,
|
||||
int $max_height
|
||||
): ThumbGenerationResult {
|
||||
return $this->generateThumbImpl($handle[1], $max_width, $max_height);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue