Skip to content

Commit

Permalink
Clean missing galleries (#489)
Browse files Browse the repository at this point in the history
* Clean missing galleries
* Refactor matchFile
  • Loading branch information
WithoutPants authored Apr 24, 2020
1 parent 8a4d853 commit 5923917
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 34 deletions.
13 changes: 3 additions & 10 deletions pkg/manager/exclude_files.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package manager

import (
"github.com/stashapp/stash/pkg/logger"
"regexp"
"strings"

"github.com/stashapp/stash/pkg/logger"
)

func excludeFiles(files []string, patterns []string) ([]string, int) {
Expand Down Expand Up @@ -37,21 +38,13 @@ func excludeFiles(files []string, patterns []string) ([]string, int) {
}

func matchFile(file string, patterns []string) bool {
if patterns == nil {
logger.Infof("No exclude patterns in config.")

} else {
if patterns != nil {
fileRegexps := generateRegexps(patterns)

if len(fileRegexps) == 0 {
return false
}

for _, regPattern := range fileRegexps {
if regPattern.MatchString(strings.ToLower(file)) {
return true
}

}
}

Expand Down
39 changes: 33 additions & 6 deletions pkg/manager/manager_tasks.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package manager

import (
"path/filepath"
"strconv"
"sync"
"time"

"github.com/bmatcuk/doublestar"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
"path/filepath"
"strconv"
"sync"
"time"
)

var extensionsToScan = []string{"zip", "m4v", "mp4", "mov", "wmv", "avi", "mpg", "mpeg", "rmvb", "rm", "flv", "asf", "mkv", "webm"}
Expand Down Expand Up @@ -474,6 +475,7 @@ func (s *singleton) Clean() {
s.Status.indefiniteProgress()

qb := models.NewSceneQueryBuilder()
gqb := models.NewGalleryQueryBuilder()
go func() {
defer s.returnToIdleState()

Expand All @@ -484,14 +486,20 @@ func (s *singleton) Clean() {
return
}

galleries, err := gqb.All()
if err != nil {
logger.Errorf("failed to fetch list of galleries for cleaning")
return
}

if s.Status.stopping {
logger.Info("Stopping due to user request")
return
}

var wg sync.WaitGroup
s.Status.Progress = 0
total := len(scenes)
total := len(scenes) + len(galleries)
for i, scene := range scenes {
s.Status.setProgress(i, total)
if s.Status.stopping {
Expand All @@ -506,7 +514,26 @@ func (s *singleton) Clean() {

wg.Add(1)

task := CleanTask{Scene: *scene}
task := CleanTask{Scene: scene}
go task.Start(&wg)
wg.Wait()
}

for i, gallery := range galleries {
s.Status.setProgress(len(scenes)+i, total)
if s.Status.stopping {
logger.Info("Stopping due to user request")
return
}

if gallery == nil {
logger.Errorf("nil gallery, skipping Clean")
continue
}

wg.Add(1)

task := CleanTask{Gallery: gallery}
go task.Start(&wg)
wg.Wait()
}
Expand Down
65 changes: 49 additions & 16 deletions pkg/manager/task_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,47 @@ package manager

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"

"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"
)

type CleanTask struct {
Scene models.Scene
Scene *models.Scene
Gallery *models.Gallery
}

func (t *CleanTask) Start(wg *sync.WaitGroup) {
defer wg.Done()

if t.fileExists(t.Scene.Path) && t.pathInStash() {
logger.Debugf("File Found: %s", t.Scene.Path)
if matchFile(t.Scene.Path, config.GetExcludes()) {
logger.Infof("File matched regex. Cleaning: \"%s\"", t.Scene.Path)
t.deleteScene(t.Scene.ID)
if t.Scene != nil && t.shouldClean(t.Scene.Path) {
t.deleteScene(t.Scene.ID)
}

if t.Gallery != nil && t.shouldClean(t.Gallery.Path) {
t.deleteGallery(t.Gallery.ID)
}
}

func (t *CleanTask) shouldClean(path string) bool {
if t.fileExists(path) && t.pathInStash(path) {
logger.Debugf("File Found: %s", path)
if matchFile(path, config.GetExcludes()) {
logger.Infof("File matched regex. Cleaning: \"%s\"", path)
return true
}
} else {
logger.Infof("File not found. Cleaning: \"%s\"", t.Scene.Path)
t.deleteScene(t.Scene.ID)
logger.Infof("File not found. Cleaning: \"%s\"", path)
return true
}

return false
}

func (t *CleanTask) deleteScene(sceneID int) {
Expand All @@ -53,6 +67,25 @@ func (t *CleanTask) deleteScene(sceneID int) {
DeleteGeneratedSceneFiles(scene)
}

func (t *CleanTask) deleteGallery(galleryID int) {
ctx := context.TODO()
qb := models.NewGalleryQueryBuilder()
tx := database.DB.MustBeginTx(ctx, nil)

err := qb.Destroy(galleryID, tx)

if err != nil {
logger.Infof("Error deleting gallery from database: %s", err.Error())
tx.Rollback()
return
}

if err := tx.Commit(); err != nil {
logger.Infof("Error deleting gallery from database: %s", err.Error())
return
}
}

func (t *CleanTask) fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
Expand All @@ -61,19 +94,19 @@ func (t *CleanTask) fileExists(filename string) bool {
return !info.IsDir()
}

func (t *CleanTask) pathInStash() bool {
func (t *CleanTask) pathInStash(pathToCheck string) bool {
for _, path := range config.GetStashPaths() {

rel, error := filepath.Rel(path, filepath.Dir(t.Scene.Path))
rel, error := filepath.Rel(path, filepath.Dir(pathToCheck))

if error == nil {
if !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
logger.Debugf("File %s belongs to stash path %s", t.Scene.Path, path)
logger.Debugf("File %s belongs to stash path %s", pathToCheck, path)
return true
}
}

}
logger.Debugf("File %s is out from stash path", t.Scene.Path)
logger.Debugf("File %s is out from stash path", pathToCheck)
return false
}
5 changes: 5 additions & 0 deletions pkg/models/querybuilder_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"database/sql"
"path/filepath"
"strconv"

"github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database"
Expand Down Expand Up @@ -51,6 +52,10 @@ func (qb *GalleryQueryBuilder) Update(updatedGallery Gallery, tx *sqlx.Tx) (*Gal
return &updatedGallery, nil
}

func (qb *GalleryQueryBuilder) Destroy(id int, tx *sqlx.Tx) error {
return executeDeleteQuery("galleries", strconv.Itoa(id), tx)
}

type GalleryNullSceneID struct {
SceneID sql.NullInt64
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export const SettingsTasksPanel: React.FC = () => {
cancel={{ onClick: () => setIsCleanAlertOpen(false) }}
>
<p>
Are you sure you want to Clean? This will delete db information and
generated content for all scenes that are no longer found in the
Are you sure you want to Clean? This will delete database information and
generated content for all scenes and galleries that are no longer found in the
filesystem.
</p>
</Modal>
Expand Down

0 comments on commit 5923917

Please sign in to comment.