[core] have extensions react to ImageAdditionEvent, instead of sending ImageAddition and other events in parallel

This commit is contained in:
Shish 2024-01-09 01:30:55 +00:00
parent baf5f38a25
commit 267e176658
5 changed files with 67 additions and 64 deletions

View file

@ -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;
}
}
}

View file

@ -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();
}

View file

@ -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);

View file

@ -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;

View file

@ -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;