2019-02-09 12:30:49 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-08-12 02:21:46 +00:00
|
|
|
"context"
|
2019-12-01 16:18:44 +00:00
|
|
|
"path/filepath"
|
2021-10-28 03:25:17 +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"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// Scene stores the metadata for a single video scene.
|
2019-02-09 12:30:49 +00:00
|
|
|
type Scene struct {
|
2022-07-13 06:30:54 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Details string `json:"details"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Date *Date `json:"date"`
|
|
|
|
Rating *int `json:"rating"`
|
|
|
|
Organized bool `json:"organized"`
|
|
|
|
OCounter int `json:"o_counter"`
|
|
|
|
StudioID *int `json:"studio_id"`
|
|
|
|
|
|
|
|
// transient - not persisted
|
|
|
|
Files []*file.VideoFile
|
|
|
|
|
|
|
|
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"`
|
|
|
|
Movies RelatedMovies `json:"movies"`
|
|
|
|
StashIDs RelatedStashIDs `json:"stash_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadGalleryIDs(ctx context.Context, l GalleryIDLoader) error {
|
|
|
|
return s.GalleryIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetGalleryIDs(ctx, s.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadPerformerIDs(ctx context.Context, l PerformerIDLoader) error {
|
|
|
|
return s.PerformerIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetPerformerIDs(ctx, s.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadTagIDs(ctx context.Context, l TagIDLoader) error {
|
|
|
|
return s.TagIDs.load(func() ([]int, error) {
|
|
|
|
return l.GetTagIDs(ctx, s.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadMovies(ctx context.Context, l SceneMovieLoader) error {
|
|
|
|
return s.Movies.load(func() ([]MoviesScenes, error) {
|
|
|
|
return l.GetMovies(ctx, s.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadStashIDs(ctx context.Context, l StashIDLoader) error {
|
|
|
|
return s.StashIDs.load(func() ([]StashID, error) {
|
|
|
|
return l.GetStashIDs(ctx, s.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scene) LoadRelationships(ctx context.Context, l SceneReader) error {
|
|
|
|
if err := s.LoadGalleryIDs(ctx, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.LoadPerformerIDs(ctx, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.LoadTagIDs(ctx, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.LoadMovies(ctx, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.LoadStashIDs(ctx, l); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2019-10-14 21:57:53 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s Scene) PrimaryFile() *file.VideoFile {
|
|
|
|
if len(s.Files) == 0 {
|
|
|
|
return nil
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return s.Files[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) Path() string {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
return p.Base().Path
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) getHash(type_ string) string {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
v := p.Base().Fingerprints.Get(type_)
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.(string)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
return ""
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s Scene) Checksum() string {
|
|
|
|
return s.getHash(file.FingerprintTypeMD5)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s Scene) OSHash() string {
|
|
|
|
return s.getHash(file.FingerprintTypeOshash)
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s Scene) Phash() int64 {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
v := p.Base().Fingerprints.Get(file.FingerprintTypePhash)
|
|
|
|
if v == nil {
|
|
|
|
return 0
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return v.(int64)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) Duration() float64 {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
return p.Duration
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) Format() string {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
return p.Format
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) VideoCodec() string {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
return p.VideoCodec
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scene) AudioCodec() string {
|
|
|
|
if p := s.PrimaryFile(); p != nil {
|
|
|
|
return p.AudioCodec
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// ScenePartial represents part of a Scene object. It is used to update
|
2022-07-13 06:30:54 +00:00
|
|
|
// the database entry.
|
2019-10-14 21:57:53 +00:00
|
|
|
type ScenePartial struct {
|
2022-07-13 06:30:54 +00:00
|
|
|
Title OptionalString
|
|
|
|
Details OptionalString
|
|
|
|
URL OptionalString
|
|
|
|
Date OptionalDate
|
|
|
|
Rating OptionalInt
|
|
|
|
Organized OptionalBool
|
|
|
|
OCounter OptionalInt
|
|
|
|
StudioID OptionalInt
|
|
|
|
CreatedAt OptionalTime
|
|
|
|
UpdatedAt OptionalTime
|
|
|
|
|
|
|
|
GalleryIDs *UpdateIDs
|
|
|
|
TagIDs *UpdateIDs
|
|
|
|
PerformerIDs *UpdateIDs
|
|
|
|
MovieIDs *UpdateMovieIDs
|
|
|
|
StashIDs *UpdateStashIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewScenePartial() ScenePartial {
|
|
|
|
updatedTime := time.Now()
|
|
|
|
return ScenePartial{
|
|
|
|
UpdatedAt: NewOptionalTime(updatedTime),
|
|
|
|
}
|
2019-10-14 21:57:53 +00:00
|
|
|
}
|
2019-12-01 16:18:44 +00:00
|
|
|
|
2022-04-25 05:55:05 +00:00
|
|
|
type SceneMovieInput struct {
|
|
|
|
MovieID string `json:"movie_id"`
|
|
|
|
SceneIndex *int `json:"scene_index"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SceneUpdateInput struct {
|
|
|
|
ClientMutationID *string `json:"clientMutationId"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
Details *string `json:"details"`
|
|
|
|
URL *string `json:"url"`
|
|
|
|
Date *string `json:"date"`
|
|
|
|
Rating *int `json:"rating"`
|
|
|
|
Organized *bool `json:"organized"`
|
|
|
|
StudioID *string `json:"studio_id"`
|
|
|
|
GalleryIds []string `json:"gallery_ids"`
|
|
|
|
PerformerIds []string `json:"performer_ids"`
|
|
|
|
Movies []*SceneMovieInput `json:"movies"`
|
|
|
|
TagIds []string `json:"tag_ids"`
|
|
|
|
// This should be a URL or a base64 encoded data URL
|
2022-07-13 06:30:54 +00:00
|
|
|
CoverImage *string `json:"cover_image"`
|
|
|
|
StashIds []StashID `json:"stash_ids"`
|
2022-04-25 05:55:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 03:25:17 +00:00
|
|
|
// UpdateInput constructs a SceneUpdateInput using the populated fields in the ScenePartial object.
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s ScenePartial) UpdateInput(id int) SceneUpdateInput {
|
|
|
|
var dateStr *string
|
|
|
|
if s.Date.Set {
|
|
|
|
d := s.Date.Value
|
|
|
|
v := d.String()
|
|
|
|
dateStr = &v
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
var stashIDs []StashID
|
|
|
|
if s.StashIDs != nil {
|
|
|
|
stashIDs = s.StashIDs.StashIDs
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return SceneUpdateInput{
|
|
|
|
ID: strconv.Itoa(id),
|
|
|
|
Title: s.Title.Ptr(),
|
|
|
|
Details: s.Details.Ptr(),
|
|
|
|
URL: s.URL.Ptr(),
|
|
|
|
Date: dateStr,
|
|
|
|
Rating: s.Rating.Ptr(),
|
|
|
|
Organized: s.Organized.Ptr(),
|
|
|
|
StudioID: s.StudioID.StringPtr(),
|
|
|
|
GalleryIds: s.GalleryIDs.IDStrings(),
|
|
|
|
PerformerIds: s.PerformerIDs.IDStrings(),
|
|
|
|
Movies: s.MovieIDs.SceneMovieInputs(),
|
|
|
|
TagIds: s.TagIDs.IDStrings(),
|
|
|
|
StashIds: stashIDs,
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// GetTitle returns the title of the scene. If the Title field is empty,
|
|
|
|
// then the base filename is returned.
|
2019-12-01 16:18:44 +00:00
|
|
|
func (s Scene) GetTitle() string {
|
2022-07-13 06:30:54 +00:00
|
|
|
if s.Title != "" {
|
|
|
|
return s.Title
|
2019-12-01 16:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return filepath.Base(s.Path())
|
2019-12-01 16:18:44 +00:00
|
|
|
}
|
2019-12-21 00:13:23 +00:00
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// GetHash returns the hash of the scene, based on the hash algorithm provided. If
|
|
|
|
// hash algorithm is MD5, then Checksum is returned. Otherwise, OSHash is returned.
|
|
|
|
func (s Scene) GetHash(hashAlgorithm HashAlgorithm) string {
|
2022-07-13 06:30:54 +00:00
|
|
|
f := s.PrimaryFile()
|
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch hashAlgorithm {
|
|
|
|
case HashAlgorithmMd5:
|
|
|
|
return f.Base().Fingerprints.Get(file.FingerprintTypeMD5).(string)
|
|
|
|
case HashAlgorithmOshash:
|
|
|
|
return f.Base().Fingerprints.Get(file.FingerprintTypeOshash).(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2020-08-06 01:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (s Scene) GetMinResolution() int {
|
|
|
|
f := s.PrimaryFile()
|
|
|
|
if f == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
w := f.Width
|
|
|
|
h := f.Height
|
|
|
|
|
|
|
|
if w < h {
|
|
|
|
return w
|
2021-03-11 01:51:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
return h
|
2021-03-11 01:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// SceneFileType represents the file metadata for a scene.
|
2019-12-21 00:13:23 +00:00
|
|
|
type SceneFileType struct {
|
|
|
|
Size *string `graphql:"size" json:"size"`
|
|
|
|
Duration *float64 `graphql:"duration" json:"duration"`
|
|
|
|
VideoCodec *string `graphql:"video_codec" json:"video_codec"`
|
|
|
|
AudioCodec *string `graphql:"audio_codec" json:"audio_codec"`
|
|
|
|
Width *int `graphql:"width" json:"width"`
|
|
|
|
Height *int `graphql:"height" json:"height"`
|
|
|
|
Framerate *float64 `graphql:"framerate" json:"framerate"`
|
|
|
|
Bitrate *int `graphql:"bitrate" json:"bitrate"`
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
type Scenes []*Scene
|
|
|
|
|
|
|
|
func (s *Scenes) Append(o interface{}) {
|
|
|
|
*s = append(*s, o.(*Scene))
|
|
|
|
}
|
|
|
|
|
2021-01-21 11:02:09 +00:00
|
|
|
func (s *Scenes) New() interface{} {
|
2021-01-18 01:23:20 +00:00
|
|
|
return &Scene{}
|
|
|
|
}
|
2022-05-06 01:59:28 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
type VideoCaption struct {
|
2022-05-06 01:59:28 +00:00
|
|
|
LanguageCode string `json:"language_code"`
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
CaptionType string `json:"caption_type"`
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (c VideoCaption) Path(filePath string) string {
|
|
|
|
return filepath.Join(filepath.Dir(filePath), c.Filename)
|
2022-05-06 01:59:28 +00:00
|
|
|
}
|