FallbackMediaHandler.php: use MediaException and MediaHandlerTrait

This commit is contained in:
Zankaria 2025-03-25 23:49:17 +01:00
parent 716e7cdae0
commit 82c06f923a

View file

@ -2,10 +2,12 @@
namespace Vichan\Service\Media;
use Vichan\Data\ThumbGenerationResult;
use Vichan\Functions\{Fs, Metadata};
use Vichan\Functions\Metadata;
class FallbackMediaHandler implements MediaHandler {
use MediaHandlerTrait;
private string $path;
private int $width;
private int $height;
@ -15,10 +17,10 @@ class FallbackMediaHandler implements MediaHandler {
private static function getShape(string $file_path) {
$ret = \getimagesize($file_path);
if ($ret === false) {
throw new \RuntimeException("Could not read image sizes of '$file_path'");
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");
throw new \RuntimeException("Error '$file_path' is not an image", MediaException::ERR_BAD_MEDIA_TYPE);
}
$width = $ret[0];
@ -60,15 +62,7 @@ class FallbackMediaHandler implements MediaHandler {
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 . '.' . Metadata\mime_to_ext($source_file_mime);
if ($source_file_kind === self::FILE_KIND_UPLOADED) {
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 '$out_path'");
}
} else {
if (!Fs\link_or_copy($source_file_path, $out_path)) {
throw new \RuntimeException("Could not link or copy '$source_file_path' to '$out_path'");
}
}
$this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
return $this->generateThumb(
$handle,