Check if scenes are in the library when cleaning (#169)

This commit is contained in:
bnkai
2019-10-30 15:39:44 +02:00
committed by Leopere
parent dfd1245739
commit 1d46cb8a76

View File

@@ -4,8 +4,11 @@ import (
"context" "context"
"github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"os" "os"
"path/filepath"
"strings"
"sync" "sync"
) )
@@ -16,7 +19,7 @@ type CleanTask struct {
func (t *CleanTask) Start(wg *sync.WaitGroup) { func (t *CleanTask) Start(wg *sync.WaitGroup) {
defer wg.Done() 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) logger.Debugf("File Found: %s", t.Scene.Path)
} else { } else {
logger.Infof("File not found. Cleaning: %s", t.Scene.Path) 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()) logger.Infof("Error deleting scene from database: %s", err.Error())
return return
} }
DeleteGeneratedSceneFiles(scene) DeleteGeneratedSceneFiles(scene)
} }
@@ -53,3 +56,20 @@ func (t *CleanTask) fileExists(filename string) bool {
} }
return !info.IsDir() 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
}