stash/pkg/movie/export.go

77 lines
1.9 KiB
Go
Raw Normal View History

2020-09-15 07:28:53 +00:00
package movie
import (
"fmt"
"github.com/stashapp/stash/pkg/manager/jsonschema"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
)
// ToJSON converts a Movie into its JSON equivalent.
func ToJSON(reader models.MovieReader, studioReader models.StudioReader, movie *models.Movie) (*jsonschema.Movie, error) {
newMovieJSON := jsonschema.Movie{
CreatedAt: models.JSONTime{Time: movie.CreatedAt.Timestamp},
UpdatedAt: models.JSONTime{Time: movie.UpdatedAt.Timestamp},
}
if movie.Name.Valid {
newMovieJSON.Name = movie.Name.String
}
if movie.Aliases.Valid {
newMovieJSON.Aliases = movie.Aliases.String
}
if movie.Date.Valid {
newMovieJSON.Date = utils.GetYMDFromDatabaseDate(movie.Date.String)
}
if movie.Rating.Valid {
newMovieJSON.Rating = int(movie.Rating.Int64)
}
if movie.Duration.Valid {
newMovieJSON.Duration = int(movie.Duration.Int64)
}
if movie.Director.Valid {
newMovieJSON.Director = movie.Director.String
}
if movie.Synopsis.Valid {
newMovieJSON.Synopsis = movie.Synopsis.String
}
if movie.URL.Valid {
newMovieJSON.URL = movie.URL.String
}
if movie.StudioID.Valid {
studio, err := studioReader.Find(int(movie.StudioID.Int64))
if err != nil {
return nil, fmt.Errorf("error getting movie studio: %s", err.Error())
}
if studio != nil {
newMovieJSON.Studio = studio.Name.String
}
}
frontImage, err := reader.GetFrontImage(movie.ID)
if err != nil {
return nil, fmt.Errorf("error getting movie front image: %s", err.Error())
}
if len(frontImage) > 0 {
newMovieJSON.FrontImage = utils.GetBase64StringFromData(frontImage)
}
backImage, err := reader.GetBackImage(movie.ID)
if err != nil {
return nil, fmt.Errorf("error getting movie back image: %s", err.Error())
}
if len(backImage) > 0 {
newMovieJSON.BackImage = utils.GetBase64StringFromData(backImage)
}
return &newMovieJSON, nil
}