stash/pkg/manager/task_generate_phash.go
SmallCoccinelle 4a0c4c4847
Reorder waitgroup completion (#1748)
Rather than passing a pointer to a waitgroup into task.Start(..)
functions, handle the waitgroup.Done() at the callsite.

This makes waitgroup handling local to its definition rather than it
being spread out over multiple files. Tasks now simply execute, and
the policy of waiting on them is handled by the caller.
2021-09-22 13:22:59 +10:00

59 lines
1.4 KiB
Go

package manager
import (
"context"
"database/sql"
"github.com/stashapp/stash/pkg/ffmpeg"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
)
type GeneratePhashTask struct {
Scene models.Scene
Overwrite bool
fileNamingAlgorithm models.HashAlgorithm
txnManager models.TransactionManager
}
func (t *GeneratePhashTask) Start() {
if !t.shouldGenerate() {
return
}
videoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.Scene.Path, false)
if err != nil {
logger.Errorf("error reading video file: %s", err.Error())
return
}
sceneHash := t.Scene.GetHash(t.fileNamingAlgorithm)
generator, err := NewPhashGenerator(*videoFile, sceneHash)
if err != nil {
logger.Errorf("error creating phash generator: %s", err.Error())
return
}
hash, err := generator.Generate()
if err != nil {
logger.Errorf("error generating phash: %s", err.Error())
return
}
if err := t.txnManager.WithTxn(context.TODO(), func(r models.Repository) error {
qb := r.Scene()
hashValue := sql.NullInt64{Int64: int64(*hash), Valid: true}
scenePartial := models.ScenePartial{
ID: t.Scene.ID,
Phash: &hashValue,
}
_, err := qb.Update(scenePartial)
return err
}); err != nil {
logger.Error(err.Error())
}
}
func (t *GeneratePhashTask) shouldGenerate() bool {
return t.Overwrite || !t.Scene.Phash.Valid
}