dedupe more data handling

This commit is contained in:
Shish 2020-02-23 18:37:22 +00:00
parent 674d3fc6fa
commit b5e9daeab5
7 changed files with 149 additions and 198 deletions

View File

@ -344,6 +344,8 @@ abstract class FormatterExtension extends Extension
*/ */
abstract class DataHandlerExtension extends Extension abstract class DataHandlerExtension extends Extension
{ {
protected $SUPPORTED_EXT = [];
protected function move_upload_to_archive(DataUploadEvent $event) protected function move_upload_to_archive(DataUploadEvent $event)
{ {
$target = warehouse_path(Image::IMAGE_DIR, $event->hash); $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 protected function create_image_from_data(string $filename, array $metadata): Image
{ {
global $config; global $config;
@ -475,10 +484,15 @@ abstract class DataHandlerExtension extends Extension
return $image; 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 check_contents(string $tmpname): bool;
abstract protected function create_thumb(string $hash, string $type): 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 public static function get_all_supported_exts(): array
{ {
$arr = []; $arr = [];

View File

@ -2,23 +2,18 @@
class FlashFileHandler extends DataHandlerExtension 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->lossless = true;
$event->image->video = true; $event->image->video = true;
$info = getimagesize($event->file_name);
if (!$info) {
return null;
}
$event->image->image = false; $event->image->image = false;
$info = getimagesize($event->file_name);
if ($info) {
$event->image->width = $info[0]; $event->image->width = $info[0];
$event->image->height = $info[1]; $event->image->height = $info[1];
break;
} }
} }
@ -30,12 +25,6 @@ class FlashFileHandler extends DataHandlerExtension
return true; return true;
} }
protected function supported_ext(string $ext): bool
{
$exts = ["swf"];
return in_array(strtolower($ext), $exts);
}
protected function check_contents(string $tmpname): bool protected function check_contents(string $tmpname): bool
{ {
$fp = fopen($tmpname, "r"); $fp = fopen($tmpname, "r");

View File

@ -2,11 +2,10 @@
class IcoFileHandler extends DataHandlerExtension 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->lossless = true;
$event->image->video = false; $event->image->video = false;
$event->image->audio = false; $event->image->audio = false;
@ -25,20 +24,6 @@ class IcoFileHandler extends DataHandlerExtension
$event->image->width = $width == 0 ? 256 : $width; $event->image->width = $width == 0 ? 256 : $width;
$event->image->height = $height == 0 ? 256 : $height; $event->image->height = $height == 0 ? 256 : $height;
} }
}
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));
}
protected function create_thumb(string $hash, string $type): bool protected function create_thumb(string $hash, string $type): bool
{ {
@ -50,4 +35,12 @@ class IcoFileHandler extends DataHandlerExtension
return false; 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));
}
} }

View File

@ -2,19 +2,17 @@
class MP3FileHandler extends DataHandlerExtension 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->audio = true;
$event->image->video = false; $event->image->video = false;
$event->image->lossless = false; $event->image->lossless = false;
$event->image->image = false; $event->image->image = false;
$event->image->width = 0; $event->image->width = 0;
$event->image->height = 0; $event->image->height = 0;
break; // TODO: ->length = ???
}
// TODO: Buff out audio format support, length scanning
} }
protected function create_thumb(string $hash, string $type): bool protected function create_thumb(string $hash, string $type): bool
@ -23,12 +21,6 @@ class MP3FileHandler extends DataHandlerExtension
return true; return true;
} }
protected function supported_ext(string $ext): bool
{
$exts = ["mp3"];
return in_array(strtolower($ext), $exts);
}
protected function check_contents(string $tmpname): bool protected function check_contents(string $tmpname): bool
{ {
return getMimeType($tmpname) == 'audio/mpeg'; return getMimeType($tmpname) == 'audio/mpeg';

View File

@ -2,9 +2,9 @@
class PixelFileHandler extends DataHandlerExtension 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)) { if (in_array($event->ext, Media::LOSSLESS_FORMATS)) {
$event->image->lossless = true; $event->image->lossless = true;
@ -12,7 +12,6 @@ class PixelFileHandler extends DataHandlerExtension
$event->image->lossless = Media::is_lossless_webp($event->file_name); $event->image->lossless = Media::is_lossless_webp($event->file_name);
} }
if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) {
if ($event->image->lossless==null) { if ($event->image->lossless==null) {
$event->image->lossless = false; $event->image->lossless = false;
} }
@ -31,21 +30,12 @@ class PixelFileHandler extends DataHandlerExtension
$event->image->image = !$event->image->video; $event->image->image = !$event->image->video;
$info = getimagesize($event->file_name); $info = getimagesize($event->file_name);
if (!$info) { if ($info) {
return null;
}
$event->image->width = $info[0]; $event->image->width = $info[0];
$event->image->height = $info[1]; $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 protected function check_contents(string $tmpname): bool
{ {
$valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP]; $valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP];

View File

@ -3,13 +3,32 @@ use enshrined\svgSanitize\Sanitizer;
class SVGFileHandler extends DataHandlerExtension class SVGFileHandler extends DataHandlerExtension
{ {
protected $SUPPORTED_EXT = ["svg"];
/** @var SVGFileHandlerTheme */ /** @var SVGFileHandlerTheme */
protected $theme; protected $theme;
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) 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 media_check_properties(MediaCheckPropertiesEvent $event): void
{ {
switch ($event->ext) {
case "svg":
$event->image->lossless = true; $event->image->lossless = true;
$event->image->video = false; $event->image->video = false;
$event->image->audio = false; $event->image->audio = false;
@ -18,9 +37,6 @@ class SVGFileHandler extends DataHandlerExtension
$msp = new MiniSVGParser($event->file_name); $msp = new MiniSVGParser($event->file_name);
$event->image->width = $msp->width; $event->image->width = $msp->width;
$event->image->height = $msp->height; $event->image->height = $msp->height;
break;
}
} }
protected function move_upload_to_archive(DataUploadEvent $event) protected function move_upload_to_archive(DataUploadEvent $event)
@ -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 protected function check_contents(string $file): bool
{ {
$msp = new MiniSVGParser($file); $msp = new MiniSVGParser($file);

View File

@ -2,14 +2,14 @@
class VideoFileHandler extends DataHandlerExtension class VideoFileHandler extends DataHandlerExtension
{ {
const SUPPORTED_MIME = [ protected $SUPPORTED_MIME = [
'video/webm', 'video/webm',
'video/mp4', 'video/mp4',
'video/ogg', 'video/ogg',
'video/flv', 'video/flv',
'video/x-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) public function onInitExt(InitExtEvent $event)
{ {
@ -28,9 +28,8 @@ class VideoFileHandler extends DataHandlerExtension
$event->panel->add_block($sb); $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->video = true;
$event->image->image = false; $event->image->image = false;
try { try {
@ -80,23 +79,14 @@ class VideoFileHandler extends DataHandlerExtension
// a post with no metadata is better than no post // 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 protected function create_thumb(string $hash, string $type): bool
{ {
return Media::create_thumbnail_ffmpeg($hash); 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 protected function check_contents(string $tmpname): bool
{ {
return in_array(getMimeType($tmpname), self::SUPPORTED_MIME); return in_array(getMimeType($tmpname), $this->SUPPORTED_MIME);
} }
} }