From 0762da54eb691e922b0d8e05883c2e7b565a2fd0 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 9 Jan 2024 01:03:46 +0000 Subject: [PATCH] [core] have ThumbnailGenerationEvent work on an Image --- core/extension.php | 9 ++++----- core/imageboard/event.php | 3 +-- ext/admin/main.php | 2 +- ext/image/main.php | 4 +++- ext/regen_thumb/main.php | 4 ++-- ext/resize/main.php | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/extension.php b/core/extension.php index dbf4ec8a..4fd42e32 100644 --- a/core/extension.php +++ b/core/extension.php @@ -305,7 +305,6 @@ abstract class DataHandlerExtension extends Extension $check_contents = $this->check_contents($event->tmpname); if ($supported_mime && $check_contents) { $this->move_upload_to_archive($event); - send_event(new ThumbnailGenerationEvent($event->hash, $event->mime)); /* Check if we are replacing an image */ if (!is_null($event->replace_id)) { @@ -358,15 +357,15 @@ abstract class DataHandlerExtension extends Extension public function onThumbnailGeneration(ThumbnailGenerationEvent $event) { $result = false; - if ($this->supported_mime($event->mime)) { + if ($this->supported_mime($event->image->get_mime())) { if ($event->force) { - $result = $this->create_thumb($event->hash, $event->mime); + $result = $this->create_thumb($event->image->hash, $event->image->get_mime()); } else { - $outname = warehouse_path(Image::THUMBNAIL_DIR, $event->hash); + $outname = $event->image->get_thumb_filename(); if (file_exists($outname)) { return; } - $result = $this->create_thumb($event->hash, $event->mime); + $result = $this->create_thumb($event->image->hash, $event->image->get_mime()); } } if ($result) { diff --git a/core/imageboard/event.php b/core/imageboard/event.php index d14c0019..34227d05 100644 --- a/core/imageboard/event.php +++ b/core/imageboard/event.php @@ -82,8 +82,7 @@ class ThumbnailGenerationEvent extends Event * Request a thumbnail be made for an image object */ public function __construct( - public string $hash, - public string $mime, + public Image $image, public bool $force = false ) { parent::__construct(); diff --git a/ext/admin/main.php b/ext/admin/main.php index 44b6d503..39c314f1 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -105,7 +105,7 @@ class AdminPage extends Extension $uid = $event->args[0]; $image = Image::by_id_or_hash($uid); if ($image) { - send_event(new ThumbnailGenerationEvent($image->hash, $image->get_mime(), true)); + send_event(new ThumbnailGenerationEvent($image, true)); } else { print("No post with ID '$uid'\n"); } diff --git a/ext/image/main.php b/ext/image/main.php index f475012a..2a024ee8 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -147,6 +147,8 @@ class ImageIO extends Extension // 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); @@ -205,7 +207,7 @@ class ImageIO extends Extension $original->remove_image_only(); // Actually delete the old image file from disk /* Generate new thumbnail */ - send_event(new ThumbnailGenerationEvent($replacement->hash, $replacement->get_mime())); + send_event(new ThumbnailGenerationEvent($replacement)); log_info("image", "Replaced >>{$original->id} with ({$replacement->hash})"); } catch (ImageReplaceException $e) { diff --git a/ext/regen_thumb/main.php b/ext/regen_thumb/main.php index 48a4929c..5067ab71 100644 --- a/ext/regen_thumb/main.php +++ b/ext/regen_thumb/main.php @@ -12,7 +12,7 @@ class RegenThumb extends Extension public function regenerate_thumbnail(Image $image, bool $force = true): bool { global $cache; - $event = send_event(new ThumbnailGenerationEvent($image->hash, $image->get_mime(), $force)); + $event = send_event(new ThumbnailGenerationEvent($image, $force)); $cache->delete("thumb-block:{$image->id}"); return $event->generated; } @@ -125,7 +125,7 @@ class RegenThumb extends Extension continue; } } - $event = send_event(new ThumbnailGenerationEvent($image->hash, $image->mime, $force)); + $event = send_event(new ThumbnailGenerationEvent($image, $force)); if ($event->generated) { $i++; } diff --git a/ext/resize/main.php b/ext/resize/main.php index 0ef4eb11..3db212c2 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -109,7 +109,7 @@ class ResizeImage extends Extension //Need to generate thumbnail again... //This only seems to be an issue if one of the sizes was set to 0. $image_obj = Image::by_id($image_obj->id); //Must be a better way to grab the new hash than setting this again.. - send_event(new ThumbnailGenerationEvent($image_obj->hash, $image_obj->get_mime(), true)); + send_event(new ThumbnailGenerationEvent($image_obj, true)); log_info("resize", ">>{$image_obj->id} has been resized to: ".$width."x".$height); //TODO: Notify user that image has been resized.