2020-10-12 23:12:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
"github.com/99designs/gqlgen/graphql"
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-10-25 00:40:13 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *queryResolver) FindImage(ctx context.Context, id *string, checksum *string) (*models.Image, error) {
|
|
|
|
var image *models.Image
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
qb := repo.Image()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if id != nil {
|
|
|
|
idInt, err := strconv.Atoi(*id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err = qb.Find(idInt)
|
2021-05-25 08:40:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
} else if checksum != nil {
|
|
|
|
image, err = qb.FindByChecksum(*checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return image, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *queryResolver) FindImages(ctx context.Context, imageFilter *models.ImageFilterType, imageIds []int, filter *models.FindFilterType) (ret *models.FindImagesResultType, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
qb := repo.Image()
|
2021-10-25 00:40:13 +00:00
|
|
|
|
|
|
|
fields := graphql.CollectAllFields(ctx)
|
|
|
|
|
|
|
|
result, err := qb.Query(models.ImageQueryOptions{
|
|
|
|
QueryOptions: models.QueryOptions{
|
|
|
|
FindFilter: filter,
|
|
|
|
Count: utils.StrInclude(fields, "count"),
|
|
|
|
},
|
|
|
|
ImageFilter: imageFilter,
|
|
|
|
Megapixels: utils.StrInclude(fields, "megapixels"),
|
|
|
|
TotalSize: utils.StrInclude(fields, "filesize"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := result.Resolve()
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = &models.FindImagesResultType{
|
2021-10-25 00:40:13 +00:00
|
|
|
Count: result.Count,
|
|
|
|
Images: images,
|
|
|
|
Megapixels: result.Megapixels,
|
|
|
|
Filesize: result.TotalSize,
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|