diff --git a/core/extension.php b/core/extension.php index 31c41f88..e0450e3f 100644 --- a/core/extension.php +++ b/core/extension.php @@ -301,6 +301,8 @@ abstract class DataHandlerExtension extends Extension public function onDataUpload(DataUploadEvent $event) { + global $config; + if ($this->supported_mime($event->mime)) { if (!$this->check_contents($event->tmpname)) { // We DO support this extension - but the file looks corrupt @@ -324,32 +326,27 @@ abstract class DataHandlerExtension extends Extension } $replacement = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata); - send_event(new ImageReplaceEvent($existing, $replacement)); + send_event(new ImageReplaceEvent($existing, $replacement, $event->metadata)); $event->images[] = $replacement; - if(!empty($event->metadata['source'])) { - send_event(new SourceSetEvent($existing, $event->metadata['source'])); - } } else { $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata); - $iae = send_event(new ImageAdditionEvent($image)); - $event->images[] = $iae->image; - $event->merged = $iae->merged; - if(!empty($event->metadata['tags'])) { - if($iae->merged) { - $event->metadata['tags'] = array_merge($iae->image->get_tag_array(), $event->metadata['tags']); + $existing = Image::by_hash($image->hash); + if (!is_null($existing)) { + $handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER); + if ($handler == ImageConfig::COLLISION_MERGE) { + $image = $existing; + } else { + throw new UploadException(">>{$existing->id} already has hash {$image->hash}"); } - send_event(new TagSetEvent($image, $event->metadata['tags'])); - } - if(!empty($event->metadata['source'])) { - send_event(new SourceSetEvent($image, $event->metadata['source'])); - } - if (!empty($event->metadata['rating'])) { - send_event(new RatingSetEvent($image, $event->metadata['rating'])); - } - if (!empty($event->metadata['locked'])) { - send_event(new LockSetEvent($image, $event->metadata['locked'])); } + + // 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))); + $event->images[] = $iae->image; } } } diff --git a/core/imageboard/event.php b/core/imageboard/event.php index 34227d05..9e2a223d 100644 --- a/core/imageboard/event.php +++ b/core/imageboard/event.php @@ -9,16 +9,16 @@ namespace Shimmie2; */ class ImageAdditionEvent extends Event { - public User $user; - public bool $merged = false; - /** * Inserts a new image into the database with its associated - * information. Also calls TagSetEvent to set the tags for - * this new image. + * information. + * + * @param mixed[] $metadata */ public function __construct( public Image $image, + public array $metadata, + public bool $merged, ) { parent::__construct(); } @@ -58,10 +58,13 @@ class ImageReplaceEvent extends Event * Updates an existing ID in the database to use a new image * file, leaving the tags and such unchanged. Also removes * the old image file and thumbnail from the disk. + * + * @param mixed[] $metadata */ public function __construct( public Image $original, - public Image $replacement + public Image $replacement, + public array $metadata = [], ) { parent::__construct(); } diff --git a/ext/image/main.php b/ext/image/main.php index 2a024ee8..6288dac7 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -122,44 +122,6 @@ class ImageIO extends Extension } } - public function onImageAddition(ImageAdditionEvent $event) - { - global $config; - - try { - $image = $event->image; - - /* - * Check for an existing image - */ - $existing = Image::by_hash($image->hash); - if (!is_null($existing)) { - $handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER); - if ($handler == ImageConfig::COLLISION_MERGE || isset($_GET['update'])) { - $event->merged = true; - $event->image = $existing; - return; - } else { - throw new ImageAdditionException(">>{$existing->id} already has hash {$image->hash}"); - } - } - - // actually insert the info - $image->save_to_db(); - - send_event(new ThumbnailGenerationEvent($image)); - - log_info("image", "Uploaded >>{$image->id} ({$image->hash})"); - } catch (ImageAdditionException $e) { - throw new UploadException($e->error); - } - } - - public function onImageDeletion(ImageDeletionEvent $event) - { - $event->image->delete(); - } - public function onCommand(CommandEvent $event) { if ($event->cmd == "help") { @@ -173,6 +135,12 @@ class ImageIO extends Extension } } + public function onImageAddition(ImageAdditionEvent $event) + { + send_event(new ThumbnailGenerationEvent($event->image)); + log_info("image", "Uploaded >>{$event->image->id} ({$event->image->hash})"); + } + public function onImageReplace(ImageReplaceEvent $event) { $original = $event->original; @@ -215,6 +183,11 @@ class ImageIO extends Extension } } + public function onImageDeletion(ImageDeletionEvent $event) + { + $event->image->delete(); + } + public function onUserPageBuilding(UserPageBuildingEvent $event) { $u_name = url_escape($event->display_user->name); diff --git a/ext/rating/main.php b/ext/rating/main.php index e597cbc2..dc954788 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -164,6 +164,13 @@ class Ratings extends Extension $sb->end_table(); } + public function onImageAddition(ImageAdditionEvent $event) + { + if(!empty($event->metadata['rating'])) { + send_event(new RatingSetEvent($event->image, $event->metadata['rating'])); + } + } + public function onDisplayingImage(DisplayingImageEvent $event) { global $page; diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index 986b5418..7531d389 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -166,6 +166,29 @@ 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'])) { + send_event(new SourceSetEvent($event->image, $event->metadata['source'])); + } + if (!empty($event->metadata['locked'])) { + send_event(new LockSetEvent($event->image, $event->metadata['locked'])); + } + } + + public function onImageReplace(ImageReplaceEvent $event) + { + if(!empty($event->metadata['source'])) { + send_event(new SourceSetEvent($event->replacement, $event->metadata['source'])); + } + } + public function onImageInfoSet(ImageInfoSetEvent $event) { global $page, $user;