diff --git a/core/extension.php b/core/extension.php index 9761e27e..aa93759d 100644 --- a/core/extension.php +++ b/core/extension.php @@ -344,6 +344,8 @@ abstract class FormatterExtension extends Extension */ abstract class DataHandlerExtension extends Extension { + protected $SUPPORTED_EXT = []; + protected function move_upload_to_archive(DataUploadEvent $event) { $target = warehouse_path(Image::IMAGE_DIR, $event->hash); @@ -455,6 +457,13 @@ abstract class DataHandlerExtension extends Extension } } + public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + { + if ($this->supported_ext($event->ext)) { + $this->media_check_properties($event); + } + } + protected function create_image_from_data(string $filename, array $metadata): Image { global $config; @@ -475,10 +484,15 @@ abstract class DataHandlerExtension extends Extension return $image; } - abstract protected function supported_ext(string $ext): bool; + abstract protected function media_check_properties(MediaCheckPropertiesEvent $event): void; abstract protected function check_contents(string $tmpname): bool; abstract protected function create_thumb(string $hash, string $type): bool; + protected function supported_ext(string $ext): bool + { + return in_array(strtolower($ext), $this->SUPPORTED_EXT); + } + public static function get_all_supported_exts(): array { $arr = []; diff --git a/ext/handle_flash/main.php b/ext/handle_flash/main.php index 5dc49fb3..2f8fd1aa 100644 --- a/ext/handle_flash/main.php +++ b/ext/handle_flash/main.php @@ -2,23 +2,18 @@ class FlashFileHandler extends DataHandlerExtension { - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + protected $SUPPORTED_EXT = ["swf"]; + + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { - switch ($event->ext) { - case "swf": - $event->image->lossless = true; - $event->image->video = true; + $event->image->lossless = true; + $event->image->video = true; + $event->image->image = false; - $info = getimagesize($event->file_name); - if (!$info) { - return null; - } - $event->image->image = false; - - $event->image->width = $info[0]; - $event->image->height = $info[1]; - - break; + $info = getimagesize($event->file_name); + if ($info) { + $event->image->width = $info[0]; + $event->image->height = $info[1]; } } @@ -30,12 +25,6 @@ class FlashFileHandler extends DataHandlerExtension return true; } - protected function supported_ext(string $ext): bool - { - $exts = ["swf"]; - return in_array(strtolower($ext), $exts); - } - protected function check_contents(string $tmpname): bool { $fp = fopen($tmpname, "r"); diff --git a/ext/handle_ico/main.php b/ext/handle_ico/main.php index a61fec92..f80555d3 100644 --- a/ext/handle_ico/main.php +++ b/ext/handle_ico/main.php @@ -2,42 +2,27 @@ class IcoFileHandler extends DataHandlerExtension { - const SUPPORTED_EXTENSIONS = ["ico", "ani", "cur"]; + protected $SUPPORTED_EXT = ["ico", "ani", "cur"]; - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { - if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) { - $event->image->lossless = true; - $event->image->video = false; - $event->image->audio = false; - $event->image->image = ($event->ext!="ani"); + $event->image->lossless = true; + $event->image->video = false; + $event->image->audio = false; + $event->image->image = ($event->ext!="ani"); - $fp = fopen($event->file_name, "r"); - try { - unpack("Snull/Stype/Scount", fread($fp, 6)); - $subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16)); - } finally { - fclose($fp); - } - - $width = $subheader['width']; - $height = $subheader['height']; - $event->image->width = $width == 0 ? 256 : $width; - $event->image->height = $height == 0 ? 256 : $height; + $fp = fopen($event->file_name, "r"); + try { + unpack("Snull/Stype/Scount", fread($fp, 6)); + $subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16)); + } finally { + fclose($fp); } - } - protected function supported_ext(string $ext): bool - { - return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); - } - - protected function check_contents(string $file): bool - { - $fp = fopen($file, "r"); - $header = unpack("Snull/Stype/Scount", fread($fp, 6)); - fclose($fp); - return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1)); + $width = $subheader['width']; + $height = $subheader['height']; + $event->image->width = $width == 0 ? 256 : $width; + $event->image->height = $height == 0 ? 256 : $height; } protected function create_thumb(string $hash, string $type): bool @@ -50,4 +35,12 @@ class IcoFileHandler extends DataHandlerExtension return false; } } + + protected function check_contents(string $file): bool + { + $fp = fopen($file, "r"); + $header = unpack("Snull/Stype/Scount", fread($fp, 6)); + fclose($fp); + return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1)); + } } diff --git a/ext/handle_mp3/main.php b/ext/handle_mp3/main.php index 07c54393..1d943b62 100644 --- a/ext/handle_mp3/main.php +++ b/ext/handle_mp3/main.php @@ -2,19 +2,17 @@ class MP3FileHandler extends DataHandlerExtension { - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + protected $SUPPORTED_EXT = ["mp3"]; + + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { - switch ($event->ext) { - case "mp3": - $event->image->audio = true; - $event->image->video = false; - $event->image->lossless = false; - $event->image->image = false; - $event->image->width = 0; - $event->image->height = 0; - break; - } - // TODO: Buff out audio format support, length scanning + $event->image->audio = true; + $event->image->video = false; + $event->image->lossless = false; + $event->image->image = false; + $event->image->width = 0; + $event->image->height = 0; + // TODO: ->length = ??? } protected function create_thumb(string $hash, string $type): bool @@ -23,12 +21,6 @@ class MP3FileHandler extends DataHandlerExtension return true; } - protected function supported_ext(string $ext): bool - { - $exts = ["mp3"]; - return in_array(strtolower($ext), $exts); - } - protected function check_contents(string $tmpname): bool { return getMimeType($tmpname) == 'audio/mpeg'; diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index 3306fe14..c44f7bd0 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -2,9 +2,9 @@ class PixelFileHandler extends DataHandlerExtension { - const SUPPORTED_EXTENSIONS = ["jpg", "jpeg", "gif", "png", "webp"]; + protected $SUPPORTED_EXT = ["jpg", "jpeg", "gif", "png", "webp"]; - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { if (in_array($event->ext, Media::LOSSLESS_FORMATS)) { $event->image->lossless = true; @@ -12,40 +12,30 @@ class PixelFileHandler extends DataHandlerExtension $event->image->lossless = Media::is_lossless_webp($event->file_name); } - if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) { - if ($event->image->lossless==null) { - $event->image->lossless = false; - } - $event->image->audio = false; - switch ($event->ext) { - case "gif": - $event->image->video = Media::is_animated_gif($event->file_name); - break; - case "webp": - $event->image->video = Media::is_animated_webp($event->file_name); - break; - default: - $event->image->video = false; - break; - } - $event->image->image = !$event->image->video; - - $info = getimagesize($event->file_name); - if (!$info) { - return null; - } + if ($event->image->lossless==null) { + $event->image->lossless = false; + } + $event->image->audio = false; + switch ($event->ext) { + case "gif": + $event->image->video = Media::is_animated_gif($event->file_name); + break; + case "webp": + $event->image->video = Media::is_animated_webp($event->file_name); + break; + default: + $event->image->video = false; + break; + } + $event->image->image = !$event->image->video; + $info = getimagesize($event->file_name); + if ($info) { $event->image->width = $info[0]; $event->image->height = $info[1]; } } - protected function supported_ext(string $ext): bool - { - $ext = (($pos = strpos($ext, '?')) !== false) ? substr($ext, 0, $pos) : $ext; - return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); - } - protected function check_contents(string $tmpname): bool { $valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP]; diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 4ffca378..f8c8bef2 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -3,26 +3,42 @@ use enshrined\svgSanitize\Sanitizer; class SVGFileHandler extends DataHandlerExtension { + protected $SUPPORTED_EXT = ["svg"]; + /** @var SVGFileHandlerTheme */ protected $theme; - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + public function onPageRequest(PageRequestEvent $event) { - switch ($event->ext) { - case "svg": - $event->image->lossless = true; - $event->image->video = false; - $event->image->audio = false; - $event->image->image = true; + global $page; + if ($event->page_matches("get_svg")) { + $id = int_escape($event->get_arg(0)); + $image = Image::by_id($id); + $hash = $image->hash; - $msp = new MiniSVGParser($event->file_name); - $event->image->width = $msp->width; - $event->image->height = $msp->height; + $page->set_type("image/svg+xml"); + $page->set_mode(PageMode::DATA); - break; + $sanitizer = new Sanitizer(); + $sanitizer->removeRemoteReferences(true); + $dirtySVG = file_get_contents(warehouse_path(Image::IMAGE_DIR, $hash)); + $cleanSVG = $sanitizer->sanitize($dirtySVG); + $page->set_data($cleanSVG); } } + protected function media_check_properties(MediaCheckPropertiesEvent $event): void + { + $event->image->lossless = true; + $event->image->video = false; + $event->image->audio = false; + $event->image->image = true; + + $msp = new MiniSVGParser($event->file_name); + $event->image->width = $msp->width; + $event->image->height = $msp->height; + } + protected function move_upload_to_archive(DataUploadEvent $event) { $sanitizer = new Sanitizer(); @@ -49,39 +65,6 @@ class SVGFileHandler extends DataHandlerExtension } } - public function onDisplayingImage(DisplayingImageEvent $event) - { - global $page; - if ($this->supported_ext($event->image->ext)) { - $this->theme->display_image($page, $event->image); - } - } - - public function onPageRequest(PageRequestEvent $event) - { - global $page; - if ($event->page_matches("get_svg")) { - $id = int_escape($event->get_arg(0)); - $image = Image::by_id($id); - $hash = $image->hash; - - $page->set_type("image/svg+xml"); - $page->set_mode(PageMode::DATA); - - $sanitizer = new Sanitizer(); - $sanitizer->removeRemoteReferences(true); - $dirtySVG = file_get_contents(warehouse_path(Image::IMAGE_DIR, $hash)); - $cleanSVG = $sanitizer->sanitize($dirtySVG); - $page->set_data($cleanSVG); - } - } - - protected function supported_ext(string $ext): bool - { - $exts = ["svg"]; - return in_array(strtolower($ext), $exts); - } - protected function check_contents(string $file): bool { $msp = new MiniSVGParser($file); diff --git a/ext/handle_video/main.php b/ext/handle_video/main.php index e64b5c8c..5b5b3beb 100644 --- a/ext/handle_video/main.php +++ b/ext/handle_video/main.php @@ -2,14 +2,14 @@ class VideoFileHandler extends DataHandlerExtension { - const SUPPORTED_MIME = [ + protected $SUPPORTED_MIME = [ 'video/webm', 'video/mp4', 'video/ogg', 'video/flv', 'video/x-flv' ]; - const SUPPORTED_EXT = ["flv", "mp4", "m4v", "ogv", "webm"]; + protected $SUPPORTED_EXT = ["flv", "mp4", "m4v", "ogv", "webm"]; public function onInitExt(InitExtEvent $event) { @@ -28,75 +28,65 @@ class VideoFileHandler extends DataHandlerExtension $event->panel->add_block($sb); } - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) + protected function media_check_properties(MediaCheckPropertiesEvent $event): void { - if (in_array($event->ext, self::SUPPORTED_EXT)) { - $event->image->video = true; - $event->image->image = false; - try { - $data = Media::get_ffprobe_data($event->file_name); + $event->image->video = true; + $event->image->image = false; + try { + $data = Media::get_ffprobe_data($event->file_name); - if (is_array($data)) { - if (array_key_exists("streams", $data)) { - $video = false; - $audio = true; - $streams = $data["streams"]; - if (is_array($streams)) { - foreach ($streams as $stream) { - if (is_array($stream)) { - if (array_key_exists("codec_type", $stream)) { - $type = $stream["codec_type"]; - switch ($type) { - case "audio": - $audio = true; - break; - case "video": - $video = true; - break; - } - } - if (array_key_exists("width", $stream) && !empty($stream["width"]) - && is_numeric($stream["width"]) && intval($stream["width"]) > ($event->image->width) ?? 0) { - $event->image->width = intval($stream["width"]); - } - if (array_key_exists("height", $stream) && !empty($stream["height"]) - && is_numeric($stream["height"]) && intval($stream["height"]) > ($event->image->height) ?? 0) { - $event->image->height = intval($stream["height"]); + if (is_array($data)) { + if (array_key_exists("streams", $data)) { + $video = false; + $audio = true; + $streams = $data["streams"]; + if (is_array($streams)) { + foreach ($streams as $stream) { + if (is_array($stream)) { + if (array_key_exists("codec_type", $stream)) { + $type = $stream["codec_type"]; + switch ($type) { + case "audio": + $audio = true; + break; + case "video": + $video = true; + break; } } + if (array_key_exists("width", $stream) && !empty($stream["width"]) + && is_numeric($stream["width"]) && intval($stream["width"]) > ($event->image->width) ?? 0) { + $event->image->width = intval($stream["width"]); + } + if (array_key_exists("height", $stream) && !empty($stream["height"]) + && is_numeric($stream["height"]) && intval($stream["height"]) > ($event->image->height) ?? 0) { + $event->image->height = intval($stream["height"]); + } } - $event->image->video = $video; - $event->image->audio = $audio; - } - } - if (array_key_exists("format", $data)&& is_array($data["format"])) { - $format = $data["format"]; - if (array_key_exists("duration", $format) && is_numeric($format["duration"])) { - $event->image->length = floor(floatval($format["duration"]) * 1000); } + $event->image->video = $video; + $event->image->audio = $audio; + } + } + if (array_key_exists("format", $data)&& is_array($data["format"])) { + $format = $data["format"]; + if (array_key_exists("duration", $format) && is_numeric($format["duration"])) { + $event->image->length = floor(floatval($format["duration"]) * 1000); } } - } catch (MediaException $e) { - // a post with no metadata is better than no post } + } catch (MediaException $e) { + // a post with no metadata is better than no post } } - /** - * Generate the Thumbnail image for particular file. - */ protected function create_thumb(string $hash, string $type): bool { return Media::create_thumbnail_ffmpeg($hash); } - protected function supported_ext(string $ext): bool - { - return in_array(strtolower($ext), self::SUPPORTED_EXT); - } - protected function check_contents(string $tmpname): bool { - return in_array(getMimeType($tmpname), self::SUPPORTED_MIME); + return in_array(getMimeType($tmpname), $this->SUPPORTED_MIME); } }