From fd7c774f5bd550ab377a316efea30cee1a4205c3 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 8 Feb 2020 13:35:53 +0000 Subject: [PATCH] handle_svg doesn't need to override ALL of onDataUpload --- core/extension.php | 14 +++++++++++++- core/imageboard/misc.php | 19 ------------------- ext/handle_svg/main.php | 26 ++++++-------------------- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/core/extension.php b/core/extension.php index 158b338b..f302403e 100644 --- a/core/extension.php +++ b/core/extension.php @@ -347,12 +347,24 @@ abstract class FormatterExtension extends Extension */ abstract class DataHandlerExtension extends Extension { + protected function move_upload_to_archive(DataUploadEvent $event) + { + $target = warehouse_path(Image::IMAGE_DIR, $event->hash); + if (!@copy($event->tmpname, $target)) { + $errors = error_get_last(); + throw new UploadException( + "Failed to copy file from uploads ({$event->tmpname}) to archive ($target): ". + "{$errors['type']} / {$errors['message']}" + ); + } + } + public function onDataUpload(DataUploadEvent $event) { $supported_ext = $this->supported_ext($event->type); $check_contents = $this->check_contents($event->tmpname); if ($supported_ext && $check_contents) { - move_upload_to_archive($event); + $this->move_upload_to_archive($event); send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); /* Check if we are replacing an image */ diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 2cfcfa44..92922859 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -3,25 +3,6 @@ * Misc functions * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -/** - * Move a file from PHP's temporary area into shimmie's image storage - * hierarchy, or throw an exception trying. - * - * @param DataUploadEvent $event - * @throws UploadException - */ -function move_upload_to_archive(DataUploadEvent $event): void -{ - $target = warehouse_path(Image::IMAGE_DIR, $event->hash); - if (!@copy($event->tmpname, $target)) { - $errors = error_get_last(); - throw new UploadException( - "Failed to copy file from uploads ({$event->tmpname}) to archive ($target): ". - "{$errors['type']} / {$errors['message']}" - ); - } -} - /** * Add a directory full of images * diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 73dee3d6..fc1c675d 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -23,27 +23,13 @@ class SVGFileHandler extends DataHandlerExtension } } - public function onDataUpload(DataUploadEvent $event) + protected function move_upload_to_archive(DataUploadEvent $event) { - if ($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { - $hash = $event->hash; - - $sanitizer = new Sanitizer(); - $sanitizer->removeRemoteReferences(true); - $dirtySVG = file_get_contents($event->tmpname); - $cleanSVG = $sanitizer->sanitize($dirtySVG); - file_put_contents(warehouse_path(Image::IMAGE_DIR, $hash), $cleanSVG); - - send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $hash), $event->metadata); - if (is_null($image)) { - throw new UploadException("SVG handler failed to create image object from data"); - } - $iae = new ImageAdditionEvent($image); - send_event($iae); - $event->image_id = $iae->image->id; - $event->merged = $iae->merged; - } + $sanitizer = new Sanitizer(); + $sanitizer->removeRemoteReferences(true); + $dirtySVG = file_get_contents($event->tmpname); + $cleanSVG = $sanitizer->sanitize($dirtySVG); + file_put_contents(warehouse_path(Image::IMAGE_DIR, $event->hash), $cleanSVG); } protected function create_thumb(string $hash, string $type): bool