From 03d40451175d0a8112be7f7a529399b04749c08a Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 9 Jan 2024 04:07:25 +0000 Subject: [PATCH] [core] also simplify uploading by having merge handled as a special case --- core/extension.php | 70 +++++++++++++++++++++------------------ core/imageboard/event.php | 1 - ext/handle_svg/main.php | 6 ++-- ext/tag_edit/main.php | 3 -- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/core/extension.php b/core/extension.php index 13822a83..0db1f8af 100644 --- a/core/extension.php +++ b/core/extension.php @@ -287,7 +287,7 @@ abstract class DataHandlerExtension extends Extension { protected array $SUPPORTED_MIME = []; - protected function move_upload_to_archive(DataUploadEvent $event) + protected function move_upload_to_archive(DataUploadEvent $event): string { $target = warehouse_path(Image::IMAGE_DIR, $event->hash); if (!@copy($event->tmpname, $target)) { @@ -297,6 +297,7 @@ abstract class DataHandlerExtension extends Extension "{$errors['type']} / {$errors['message']}" ); } + return $target; } public function onDataUpload(DataUploadEvent $event) @@ -309,24 +310,50 @@ abstract class DataHandlerExtension extends Extension throw new UploadException("Invalid or corrupted file"); } - $this->move_upload_to_archive($event); - $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata); - - $existing = Image::by_hash($image->hash); + $existing = Image::by_hash(md5_file($event->tmpname)); if (!is_null($existing)) { - $handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER); - if ($handler == ImageConfig::COLLISION_MERGE) { - $image = $existing; + if ($config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER) == ImageConfig::COLLISION_MERGE) { + // Right now tags are the only thing that get merged, so + // we can just send a TagSetEvent - in the future we might + // want a dedicated MergeEvent? + if(!empty($event->metadata['tags'])) { + send_event(new TagSetEvent($existing, array_merge($existing->get_tag_array(), $event->metadata['tags']))); + } + $event->images[] = $existing; + return; } else { - throw new UploadException(">>{$existing->id} already has hash {$image->hash}"); + throw new UploadException(">>{$existing->id} already has hash {$existing->hash}"); } } + $filename = $event->tmpname; + // FIXME: this should happen after ImageAdditionEvent, but the thumbnail + // code assumes the file is in the archive already instead of using + // the image->tmp_file field + $filename = $this->move_upload_to_archive($event); + + assert(is_readable($filename)); + $image = new Image(); + $image->tmp_file = $filename; + $image->filesize = filesize($filename); + $image->hash = md5_file($filename); + $image->filename = (($pos = strpos($event->metadata['filename'], '?')) !== false) ? substr($event->metadata['filename'], 0, $pos) : $event->metadata['filename']; + $image->set_mime(MimeType::get_for_file($filename, get_file_ext($event->metadata["filename"]) ?? null)); + if (empty($image->get_mime())) { + throw new UploadException("Unable to determine MIME for $filename"); + } + try { + send_event(new MediaCheckPropertiesEvent($image)); + } catch (MediaException $e) { + throw new UploadException("Unable to scan media properties $filename / $image->filename / $image->hash: ".$e->getMessage()); + } + // ensure $image has a database-assigned ID number // before anything else happens $image->save_to_db(); - $iae = send_event(new ImageAdditionEvent($image, $event->metadata, !is_null($existing))); + $iae = send_event(new ImageAdditionEvent($image, $event->metadata)); + $event->images[] = $iae->image; } } @@ -369,29 +396,6 @@ abstract class DataHandlerExtension extends Extension } } - protected function create_image_from_data(string $filename, array $metadata): Image - { - $image = new Image(); - - assert(is_readable($filename)); - $image->tmp_file = $filename; - $image->filesize = filesize($filename); - $image->hash = md5_file($filename); - $image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; - $image->set_mime(MimeType::get_for_file($filename, get_file_ext($metadata["filename"]) ?? null)); - - if (empty($image->get_mime())) { - throw new UploadException("Unable to determine MIME for $filename"); - } - try { - send_event(new MediaCheckPropertiesEvent($image)); - } catch (MediaException $e) { - throw new UploadException("Unable to scan media properties $filename / $image->filename / $image->hash: ".$e->getMessage()); - } - - return $image; - } - 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 $mime): bool; diff --git a/core/imageboard/event.php b/core/imageboard/event.php index 27e764f2..9b34d598 100644 --- a/core/imageboard/event.php +++ b/core/imageboard/event.php @@ -18,7 +18,6 @@ class ImageAdditionEvent extends Event public function __construct( public Image $image, public array $metadata, - public bool $merged, ) { parent::__construct(); } diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 7484e316..448fb73e 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -44,14 +44,16 @@ class SVGFileHandler extends DataHandlerExtension $event->image->height = $msp->height; } - protected function move_upload_to_archive(DataUploadEvent $event) + protected function move_upload_to_archive(DataUploadEvent $event): string { $sanitizer = new Sanitizer(); $sanitizer->removeRemoteReferences(true); $dirtySVG = file_get_contents($event->tmpname); $cleanSVG = $sanitizer->sanitize($dirtySVG); $event->hash = md5($cleanSVG); - file_put_contents(warehouse_path(Image::IMAGE_DIR, $event->hash), $cleanSVG); + $filename = warehouse_path(Image::IMAGE_DIR, $event->hash); + file_put_contents($filename, $cleanSVG); + return $filename; } protected function create_thumb(string $hash, string $mime): bool diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index 6f0e3f2e..c7180059 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -169,9 +169,6 @@ class TagEdit extends Extension public function onImageAddition(ImageAdditionEvent $event) { if(!empty($event->metadata['tags'])) { - if($event->merged) { - $event->metadata['tags'] = array_merge($event->image->get_tag_array(), $event->metadata['tags']); - } send_event(new TagSetEvent($event->image, $event->metadata['tags'])); } if(!empty($event->metadata['source'])) {