From 79e01589ca195b9b1840b28a21a2b5544cfaea52 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:38:00 +1100 Subject: [PATCH] Include path and hashes in destroy hook input (#2102) --- pkg/api/resolver_mutation_gallery.go | 11 ++++-- pkg/api/resolver_mutation_image.go | 12 +++++-- pkg/api/resolver_mutation_scene.go | 14 ++++++-- pkg/manager/task_clean.go | 29 +++++++++++++--- pkg/plugin/hooks.go | 34 +++++++++++++++++++ .../components/Changelog/versions/v0120.md | 1 + 6 files changed, 91 insertions(+), 10 deletions(-) diff --git a/pkg/api/resolver_mutation_gallery.go b/pkg/api/resolver_mutation_gallery.go index 0864d3df1..42206b6cd 100644 --- a/pkg/api/resolver_mutation_gallery.go +++ b/pkg/api/resolver_mutation_gallery.go @@ -494,12 +494,19 @@ func (r *mutationResolver) GalleryDestroy(ctx context.Context, input models.Gall // call post hook after performing the other actions for _, gallery := range galleries { - r.hookExecutor.ExecutePostHooks(ctx, gallery.ID, plugin.GalleryDestroyPost, input, nil) + r.hookExecutor.ExecutePostHooks(ctx, gallery.ID, plugin.GalleryDestroyPost, plugin.GalleryDestroyInput{ + GalleryDestroyInput: input, + Checksum: gallery.Checksum, + Path: gallery.Path.String, + }, nil) } // call image destroy post hook as well for _, img := range imgsDestroyed { - r.hookExecutor.ExecutePostHooks(ctx, img.ID, plugin.ImageDestroyPost, nil, nil) + r.hookExecutor.ExecutePostHooks(ctx, img.ID, plugin.ImageDestroyPost, plugin.ImageDestroyInput{ + Checksum: img.Checksum, + Path: img.Path, + }, nil) } return true, nil diff --git a/pkg/api/resolver_mutation_image.go b/pkg/api/resolver_mutation_image.go index baf9eb6b7..cd0b75aa6 100644 --- a/pkg/api/resolver_mutation_image.go +++ b/pkg/api/resolver_mutation_image.go @@ -310,7 +310,11 @@ func (r *mutationResolver) ImageDestroy(ctx context.Context, input models.ImageD fileDeleter.Commit() // call post hook after performing the other actions - r.hookExecutor.ExecutePostHooks(ctx, i.ID, plugin.ImageDestroyPost, input, nil) + r.hookExecutor.ExecutePostHooks(ctx, i.ID, plugin.ImageDestroyPost, plugin.ImageDestroyInput{ + ImageDestroyInput: input, + Checksum: i.Checksum, + Path: i.Path, + }, nil) return true, nil } @@ -358,7 +362,11 @@ func (r *mutationResolver) ImagesDestroy(ctx context.Context, input models.Image for _, image := range images { // call post hook after performing the other actions - r.hookExecutor.ExecutePostHooks(ctx, image.ID, plugin.ImageDestroyPost, input, nil) + r.hookExecutor.ExecutePostHooks(ctx, image.ID, plugin.ImageDestroyPost, plugin.ImagesDestroyInput{ + ImagesDestroyInput: input, + Checksum: image.Checksum, + Path: image.Path, + }, nil) } return true, nil diff --git a/pkg/api/resolver_mutation_scene.go b/pkg/api/resolver_mutation_scene.go index 11d70081c..ff747a2b0 100644 --- a/pkg/api/resolver_mutation_scene.go +++ b/pkg/api/resolver_mutation_scene.go @@ -494,7 +494,12 @@ func (r *mutationResolver) SceneDestroy(ctx context.Context, input models.SceneD fileDeleter.Commit() // call post hook after performing the other actions - r.hookExecutor.ExecutePostHooks(ctx, s.ID, plugin.SceneDestroyPost, input, nil) + r.hookExecutor.ExecutePostHooks(ctx, s.ID, plugin.SceneDestroyPost, plugin.SceneDestroyInput{ + SceneDestroyInput: input, + Checksum: s.Checksum.String, + OSHash: s.OSHash.String, + Path: s.Path, + }, nil) return true, nil } @@ -545,7 +550,12 @@ func (r *mutationResolver) ScenesDestroy(ctx context.Context, input models.Scene for _, scene := range scenes { // call post hook after performing the other actions - r.hookExecutor.ExecutePostHooks(ctx, scene.ID, plugin.SceneDestroyPost, input, nil) + r.hookExecutor.ExecutePostHooks(ctx, scene.ID, plugin.SceneDestroyPost, plugin.ScenesDestroyInput{ + ScenesDestroyInput: input, + Checksum: scene.Checksum.String, + OSHash: scene.OSHash.String, + Path: scene.Path, + }, nil) } return true, nil diff --git a/pkg/manager/task_clean.go b/pkg/manager/task_clean.go index b59e23483..45d6e4590 100644 --- a/pkg/manager/task_clean.go +++ b/pkg/manager/task_clean.go @@ -411,19 +411,35 @@ func (j *cleanJob) deleteScene(ctx context.Context, fileNamingAlgorithm models.H // perform the post-commit actions fileDeleter.Commit() - GetInstance().PluginCache.ExecutePostHooks(ctx, sceneID, plugin.SceneDestroyPost, nil, nil) + GetInstance().PluginCache.ExecutePostHooks(ctx, sceneID, plugin.SceneDestroyPost, plugin.SceneDestroyInput{ + Checksum: s.Checksum.String, + OSHash: s.OSHash.String, + Path: s.Path, + }, nil) } func (j *cleanJob) deleteGallery(ctx context.Context, galleryID int) { + var g *models.Gallery + if err := j.txnManager.WithTxn(context.TODO(), func(repo models.Repository) error { qb := repo.Gallery() + + var err error + g, err = qb.Find(galleryID) + if err != nil { + return err + } + return qb.Destroy(galleryID) }); err != nil { logger.Errorf("Error deleting gallery from database: %s", err.Error()) return } - GetInstance().PluginCache.ExecutePostHooks(ctx, galleryID, plugin.GalleryDestroyPost, nil, nil) + GetInstance().PluginCache.ExecutePostHooks(ctx, galleryID, plugin.GalleryDestroyPost, plugin.GalleryDestroyInput{ + Checksum: g.Checksum, + Path: g.Path.String, + }, nil) } func (j *cleanJob) deleteImage(ctx context.Context, imageID int) { @@ -432,10 +448,12 @@ func (j *cleanJob) deleteImage(ctx context.Context, imageID int) { Paths: GetInstance().Paths, } + var i *models.Image if err := j.txnManager.WithTxn(context.TODO(), func(repo models.Repository) error { qb := repo.Image() - i, err := qb.Find(imageID) + var err error + i, err = qb.Find(imageID) if err != nil { return err } @@ -454,7 +472,10 @@ func (j *cleanJob) deleteImage(ctx context.Context, imageID int) { // perform the post-commit actions fileDeleter.Commit() - GetInstance().PluginCache.ExecutePostHooks(ctx, imageID, plugin.ImageDestroyPost, nil, nil) + GetInstance().PluginCache.ExecutePostHooks(ctx, imageID, plugin.ImageDestroyPost, plugin.ImageDestroyInput{ + Checksum: i.Checksum, + Path: i.Path, + }, nil) } func getStashFromPath(pathToCheck string) *models.StashConfig { diff --git a/pkg/plugin/hooks.go b/pkg/plugin/hooks.go index 6267c994f..f95eb1620 100644 --- a/pkg/plugin/hooks.go +++ b/pkg/plugin/hooks.go @@ -1,6 +1,7 @@ package plugin import ( + "github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/plugin/common" ) @@ -125,3 +126,36 @@ func (e HookTriggerEnum) String() string { func addHookContext(argsMap common.ArgsMap, hookContext common.HookContext) { argsMap[common.HookContextKey] = hookContext } + +// types for destroy hooks, to provide a little more information +type SceneDestroyInput struct { + models.SceneDestroyInput + Checksum string `json:"checksum"` + OSHash string `json:"oshash"` + Path string `json:"path"` +} + +type ScenesDestroyInput struct { + models.ScenesDestroyInput + Checksum string `json:"checksum"` + OSHash string `json:"oshash"` + Path string `json:"path"` +} + +type GalleryDestroyInput struct { + models.GalleryDestroyInput + Checksum string `json:"checksum"` + Path string `json:"path"` +} + +type ImageDestroyInput struct { + models.ImageDestroyInput + Checksum string `json:"checksum"` + Path string `json:"path"` +} + +type ImagesDestroyInput struct { + models.ImagesDestroyInput + Checksum string `json:"checksum"` + Path string `json:"path"` +} diff --git a/ui/v2.5/src/components/Changelog/versions/v0120.md b/ui/v2.5/src/components/Changelog/versions/v0120.md index 875e55025..c12f3640d 100644 --- a/ui/v2.5/src/components/Changelog/versions/v0120.md +++ b/ui/v2.5/src/components/Changelog/versions/v0120.md @@ -5,6 +5,7 @@ * Add forward jump 10 second button to video player. ([#1973](https://github.com/stashapp/stash/pull/1973)) ### 🎨 Improvements +* Include path and hashes in destroy scene/image/gallery post hook input. ([#2102](https://github.com/stashapp/stash/pull/2102/files)) * Rollback operation if files fail to be deleted. ([#1954](https://github.com/stashapp/stash/pull/1954)) * Prefer right-most Studio match in the file path when autotagging. ([#2057](https://github.com/stashapp/stash/pull/2057)) * Added plugin hook for Tag merge operation. ([#2010](https://github.com/stashapp/stash/pull/2010))