Files
stash/pkg/studio/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

77 lines
1.9 KiB
Go

package studio
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/utils"
)
type FinderImageStashIDGetter interface {
Finder
GetAliases(ctx context.Context, studioID int) ([]string, error)
GetImage(ctx context.Context, studioID int) ([]byte, error)
models.StashIDLoader
}
// ToJSON converts a Studio object into its JSON equivalent.
func ToJSON(ctx context.Context, reader FinderImageStashIDGetter, studio *models.Studio) (*jsonschema.Studio, error) {
newStudioJSON := jsonschema.Studio{
Name: studio.Name,
URL: studio.URL,
Details: studio.Details,
IgnoreAutoTag: studio.IgnoreAutoTag,
CreatedAt: json.JSONTime{Time: studio.CreatedAt},
UpdatedAt: json.JSONTime{Time: studio.UpdatedAt},
}
if studio.ParentID != nil {
parent, err := reader.Find(ctx, *studio.ParentID)
if err != nil {
return nil, fmt.Errorf("error getting parent studio: %v", err)
}
if parent != nil {
newStudioJSON.ParentStudio = parent.Name
}
}
if studio.Rating != nil {
newStudioJSON.Rating = *studio.Rating
}
aliases, err := reader.GetAliases(ctx, studio.ID)
if err != nil {
return nil, fmt.Errorf("error getting studio aliases: %v", err)
}
newStudioJSON.Aliases = aliases
image, err := reader.GetImage(ctx, studio.ID)
if err != nil {
logger.Errorf("Error getting studio image: %v", err)
}
if len(image) > 0 {
newStudioJSON.Image = utils.GetBase64StringFromData(image)
}
stashIDs, _ := reader.GetStashIDs(ctx, studio.ID)
var ret []models.StashID
for _, stashID := range stashIDs {
newJoin := models.StashID{
StashID: stashID.StashID,
Endpoint: stashID.Endpoint,
}
ret = append(ret, newJoin)
}
newStudioJSON.StashIDs = ret
return &newStudioJSON, nil
}