2020-10-12 23:12:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-25 00:56:34 +00:00
|
|
|
"time"
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/api/urlbuilders"
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/image"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *imageResolver) Title(ctx context.Context, obj *models.Image) (*string, error) {
|
|
|
|
ret := image.GetTitle(obj)
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageResolver) Rating(ctx context.Context, obj *models.Image) (*int, error) {
|
|
|
|
if obj.Rating.Valid {
|
|
|
|
rating := int(obj.Rating.Int64)
|
|
|
|
return &rating, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageResolver) File(ctx context.Context, obj *models.Image) (*models.ImageFileType, error) {
|
|
|
|
width := int(obj.Width.Int64)
|
|
|
|
height := int(obj.Height.Int64)
|
|
|
|
size := int(obj.Size.Int64)
|
|
|
|
return &models.ImageFileType{
|
|
|
|
Size: &size,
|
|
|
|
Width: &width,
|
|
|
|
Height: &height,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageResolver) Paths(ctx context.Context, obj *models.Image) (*models.ImagePathsType, error) {
|
|
|
|
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
|
2021-03-13 00:49:20 +00:00
|
|
|
builder := urlbuilders.NewImageURLBuilder(baseURL, obj)
|
2020-10-12 23:12:46 +00:00
|
|
|
thumbnailPath := builder.GetThumbnailURL()
|
|
|
|
imagePath := builder.GetImageURL()
|
|
|
|
return &models.ImagePathsType{
|
|
|
|
Image: &imagePath,
|
|
|
|
Thumbnail: &thumbnailPath,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *imageResolver) Galleries(ctx context.Context, obj *models.Image) (ret []*models.Gallery, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
ret, err = repo.Gallery().FindByImageID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *imageResolver) Studio(ctx context.Context, obj *models.Image) (ret *models.Studio, err error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
if !obj.StudioID.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Studio().Find(int(obj.StudioID.Int64))
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *imageResolver) Tags(ctx context.Context, obj *models.Image) (ret []*models.Tag, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Tag().FindByImageID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *imageResolver) Performers(ctx context.Context, obj *models.Image) (ret []*models.Performer, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Performer().FindByImageID(obj.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2021-05-25 00:56:34 +00:00
|
|
|
|
|
|
|
func (r *imageResolver) CreatedAt(ctx context.Context, obj *models.Image) (*time.Time, error) {
|
|
|
|
return &obj.CreatedAt.Timestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageResolver) UpdatedAt(ctx context.Context, obj *models.Image) (*time.Time, error) {
|
|
|
|
return &obj.UpdatedAt.Timestamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *imageResolver) FileModTime(ctx context.Context, obj *models.Image) (*time.Time, error) {
|
|
|
|
return &obj.FileModTime.Timestamp, nil
|
|
|
|
}
|