mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
Autoassociate galleries to scenes when scanning (#405)
This commit is contained in:
parent
1a6374fae9
commit
9dacad70a1
2 changed files with 89 additions and 2 deletions
|
|
@ -12,6 +12,32 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var extensionsToScan = []string{"zip", "m4v", "mp4", "mov", "wmv", "avi", "mpg", "mpeg", "rmvb", "rm", "flv", "asf", "mkv", "webm"}
|
||||
var extensionsGallery = []string{"zip"}
|
||||
|
||||
func constructGlob() string { // create a sequence for glob doublestar from our extensions
|
||||
extLen := len(extensionsToScan)
|
||||
glb := "{"
|
||||
for i := 0; i < extLen-1; i++ { // append extensions and commas
|
||||
glb += extensionsToScan[i] + ","
|
||||
}
|
||||
if extLen >= 1 { // append last extension without comma
|
||||
glb += extensionsToScan[extLen-1]
|
||||
}
|
||||
glb += "}"
|
||||
return glb
|
||||
|
||||
}
|
||||
|
||||
func isGallery(pathname string) bool {
|
||||
for _, ext := range extensionsGallery {
|
||||
if filepath.Ext(pathname) == "."+ext {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type TaskStatus struct {
|
||||
Status JobStatus
|
||||
Progress float64
|
||||
|
|
@ -67,7 +93,7 @@ func (s *singleton) Scan(useFileMetadata bool) {
|
|||
|
||||
var results []string
|
||||
for _, path := range config.GetStashPaths() {
|
||||
globPath := filepath.Join(path, "**/*.{zip,m4v,mp4,mov,wmv,avi,mpg,mpeg,rmvb,rm,flv,asf,mkv,webm}") // TODO: Make this configurable
|
||||
globPath := filepath.Join(path, "**/*."+constructGlob())
|
||||
globResults, _ := doublestar.Glob(globPath)
|
||||
results = append(results, globResults...)
|
||||
}
|
||||
|
|
@ -96,6 +122,15 @@ func (s *singleton) Scan(useFileMetadata bool) {
|
|||
}
|
||||
|
||||
logger.Info("Finished scan")
|
||||
for _, path := range results {
|
||||
if isGallery(path) {
|
||||
wg.Add(1)
|
||||
task := ScanTask{FilePath: path, UseFileMetadata: false}
|
||||
go task.associateGallery(&wg)
|
||||
wg.Wait()
|
||||
}
|
||||
}
|
||||
logger.Info("Finished gallery association")
|
||||
}()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"database/sql"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ type ScanTask struct {
|
|||
}
|
||||
|
||||
func (t *ScanTask) Start(wg *sync.WaitGroup) {
|
||||
if filepath.Ext(t.FilePath) == ".zip" {
|
||||
if isGallery(t.FilePath) {
|
||||
t.scanGallery()
|
||||
} else {
|
||||
t.scanScene()
|
||||
|
|
@ -60,6 +61,7 @@ func (t *ScanTask) scanGallery() {
|
|||
} else {
|
||||
logger.Infof("%s doesn't exist. Creating new item...", t.FilePath)
|
||||
currentTime := time.Now()
|
||||
|
||||
newGallery := models.Gallery{
|
||||
Checksum: checksum,
|
||||
Path: t.FilePath,
|
||||
|
|
@ -77,6 +79,56 @@ func (t *ScanTask) scanGallery() {
|
|||
}
|
||||
}
|
||||
|
||||
// associates a gallery to a scene with the same basename
|
||||
func (t *ScanTask) associateGallery(wg *sync.WaitGroup) {
|
||||
qb := models.NewGalleryQueryBuilder()
|
||||
gallery, _ := qb.FindByPath(t.FilePath)
|
||||
if gallery == nil {
|
||||
// shouldn't happen , associate is run after scan is finished
|
||||
logger.Errorf("associate: gallery %s not found in DB", t.FilePath)
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
|
||||
if !gallery.SceneID.Valid { // gallery has no SceneID
|
||||
basename := strings.TrimSuffix(t.FilePath, filepath.Ext(t.FilePath))
|
||||
var relatedFiles []string
|
||||
for _, ext := range extensionsToScan { // make a list of media files that can be related to the gallery
|
||||
related := basename + "." + ext
|
||||
if !isGallery(related) { //exclude gallery extensions from the related files
|
||||
relatedFiles = append(relatedFiles, related)
|
||||
}
|
||||
}
|
||||
for _, scenePath := range relatedFiles {
|
||||
qbScene := models.NewSceneQueryBuilder()
|
||||
scene, _ := qbScene.FindByPath(scenePath)
|
||||
if scene != nil { // found related Scene
|
||||
logger.Infof("associate: Gallery %s is related to scene: %d", t.FilePath, scene.ID)
|
||||
|
||||
gallery.SceneID.Int64 = int64(scene.ID)
|
||||
gallery.SceneID.Valid = true
|
||||
|
||||
ctx := context.TODO()
|
||||
tx := database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
_, err := qb.Update(*gallery, tx)
|
||||
if err != nil {
|
||||
logger.Errorf("associate: Error updating gallery sceneId %s", err)
|
||||
_ = tx.Rollback()
|
||||
} else if err := tx.Commit(); err != nil {
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
|
||||
break // since a gallery can have only one related scene
|
||||
// only first found is associated
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
func (t *ScanTask) scanScene() {
|
||||
qb := models.NewSceneQueryBuilder()
|
||||
scene, _ := qb.FindByPath(t.FilePath)
|
||||
|
|
|
|||
Loading…
Reference in a new issue