2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
2019-11-07 12:49:08 +00:00
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/manager"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *queryResolver) FindScene(ctx context.Context, id *string, checksum *string) (*models.Scene, error) {
|
|
|
|
var scene *models.Scene
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
qb := repo.Scene()
|
|
|
|
var err error
|
|
|
|
if id != nil {
|
|
|
|
idInt, err := strconv.Atoi(*id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scene, err = qb.Find(idInt)
|
|
|
|
} else if checksum != nil {
|
|
|
|
scene, err = qb.FindByChecksum(*checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return scene, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
func (r *queryResolver) FindSceneByHash(ctx context.Context, input models.SceneHashInput) (*models.Scene, error) {
|
|
|
|
var scene *models.Scene
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
qb := repo.Scene()
|
|
|
|
var err error
|
|
|
|
if input.Checksum != nil {
|
|
|
|
scene, err = qb.FindByChecksum(*input.Checksum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if scene == nil && input.Oshash != nil {
|
|
|
|
scene, err = qb.FindByOSHash(*input.Oshash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return scene, nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 03:36:11 +00:00
|
|
|
func (r *queryResolver) FindScenes(ctx context.Context, sceneFilter *models.SceneFilterType, sceneIDs []int, filter *models.FindFilterType) (ret *models.FindScenesResultType, err error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
2021-03-31 03:36:11 +00:00
|
|
|
var scenes []*models.Scene
|
|
|
|
var total int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(sceneIDs) > 0 {
|
|
|
|
scenes, err = repo.Scene().FindMany(sceneIDs)
|
|
|
|
if err == nil {
|
|
|
|
total = len(scenes)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scenes, total, err = repo.Scene().Query(sceneFilter, filter)
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
if err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-31 03:36:11 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
ret = &models.FindScenesResultType{
|
|
|
|
Count: total,
|
|
|
|
Scenes: scenes,
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return ret, nil
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *queryResolver) FindScenesByPathRegex(ctx context.Context, filter *models.FindFilterType) (ret *models.FindScenesResultType, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
2021-02-01 20:57:56 +00:00
|
|
|
|
|
|
|
sceneFilter := &models.SceneFilterType{}
|
|
|
|
|
|
|
|
if filter != nil && filter.Q != nil {
|
|
|
|
sceneFilter.Path = &models.StringCriterionInput{
|
|
|
|
Modifier: models.CriterionModifierMatchesRegex,
|
|
|
|
Value: "(?i)" + *filter.Q,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a copy of the filter if provided, nilling out Q
|
|
|
|
var queryFilter *models.FindFilterType
|
|
|
|
if filter != nil {
|
|
|
|
f := *filter
|
|
|
|
queryFilter = &f
|
|
|
|
queryFilter.Q = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
scenes, total, err := repo.Scene().Query(sceneFilter, queryFilter)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-30 13:37:21 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
ret = &models.FindScenesResultType{
|
|
|
|
Count: total,
|
|
|
|
Scenes: scenes,
|
|
|
|
}
|
2019-10-30 13:37:21 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2019-10-30 13:37:21 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *queryResolver) ParseSceneFilenames(ctx context.Context, filter *models.FindFilterType, config models.SceneParserInput) (ret *models.SceneParserResultType, err error) {
|
2019-11-07 12:49:08 +00:00
|
|
|
parser := manager.NewSceneFilenameParser(filter, config)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
result, count, err := parser.Parse(repo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = &models.SceneParserResultType{
|
|
|
|
Count: count,
|
|
|
|
Results: result,
|
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2019-11-07 12:49:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return ret, nil
|
2019-11-07 12:49:08 +00:00
|
|
|
}
|
2021-04-11 23:04:40 +00:00
|
|
|
|
|
|
|
func (r *queryResolver) FindDuplicateScenes(ctx context.Context, distance *int) (ret [][]*models.Scene, err error) {
|
|
|
|
dist := 0
|
|
|
|
if distance != nil {
|
|
|
|
dist = *distance
|
|
|
|
}
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Scene().FindDuplicates(dist)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|