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) { $event->image->lossless = true;
case "swf": $event->image->video = true;
$event->image->lossless = true; $event->image->image = false;
$event->image->video = true;
$info = getimagesize($event->file_name); $info = getimagesize($event->file_name);
if (!$info) { if ($info) {
return null; $event->image->width = $info[0];
} $event->image->height = $info[1];
$event->image->image = false;
$event->image->width = $info[0];
$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,42 +2,27 @@
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; $event->image->image = ($event->ext!="ani");
$event->image->image = ($event->ext!="ani");
$fp = fopen($event->file_name, "r"); $fp = fopen($event->file_name, "r");
try { try {
unpack("Snull/Stype/Scount", fread($fp, 6)); unpack("Snull/Stype/Scount", fread($fp, 6));
$subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16)); $subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16));
} finally { } finally {
fclose($fp); fclose($fp);
}
$width = $subheader['width'];
$height = $subheader['height'];
$event->image->width = $width == 0 ? 256 : $width;
$event->image->height = $height == 0 ? 256 : $height;
} }
}
protected function supported_ext(string $ext): bool $width = $subheader['width'];
{ $height = $subheader['height'];
return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); $event->image->width = $width == 0 ? 256 : $width;
} $event->image->height = $height == 0 ? 256 : $height;
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) { $event->image->audio = true;
case "mp3": $event->image->video = false;
$event->image->audio = true; $event->image->lossless = false;
$event->image->video = false; $event->image->image = false;
$event->image->lossless = false; $event->image->width = 0;
$event->image->image = false; $event->image->height = 0;
$event->image->width = 0; // TODO: ->length = ???
$event->image->height = 0;
break;
}
// 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,40 +12,30 @@ 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; }
} $event->image->audio = false;
$event->image->audio = false; switch ($event->ext) {
switch ($event->ext) { case "gif":
case "gif": $event->image->video = Media::is_animated_gif($event->file_name);
$event->image->video = Media::is_animated_gif($event->file_name); break;
break; case "webp":
case "webp": $event->image->video = Media::is_animated_webp($event->file_name);
$event->image->video = Media::is_animated_webp($event->file_name); break;
break; default:
default: $event->image->video = false;
$event->image->video = false; break;
break; }
} $event->image->image = !$event->image->video;
$event->image->image = !$event->image->video;
$info = getimagesize($event->file_name);
if (!$info) {
return null;
}
$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];
} }
} }
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,26 +3,42 @@ 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)
{ {
switch ($event->ext) { global $page;
case "svg": if ($event->page_matches("get_svg")) {
$event->image->lossless = true; $id = int_escape($event->get_arg(0));
$event->image->video = false; $image = Image::by_id($id);
$event->image->audio = false; $hash = $image->hash;
$event->image->image = true;
$msp = new MiniSVGParser($event->file_name); $page->set_type("image/svg+xml");
$event->image->width = $msp->width; $page->set_mode(PageMode::DATA);
$event->image->height = $msp->height;
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) protected function move_upload_to_archive(DataUploadEvent $event)
{ {
$sanitizer = new Sanitizer(); $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 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,75 +28,65 @@ 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 { $data = Media::get_ffprobe_data($event->file_name);
$data = Media::get_ffprobe_data($event->file_name);
if (is_array($data)) { if (is_array($data)) {
if (array_key_exists("streams", $data)) { if (array_key_exists("streams", $data)) {
$video = false; $video = false;
$audio = true; $audio = true;
$streams = $data["streams"]; $streams = $data["streams"];
if (is_array($streams)) { if (is_array($streams)) {
foreach ($streams as $stream) { foreach ($streams as $stream) {
if (is_array($stream)) { if (is_array($stream)) {
if (array_key_exists("codec_type", $stream)) { if (array_key_exists("codec_type", $stream)) {
$type = $stream["codec_type"]; $type = $stream["codec_type"];
switch ($type) { switch ($type) {
case "audio": case "audio":
$audio = true; $audio = true;
break; break;
case "video": case "video":
$video = true; $video = true;
break; 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 (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 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);
} }
} }