FallbackMediaHandler.php: use mime functions

This commit is contained in:
Zankaria 2025-03-20 17:38:47 +01:00
parent 02a807e0e5
commit faf754afed

View file

@ -2,8 +2,7 @@
namespace Vichan\Service\Media; namespace Vichan\Service\Media;
use Vichan\Data\ThumbGenerationResult; use Vichan\Data\ThumbGenerationResult;
use Vichan\Functions\Fs; use Vichan\Functions\{Fs, Mime};
use Mimey\MimeTypes;
class FallbackMediaHandler implements MediaHandler { class FallbackMediaHandler implements MediaHandler {
@ -11,16 +10,14 @@ class FallbackMediaHandler implements MediaHandler {
private int $width; private int $width;
private int $height; private int $height;
private string $mime; private string $mime;
private MimeTypes $mime_types;
public function __construct(ImageMetadataReader $image_metadate_reader, string $default_thumb_path, MimeTypes $mime_types) { public function __construct(ImageMetadataReader $image_metadate_reader, string $default_thumb_path) {
$res = $image_metadate_reader->getMetadata($default_thumb_path); $res = $image_metadate_reader->getMetadata($default_thumb_path);
$this->path = $default_thumb_path; $this->path = $default_thumb_path;
$this->width = $res->width; $this->width = $res->width;
$this->height = $res->height; $this->height = $res->height;
$this->mime = $res->mime; $this->mime = $res->mime;
$this->mime_types = $mime_types;
} }
public function supportsMime(string $mime): bool { public function supportsMime(string $mime): bool {
@ -28,7 +25,7 @@ class FallbackMediaHandler implements MediaHandler {
} }
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 {
return [ $file_path, $file_kind ]; return [ $file_path, $file_mime, $file_kind ];
} }
public function closeHandle(mixed $handle) { public function closeHandle(mixed $handle) {
@ -37,27 +34,31 @@ class FallbackMediaHandler implements MediaHandler {
public function installMediaAndGenerateThumb( public function installMediaAndGenerateThumb(
mixed $handle, mixed $handle,
string $media_preferred_out_file_path, string $media_preferred_out_file_dir,
string $thumb_preferred_out_file_path, string $media_preferred_out_file_name,
string $thumb_preferred_out_file_dir,
string $thumb_preferred_out_file_name,
string $thumb_preferred_out_mime, string $thumb_preferred_out_mime,
int $thumb_max_width, int $thumb_max_width,
int $thumb_max_height int $thumb_max_height
) { ) {
list($source_file_path, $source_file_kind) = $handle; list($source_file_path, $source_file_mime, $source_file_kind) = $handle;
$out_path = $media_preferred_out_file_dir . DIRECTORY_SEPARATOR . $media_preferred_out_file_name . '.' . Mime\mime_to_ext($source_file_mime);
if ($source_file_kind === self::FILE_KIND_UPLOADED) { if ($source_file_kind === self::FILE_KIND_UPLOADED) {
if (!Fs\move_or_copy_uploaded($source_file_path, $media_preferred_out_file_path)) { if (!Fs\move_or_copy_uploaded($source_file_path, $out_path)) {
throw new \RuntimeException("Could not move or copy uploaded file '$source_file_path' to '$media_preferred_out_file_path'"); throw new \RuntimeException("Could not move or copy uploaded file '$source_file_path' to '$out_path'");
} }
} else { } else {
if (!Fs\link_or_copy($source_file_path, $media_preferred_out_file_path)) { if (!Fs\link_or_copy($source_file_path, $out_path)) {
throw new \RuntimeException("Could not link or copy '$source_file_path' to '$media_preferred_out_file_path'"); throw new \RuntimeException("Could not link or copy '$source_file_path' to '$out_path'");
} }
} }
return $this->generateThumb( return $this->generateThumb(
$handle, $handle,
$thumb_preferred_out_file_path, $thumb_preferred_out_file_dir,
$thumb_preferred_out_file_name,
$thumb_preferred_out_mime, $thumb_preferred_out_mime,
$thumb_max_width, $thumb_max_width,
$thumb_max_height $thumb_max_height
@ -66,7 +67,8 @@ class FallbackMediaHandler implements MediaHandler {
public function generateThumb( public function generateThumb(
mixed $handle, mixed $handle,
string $preferred_out_file_path, string $preferred_out_file_dir,
string $preferred_out_file_name,
string $preferred_out_mime, string $preferred_out_mime,
int $max_width, int $max_width,
int $max_height int $max_height