2020-10-12 23:12:46 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-08-12 02:21:46 +00:00
|
|
|
"context"
|
2022-09-01 07:54:34 +00:00
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2022-09-02 01:18:37 +00:00
|
|
|
"strconv"
|
2021-10-14 23:39:48 +00:00
|
|
|
"time"
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/file"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Image stores the metadata for a single image.
|
|
|
|
type Image struct {
|
2022-07-13 06:30:54 +00:00
|
|
|
ID int `json:"id"`
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
Title string `json:"title"`
|
|
|
|
Rating *int `json:"rating"`
|
|
|
|
Organized bool `json:"organized"`
|
|
|
|
OCounter int `json:"o_counter"`
|
|
|
|
StudioID *int `json:"studio_id"`
|
|
|
|
|
|
|
|
// transient - not persisted
|
2022-09-01 07:54:34 +00:00
|
|
|
Files RelatedImageFiles
|
|
|
|
PrimaryFileID *file.ID
|
|
|
|
// transient - path of primary file - empty if no files
|
|
|
|
Path string
|
|
|
|
// transient - checksum of primary file - empty if no files
|
|
|
|
Checksum string
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
GalleryIDs RelatedIDs `json:"gallery_ids"`
|
|
|
|
TagIDs RelatedIDs `json:"tag_ids"`
|
|
|
|
PerformerIDs RelatedIDs `json:"performer_ids"`
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
func (i *Image) LoadFiles(ctx context.Context, l ImageFileLoader) error {
|
|
|
|
return i.Files.load(func() ([]*file.ImageFile, error) {
|
|
|
|
return l.GetFiles(ctx, i.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) LoadPrimaryFile(ctx context.Context, l file.Finder) error {
|
|
|
|
return i.Files.loadPrimary(func() (*file.ImageFile, error) {
|
|
|
|
if i.PrimaryFileID == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := l.Find(ctx, *i.PrimaryFileID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vf *file.ImageFile
|
|
|
|
if len(f) > 0 {
|
|
|
|
var ok bool
|
|
|
|
vf, ok = f[0].(*file.ImageFile)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("not an image file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vf, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
func (i *Image) LoadGalleryIDs(ctx context.Context, l GalleryIDLoader) error {
|
|
|
|
return i.GalleryIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetGalleryIDs(ctx, i.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) LoadPerformerIDs(ctx context.Context, l PerformerIDLoader) error {
|
|
|
|
return i.PerformerIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetPerformerIDs(ctx, i.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) LoadTagIDs(ctx context.Context, l TagIDLoader) error {
|
|
|
|
return i.TagIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetTagIDs(ctx, i.ID)
|
|
|
|
})
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
// GetTitle returns the title of the image. If the Title field is empty,
|
|
|
|
// then the base filename is returned.
|
2022-07-13 06:30:54 +00:00
|
|
|
func (i Image) GetTitle() string {
|
|
|
|
if i.Title != "" {
|
|
|
|
return i.Title
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
if i.Path != "" {
|
|
|
|
return filepath.Base(i.Path)
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-02 01:18:37 +00:00
|
|
|
// DisplayName returns a display name for the scene for logging purposes.
|
|
|
|
// It returns Path if not empty, otherwise it returns the ID.
|
|
|
|
func (i Image) DisplayName() string {
|
|
|
|
if i.Path != "" {
|
|
|
|
return i.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Itoa(i.ID)
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
type ImageCreateInput struct {
|
|
|
|
*Image
|
|
|
|
FileIDs []file.ID
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
type ImagePartial struct {
|
|
|
|
Title OptionalString
|
|
|
|
Rating OptionalInt
|
|
|
|
Organized OptionalBool
|
|
|
|
OCounter OptionalInt
|
|
|
|
StudioID OptionalInt
|
|
|
|
CreatedAt OptionalTime
|
|
|
|
UpdatedAt OptionalTime
|
|
|
|
|
2022-09-05 01:46:18 +00:00
|
|
|
GalleryIDs *UpdateIDs
|
|
|
|
TagIDs *UpdateIDs
|
|
|
|
PerformerIDs *UpdateIDs
|
|
|
|
PrimaryFileID *file.ID
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImagePartial() ImagePartial {
|
|
|
|
updatedTime := time.Now()
|
|
|
|
return ImagePartial{
|
|
|
|
UpdatedAt: NewOptionalTime(updatedTime),
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
type Images []*Image
|
|
|
|
|
|
|
|
func (i *Images) Append(o interface{}) {
|
|
|
|
*i = append(*i, o.(*Image))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Images) New() interface{} {
|
|
|
|
return &Image{}
|
|
|
|
}
|