From 1d46cb8a764192e455d1d262954da8587480b3f5 Mon Sep 17 00:00:00 2001 From: bnkai <48220860+bnkai@users.noreply.github.com> Date: Wed, 30 Oct 2019 15:39:44 +0200 Subject: [PATCH] Check if scenes are in the library when cleaning (#169) --- pkg/manager/task_clean.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/manager/task_clean.go b/pkg/manager/task_clean.go index 1f4c0ae9f..bcc71b424 100644 --- a/pkg/manager/task_clean.go +++ b/pkg/manager/task_clean.go @@ -4,8 +4,11 @@ import ( "context" "github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/logger" + "github.com/stashapp/stash/pkg/manager/config" "github.com/stashapp/stash/pkg/models" "os" + "path/filepath" + "strings" "sync" ) @@ -16,7 +19,7 @@ type CleanTask struct { func (t *CleanTask) Start(wg *sync.WaitGroup) { defer wg.Done() - if t.fileExists(t.Scene.Path) { + if t.fileExists(t.Scene.Path) && t.pathInStash() { logger.Debugf("File Found: %s", t.Scene.Path) } else { logger.Infof("File not found. Cleaning: %s", t.Scene.Path) @@ -42,7 +45,7 @@ func (t *CleanTask) deleteScene(sceneID int) { logger.Infof("Error deleting scene from database: %s", err.Error()) return } - + DeleteGeneratedSceneFiles(scene) } @@ -53,3 +56,20 @@ func (t *CleanTask) fileExists(filename string) bool { } return !info.IsDir() } + +func (t *CleanTask) pathInStash() bool { + for _, path := range config.GetStashPaths() { + + rel, error := filepath.Rel(path, filepath.Dir(t.Scene.Path)) + + if error == nil { + if !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + logger.Debugf("File %s belongs to stash path %s", t.Scene.Path, path) + return true + } + } + + } + logger.Debugf("File %s is out from stash path", t.Scene.Path) + return false +}