2020-10-12 23:12:46 +00:00
|
|
|
package gallery
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2023-03-16 04:04:54 +00:00
|
|
|
"fmt"
|
2022-05-19 07:49:32 +00:00
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/json"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/jsonschema"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToBasicJSON converts a gallery object into its JSON object equivalent. It
|
|
|
|
// does not convert the relationships to other objects.
|
|
|
|
func ToBasicJSON(gallery *models.Gallery) (*jsonschema.Gallery, error) {
|
|
|
|
newGalleryJSON := jsonschema.Gallery{
|
2023-11-28 04:05:33 +00:00
|
|
|
Title: gallery.Title,
|
|
|
|
Code: gallery.Code,
|
|
|
|
URLs: gallery.URLs.List(),
|
|
|
|
Details: gallery.Details,
|
|
|
|
Photographer: gallery.Photographer,
|
|
|
|
CreatedAt: json.JSONTime{Time: gallery.CreatedAt},
|
|
|
|
UpdatedAt: json.JSONTime{Time: gallery.UpdatedAt},
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
if gallery.FolderID != nil {
|
|
|
|
newGalleryJSON.FolderPath = gallery.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range gallery.Files.List() {
|
2022-08-30 02:17:15 +00:00
|
|
|
newGalleryJSON.ZipFiles = append(newGalleryJSON.ZipFiles, f.Base().Path)
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
if gallery.Date != nil {
|
|
|
|
newGalleryJSON.Date = gallery.Date.String()
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
if gallery.Rating != nil {
|
|
|
|
newGalleryJSON.Rating = *gallery.Rating
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:06:49 +00:00
|
|
|
newGalleryJSON.Organized = gallery.Organized
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
return &newGalleryJSON, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStudioName returns the name of the provided gallery's studio. It returns an
|
|
|
|
// empty string if there is no studio assigned to the gallery.
|
2023-09-01 00:39:29 +00:00
|
|
|
func GetStudioName(ctx context.Context, reader models.StudioGetter, gallery *models.Gallery) (string, error) {
|
2022-07-13 06:30:54 +00:00
|
|
|
if gallery.StudioID != nil {
|
|
|
|
studio, err := reader.Find(ctx, *gallery.StudioID)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if studio != nil {
|
2023-06-15 02:46:09 +00:00
|
|
|
return studio.Name, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
2020-10-30 22:41:12 +00:00
|
|
|
|
2023-03-16 04:04:54 +00:00
|
|
|
// GetGalleryChaptersJSON returns a slice of GalleryChapter JSON representation
|
|
|
|
// objects corresponding to the provided gallery's chapters.
|
2023-09-01 00:39:29 +00:00
|
|
|
func GetGalleryChaptersJSON(ctx context.Context, chapterReader models.GalleryChapterFinder, gallery *models.Gallery) ([]jsonschema.GalleryChapter, error) {
|
2023-03-16 04:04:54 +00:00
|
|
|
galleryChapters, err := chapterReader.FindByGalleryID(ctx, gallery.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting gallery chapters: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var results []jsonschema.GalleryChapter
|
|
|
|
|
|
|
|
for _, galleryChapter := range galleryChapters {
|
|
|
|
galleryChapterJSON := jsonschema.GalleryChapter{
|
|
|
|
Title: galleryChapter.Title,
|
|
|
|
ImageIndex: galleryChapter.ImageIndex,
|
2023-06-15 02:46:09 +00:00
|
|
|
CreatedAt: json.JSONTime{Time: galleryChapter.CreatedAt},
|
|
|
|
UpdatedAt: json.JSONTime{Time: galleryChapter.UpdatedAt},
|
2023-03-16 04:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results = append(results, galleryChapterJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-10-30 22:41:12 +00:00
|
|
|
func GetIDs(galleries []*models.Gallery) []int {
|
|
|
|
var results []int
|
|
|
|
for _, gallery := range galleries {
|
|
|
|
results = append(results, gallery.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
2021-02-01 20:56:54 +00:00
|
|
|
|
2022-08-30 02:17:15 +00:00
|
|
|
func GetRefs(galleries []*models.Gallery) []jsonschema.GalleryRef {
|
|
|
|
var results []jsonschema.GalleryRef
|
2021-02-01 20:56:54 +00:00
|
|
|
for _, gallery := range galleries {
|
2022-08-30 02:17:15 +00:00
|
|
|
toAdd := jsonschema.GalleryRef{}
|
|
|
|
switch {
|
2022-09-01 07:54:34 +00:00
|
|
|
case gallery.FolderID != nil:
|
|
|
|
toAdd.FolderPath = gallery.Path
|
|
|
|
case len(gallery.Files.List()) > 0:
|
|
|
|
for _, f := range gallery.Files.List() {
|
2022-08-30 02:17:15 +00:00
|
|
|
toAdd.ZipFiles = append(toAdd.ZipFiles, f.Base().Path)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
toAdd.Title = gallery.Title
|
2021-02-01 20:56:54 +00:00
|
|
|
}
|
2022-08-30 02:17:15 +00:00
|
|
|
|
|
|
|
results = append(results, toAdd)
|
2021-02-01 20:56:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|