Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple image URLs #4000

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphql/documents/data/image-slim.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fragment SlimImageData on Image {
id
title
date
url
urls
rating100
organized
o_counter
Expand Down
2 changes: 1 addition & 1 deletion graphql/documents/data/image.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fragment ImageData on Image {
title
rating100
date
url
urls
organized
o_counter
created_at
Expand Down
9 changes: 6 additions & 3 deletions graphql/schema/types/image.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Image {
rating: Int @deprecated(reason: "Use 1-100 range with rating100")
# rating expressed as 1-100
rating100: Int
url: String
url: String @deprecated(reason: "Use urls")
urls: [String!]!
date: String
o_counter: Int
organized: Boolean!
Expand Down Expand Up @@ -48,7 +49,8 @@ input ImageUpdateInput {
# rating expressed as 1-100
rating100: Int
organized: Boolean
url: String
url: String @deprecated(reason: "Use urls")
urls: [String!]
date: String

studio_id: ID
Expand All @@ -68,7 +70,8 @@ input BulkImageUpdateInput {
# rating expressed as 1-100
rating100: Int
organized: Boolean
url: String
url: String @deprecated(reason: "Use urls")
urls: BulkUpdateStrings
date: String

studio_id: ID
Expand Down
2 changes: 1 addition & 1 deletion graphql/schema/types/scene.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Scene {
details: String
director: String
url: String @deprecated(reason: "Use urls")
urls: [String!]
urls: [String!]!
date: String
# rating expressed as 1-5
rating: Int @deprecated(reason: "Use 1-100 range with rating100")
Expand Down
29 changes: 29 additions & 0 deletions internal/api/resolver_model_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,32 @@ func (r *imageResolver) Performers(ctx context.Context, obj *models.Image) (ret
ret, errs = loaders.From(ctx).PerformerByID.LoadAll(obj.PerformerIDs.List())
return ret, firstError(errs)
}

func (r *imageResolver) URL(ctx context.Context, obj *models.Image) (*string, error) {
if !obj.URLs.Loaded() {
if err := r.withReadTxn(ctx, func(ctx context.Context) error {
return obj.LoadURLs(ctx, r.repository.Image)
}); err != nil {
return nil, err
}
}

urls := obj.URLs.List()
if len(urls) == 0 {
return nil, nil
}

return &urls[0], nil
}

func (r *imageResolver) Urls(ctx context.Context, obj *models.Image) ([]string, error) {
if !obj.URLs.Loaded() {
if err := r.withReadTxn(ctx, func(ctx context.Context) error {
return obj.LoadURLs(ctx, r.repository.Image)
}); err != nil {
return nil, err
}
}

return obj.URLs.List(), nil
}
26 changes: 24 additions & 2 deletions internal/api/resolver_mutation_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func (r *mutationResolver) imageUpdate(ctx context.Context, input ImageUpdateInp

updatedImage.Title = translator.optionalString(input.Title, "title")
updatedImage.Rating = translator.ratingConversionOptional(input.Rating, input.Rating100)
updatedImage.URL = translator.optionalString(input.URL, "url")
updatedImage.Date, err = translator.optionalDate(input.Date, "date")
if err != nil {
return nil, fmt.Errorf("converting date: %w", err)
Expand All @@ -117,6 +116,18 @@ func (r *mutationResolver) imageUpdate(ctx context.Context, input ImageUpdateInp
}
updatedImage.Organized = translator.optionalBool(input.Organized, "organized")

if translator.hasField("urls") {
updatedImage.URLs = &models.UpdateStrings{
Values: input.Urls,
Mode: models.RelationshipUpdateModeSet,
}
} else if translator.hasField("url") {
updatedImage.URLs = &models.UpdateStrings{
Values: []string{*input.URL},
Mode: models.RelationshipUpdateModeSet,
}
}

if input.PrimaryFileID != nil {
primaryFileID, err := strconv.Atoi(*input.PrimaryFileID)
if err != nil {
Expand Down Expand Up @@ -208,7 +219,6 @@ func (r *mutationResolver) BulkImageUpdate(ctx context.Context, input BulkImageU

updatedImage.Title = translator.optionalString(input.Title, "title")
updatedImage.Rating = translator.ratingConversionOptional(input.Rating, input.Rating100)
updatedImage.URL = translator.optionalString(input.URL, "url")
updatedImage.Date, err = translator.optionalDate(input.Date, "date")
if err != nil {
return nil, fmt.Errorf("converting date: %w", err)
Expand All @@ -219,6 +229,18 @@ func (r *mutationResolver) BulkImageUpdate(ctx context.Context, input BulkImageU
}
updatedImage.Organized = translator.optionalBool(input.Organized, "organized")

if translator.hasField("urls") {
updatedImage.URLs = &models.UpdateStrings{
Values: input.Urls.Values,
Mode: input.Urls.Mode,
}
} else if translator.hasField("url") {
updatedImage.URLs = &models.UpdateStrings{
Values: []string{*input.URL},
Mode: models.RelationshipUpdateModeSet,
}
}

if translator.hasField("gallery_ids") {
updatedImage.GalleryIDs, err = translateUpdateIDs(input.GalleryIds.Ids, input.GalleryIds.Mode)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/manager/task_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ func exportImage(ctx context.Context, wg *sync.WaitGroup, jobChan <-chan *models
continue
}

if err := s.LoadURLs(ctx, repo.Image); err != nil {
logger.Errorf("[images] <%s> error getting image urls: %s", imageHash, err.Error())
continue
}

newImageJSON := image.ToBasicJSON(s)

// export files
Expand Down
15 changes: 1 addition & 14 deletions pkg/image/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func ToBasicJSON(image *models.Image) *jsonschema.Image {
newImageJSON := jsonschema.Image{
Title: image.Title,
URL: image.URL,
URLs: image.URLs.List(),
CreatedAt: json.JSONTime{Time: image.CreatedAt},
UpdatedAt: json.JSONTime{Time: image.UpdatedAt},
}
Expand All @@ -38,19 +38,6 @@ func ToBasicJSON(image *models.Image) *jsonschema.Image {
return &newImageJSON
}

// func getImageFileJSON(image *models.Image) *jsonschema.ImageFile {
// ret := &jsonschema.ImageFile{}

// f := image.PrimaryFile()

// ret.ModTime = json.JSONTime{Time: f.ModTime}
// ret.Size = f.Size
// ret.Width = f.Width
// ret.Height = f.Height

// return ret
// }

// GetStudioName returns the name of the provided image's studio. It returns an
// empty string if there is no studio assigned to the image.
func GetStudioName(ctx context.Context, reader studio.Finder, image *models.Image) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/image/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func createFullImage(id int) models.Image {
OCounter: ocounter,
Rating: &rating,
Date: &dateObj,
URL: url,
URLs: models.NewRelatedStrings([]string{url}),
Organized: organized,
CreatedAt: createTime,
UpdatedAt: updateTime,
Expand All @@ -67,7 +67,7 @@ func createFullJSONImage() *jsonschema.Image {
OCounter: ocounter,
Rating: rating,
Date: date,
URL: url,
URLs: []string{url},
Organized: organized,
Files: []string{path},
CreatedAt: json.JSONTime{
Expand Down
9 changes: 5 additions & 4 deletions pkg/image/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ func (i *Importer) PreImport(ctx context.Context) error {

func (i *Importer) imageJSONToImage(imageJSON jsonschema.Image) models.Image {
newImage := models.Image{
// Checksum: imageJSON.Checksum,
// Path: i.Path,
PerformerIDs: models.NewRelatedIDs([]int{}),
TagIDs: models.NewRelatedIDs([]int{}),
GalleryIDs: models.NewRelatedIDs([]int{}),
Expand All @@ -85,9 +83,12 @@ func (i *Importer) imageJSONToImage(imageJSON jsonschema.Image) models.Image {
if imageJSON.Rating != 0 {
newImage.Rating = &imageJSON.Rating
}
if imageJSON.URL != "" {
newImage.URL = imageJSON.URL
if len(imageJSON.URLs) > 0 {
newImage.URLs = models.NewRelatedStrings(imageJSON.URLs)
} else if imageJSON.URL != "" {
newImage.URLs = models.NewRelatedStrings([]string{imageJSON.URL})
}

if imageJSON.Date != "" {
d, err := models.ParseDate(imageJSON.Date)
if err == nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type ImageReader interface {
Query(ctx context.Context, options ImageQueryOptions) (*ImageQueryResult, error)
QueryCount(ctx context.Context, imageFilter *ImageFilterType, findFilter *FindFilterType) (int, error)

URLLoader
GalleryIDLoader
PerformerIDLoader
TagIDLoader
Expand Down
12 changes: 8 additions & 4 deletions pkg/models/jsonschema/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
)

type Image struct {
Title string `json:"title,omitempty"`
Studio string `json:"studio,omitempty"`
Rating int `json:"rating,omitempty"`
URL string `json:"url,omitempty"`
Title string `json:"title,omitempty"`
Studio string `json:"studio,omitempty"`
Rating int `json:"rating,omitempty"`

// deprecated - for import only
URL string `json:"url,omitempty"`

URLs []string `json:"urls,omitempty"`
Date string `json:"date,omitempty"`
Organized bool `json:"organized,omitempty"`
OCounter int `json:"o_counter,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion pkg/models/jsonschema/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type Scene struct {
Title string `json:"title,omitempty"`
Code string `json:"code,omitempty"`
Studio string `json:"studio,omitempty"`

// deprecated - for import only
URL string `json:"url,omitempty"`
URL string `json:"url,omitempty"`

URLs []string `json:"urls,omitempty"`
Date string `json:"date,omitempty"`
Rating int `json:"rating,omitempty"`
Expand Down
23 changes: 23 additions & 0 deletions pkg/models/mocks/ImageReaderWriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/models/mocks/StudioReaderWriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions pkg/models/model_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type Image struct {

Title string `json:"title"`
// Rating expressed in 1-100 scale
Rating *int `json:"rating"`
Organized bool `json:"organized"`
OCounter int `json:"o_counter"`
StudioID *int `json:"studio_id"`
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"`
URLs RelatedStrings `json:"urls"`
Date *Date `json:"date"`

// transient - not persisted
Files RelatedFiles
Expand All @@ -38,6 +38,12 @@ type Image struct {
PerformerIDs RelatedIDs `json:"performer_ids"`
}

func (i *Image) LoadURLs(ctx context.Context, l URLLoader) error {
return i.URLs.load(func() ([]string, error) {
return l.GetURLs(ctx, i.ID)
})
}

func (i *Image) LoadFiles(ctx context.Context, l FileLoader) error {
return i.Files.load(func() ([]file.File, error) {
return l.GetFiles(ctx, i.ID)
Expand Down Expand Up @@ -114,7 +120,7 @@ type ImagePartial struct {
Title OptionalString
// Rating expressed in 1-100 scale
Rating OptionalInt
URL OptionalString
URLs *UpdateStrings
Date OptionalDate
Organized OptionalBool
OCounter OptionalInt
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlite/anonymise.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ func (db *Anonymiser) anonymiseImages(ctx context.Context) error {
query := dialect.From(table).Select(
table.Col(idColumn),
table.Col("title"),
table.Col("url"),
).Where(table.Col(idColumn).Gt(lastID)).Limit(1000)

gotSome = false
Expand All @@ -378,20 +377,17 @@ func (db *Anonymiser) anonymiseImages(ctx context.Context) error {
var (
id int
title sql.NullString
url sql.NullString
)

if err := rows.Scan(
&id,
&title,
&url,
); err != nil {
return err
}

set := goqu.Record{}
db.obfuscateNullString(set, "title", title)
db.obfuscateNullString(set, "url", url)

if len(set) > 0 {
stmt := dialect.Update(table).Set(set).Where(table.Col(idColumn).Eq(id))
Expand All @@ -416,6 +412,10 @@ func (db *Anonymiser) anonymiseImages(ctx context.Context) error {
}
}

if err := db.anonymiseURLs(ctx, goqu.T(imagesURLsTable), "image_id"); err != nil {
return err
}

return nil
}

Expand Down
Loading
Loading