mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Images section (#813)
* Add new configuration options * Refactor scan/clean * Schema changes * Add details to galleries * Remove redundant code * Refine thumbnail generation * Gallery overhaul * Don't allow modifying zip gallery images * Show gallery card overlays * Hide zoom slider when not in grid mode
This commit is contained in:
@@ -8,38 +8,40 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/stashapp/stash/pkg/database"
|
||||
"github.com/stashapp/stash/pkg/image"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
"github.com/stashapp/stash/pkg/manager/config"
|
||||
"github.com/stashapp/stash/pkg/manager/paths"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
)
|
||||
|
||||
type CleanTask struct {
|
||||
Scene *models.Scene
|
||||
Gallery *models.Gallery
|
||||
Image *models.Image
|
||||
fileNamingAlgorithm models.HashAlgorithm
|
||||
}
|
||||
|
||||
func (t *CleanTask) Start(wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
if t.Scene != nil && t.shouldClean(t.Scene.Path) {
|
||||
if t.Scene != nil && t.shouldCleanScene(t.Scene) {
|
||||
t.deleteScene(t.Scene.ID)
|
||||
}
|
||||
|
||||
if t.Gallery != nil && t.shouldCleanGallery(t.Gallery) {
|
||||
t.deleteGallery(t.Gallery.ID)
|
||||
}
|
||||
|
||||
if t.Image != nil && t.shouldCleanImage(t.Image) {
|
||||
t.deleteImage(t.Image.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *CleanTask) shouldClean(path string) bool {
|
||||
fileExists, err := t.fileExists(path)
|
||||
if err != nil {
|
||||
logger.Errorf("Error checking existence of %s: %s", path, err.Error())
|
||||
return false
|
||||
}
|
||||
// use image.FileExists for zip file checking
|
||||
fileExists := image.FileExists(path)
|
||||
|
||||
if fileExists && t.pathInStash(path) {
|
||||
if fileExists && t.getStashFromPath(path) != nil {
|
||||
logger.Debugf("File Found: %s", path)
|
||||
if matchFile(path, config.GetExcludes()) {
|
||||
logger.Infof("File matched regex. Cleaning: \"%s\"", path)
|
||||
@@ -53,13 +55,68 @@ func (t *CleanTask) shouldClean(path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *CleanTask) shouldCleanGallery(g *models.Gallery) bool {
|
||||
if t.shouldClean(g.Path) {
|
||||
func (t *CleanTask) shouldCleanScene(s *models.Scene) bool {
|
||||
if t.shouldClean(s.Path) {
|
||||
return true
|
||||
}
|
||||
|
||||
if t.Gallery.CountFiles() == 0 {
|
||||
logger.Infof("Gallery has 0 images. Cleaning: \"%s\"", g.Path)
|
||||
stash := t.getStashFromPath(s.Path)
|
||||
if stash.ExcludeVideo {
|
||||
logger.Infof("File in stash library that excludes video. Cleaning: \"%s\"", s.Path)
|
||||
return true
|
||||
}
|
||||
|
||||
if !matchExtension(s.Path, config.GetVideoExtensions()) {
|
||||
logger.Infof("File extension does not match video extensions. Cleaning: \"%s\"", s.Path)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *CleanTask) shouldCleanGallery(g *models.Gallery) bool {
|
||||
// never clean manually created galleries
|
||||
if !g.Zip {
|
||||
return false
|
||||
}
|
||||
|
||||
path := g.Path.String
|
||||
if t.shouldClean(path) {
|
||||
return true
|
||||
}
|
||||
|
||||
stash := t.getStashFromPath(path)
|
||||
if stash.ExcludeImage {
|
||||
logger.Infof("File in stash library that excludes images. Cleaning: \"%s\"", path)
|
||||
return true
|
||||
}
|
||||
|
||||
if !matchExtension(path, config.GetGalleryExtensions()) {
|
||||
logger.Infof("File extension does not match gallery extensions. Cleaning: \"%s\"", path)
|
||||
return true
|
||||
}
|
||||
|
||||
if countImagesInZip(path) == 0 {
|
||||
logger.Infof("Gallery has 0 images. Cleaning: \"%s\"", path)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *CleanTask) shouldCleanImage(s *models.Image) bool {
|
||||
if t.shouldClean(s.Path) {
|
||||
return true
|
||||
}
|
||||
|
||||
stash := t.getStashFromPath(s.Path)
|
||||
if stash.ExcludeImage {
|
||||
logger.Infof("File in stash library that excludes images. Cleaning: \"%s\"", s.Path)
|
||||
return true
|
||||
}
|
||||
|
||||
if !matchExtension(s.Path, config.GetImageExtensions()) {
|
||||
logger.Infof("File extension does not match image extensions. Cleaning: \"%s\"", s.Path)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -105,10 +162,29 @@ func (t *CleanTask) deleteGallery(galleryID int) {
|
||||
logger.Errorf("Error deleting gallery from database: %s", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
pathErr := os.RemoveAll(paths.GetGthumbDir(t.Gallery.Checksum)) // remove cache dir of gallery
|
||||
func (t *CleanTask) deleteImage(imageID int) {
|
||||
ctx := context.TODO()
|
||||
qb := models.NewImageQueryBuilder()
|
||||
tx := database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
err := qb.Destroy(imageID, tx)
|
||||
|
||||
if err != nil {
|
||||
logger.Errorf("Error deleting image from database: %s", err.Error())
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
logger.Errorf("Error deleting image from database: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
pathErr := os.Remove(GetInstance().Paths.Generated.GetThumbnailPath(t.Image.Checksum, models.DefaultGthumbWidth)) // remove cache dir of gallery
|
||||
if pathErr != nil {
|
||||
logger.Errorf("Error deleting gallery directory from cache: %s", pathErr)
|
||||
logger.Errorf("Error deleting thumbnail image from cache: %s", pathErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,19 +202,17 @@ func (t *CleanTask) fileExists(filename string) (bool, error) {
|
||||
return !info.IsDir(), nil
|
||||
}
|
||||
|
||||
func (t *CleanTask) pathInStash(pathToCheck string) bool {
|
||||
for _, path := range config.GetStashPaths() {
|
||||
func (t *CleanTask) getStashFromPath(pathToCheck string) *models.StashConfig {
|
||||
for _, s := range config.GetStashPaths() {
|
||||
|
||||
rel, error := filepath.Rel(path, filepath.Dir(pathToCheck))
|
||||
rel, error := filepath.Rel(s.Path, filepath.Dir(pathToCheck))
|
||||
|
||||
if error == nil {
|
||||
if !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
logger.Debugf("File %s belongs to stash path %s", pathToCheck, path)
|
||||
return true
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
logger.Debugf("File %s is out from stash path", pathToCheck)
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user