Files
stash/pkg/movie/export.go
DingDongSoLong4 1c13c9e1b1 SQLite model refactoring (#3791)
* Remove ID from PerformerPartial
* Separate studio model from sqlite model
* Separate movie model from sqlite model
* Separate tag model from sqlite model
* Separate saved filter model from sqlite model
* Separate scene marker model from sqlite model
* Separate gallery chapter model from sqlite model
* Move ErrNoRows checks into sqlite, improve empty result error messages
* Move SQLiteDate and SQLiteTimestamp to sqlite
* Use changesetTranslator everywhere, refactor for consistency
* Make PerformerStore.DestroyImage private
* Fix rating on movie create
2023-06-15 12:46:09 +10:00

73 lines
1.8 KiB
Go

package movie
import (
"context"
"fmt"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/models/json"
"github.com/stashapp/stash/pkg/models/jsonschema"
"github.com/stashapp/stash/pkg/studio"
"github.com/stashapp/stash/pkg/utils"
)
type ImageGetter interface {
GetFrontImage(ctx context.Context, movieID int) ([]byte, error)
GetBackImage(ctx context.Context, movieID int) ([]byte, error)
}
// ToJSON converts a Movie into its JSON equivalent.
func ToJSON(ctx context.Context, reader ImageGetter, studioReader studio.Finder, movie *models.Movie) (*jsonschema.Movie, error) {
newMovieJSON := jsonschema.Movie{
Name: movie.Name,
Aliases: movie.Aliases,
Director: movie.Director,
Synopsis: movie.Synopsis,
URL: movie.URL,
CreatedAt: json.JSONTime{Time: movie.CreatedAt},
UpdatedAt: json.JSONTime{Time: movie.UpdatedAt},
}
if movie.Date != nil {
newMovieJSON.Date = movie.Date.String()
}
if movie.Rating != nil {
newMovieJSON.Rating = *movie.Rating
}
if movie.Duration != nil {
newMovieJSON.Duration = *movie.Duration
}
if movie.StudioID != nil {
studio, err := studioReader.Find(ctx, *movie.StudioID)
if err != nil {
return nil, fmt.Errorf("error getting movie studio: %v", err)
}
if studio != nil {
newMovieJSON.Studio = studio.Name
}
}
frontImage, err := reader.GetFrontImage(ctx, movie.ID)
if err != nil {
logger.Errorf("Error getting movie front image: %v", err)
}
if len(frontImage) > 0 {
newMovieJSON.FrontImage = utils.GetBase64StringFromData(frontImage)
}
backImage, err := reader.GetBackImage(ctx, movie.ID)
if err != nil {
logger.Errorf("Error getting movie back image: %v", err)
}
if len(backImage) > 0 {
newMovieJSON.BackImage = utils.GetBase64StringFromData(backImage)
}
return &newMovieJSON, nil
}