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

Improved gallery cover lookup #3391

Merged
merged 11 commits into from
Feb 22, 2023
5 changes: 3 additions & 2 deletions internal/api/resolver_model_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/stashapp/stash/internal/api/loaders"
"github.com/stashapp/stash/internal/manager/config"

"github.com/stashapp/stash/pkg/file"
"github.com/stashapp/stash/pkg/image"
Expand Down Expand Up @@ -145,8 +146,8 @@ func (r *galleryResolver) Images(ctx context.Context, obj *models.Gallery) (ret

func (r *galleryResolver) Cover(ctx context.Context, obj *models.Gallery) (ret *models.Image, err error) {
if err := r.withReadTxn(ctx, func(ctx context.Context) error {
// find cover.jpg first
ret, err = image.FindGalleryCover(ctx, r.repository.Image, obj.ID)
// Find cover image first
ret, err = image.FindGalleryCover(ctx, r.repository.Image, obj.ID, config.GetInstance().GetGalleryCoverRegex())
return err
}); err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions internal/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ const (
// rather than use the embedded UI.
CustomUILocation = "custom_ui_location"

// Gallery Cover Regex
GalleryCoverRegex = "gallery_cover_regex"
Ksrx01 marked this conversation as resolved.
Show resolved Hide resolved
galleryCoverRegexDefault = `(poster|cover|folder|board)(\.jpg|\.jpeg|\.png)$`
Ksrx01 marked this conversation as resolved.
Show resolved Hide resolved

// Interface options
MenuItems = "menu_items"

Expand Down Expand Up @@ -635,6 +639,10 @@ func (i *Instance) GetVideoFileNamingAlgorithm() models.HashAlgorithm {
return models.HashAlgorithm(ret)
}

func (i *Instance) GetGalleryCoverRegex() string {
return i.getString(GalleryCoverRegex)
}
Ksrx01 marked this conversation as resolved.
Show resolved Hide resolved

func (i *Instance) GetScrapersPath() string {
return i.getString(ScrapersPath)
}
Expand Down Expand Up @@ -1440,6 +1448,9 @@ func (i *Instance) setDefaultValues(write bool) error {
i.main.SetDefault(ScrapersPath, defaultScrapersPath)
i.main.SetDefault(PluginsPath, defaultPluginsPath)

// Set default gallery cover regex
i.main.SetDefault(GalleryCoverRegex, galleryCoverRegexDefault)

if write {
return i.main.WriteConfig()
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/image/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import (
"github.com/stashapp/stash/pkg/models"
)

const (
coverFilename = "cover.jpg"
coverFilenameSearchString = "%" + coverFilename
)

type Queryer interface {
Query(ctx context.Context, options models.ImageQueryOptions) (*models.ImageQueryResult, error)
}
Expand Down Expand Up @@ -102,9 +97,9 @@ func FindByGalleryID(ctx context.Context, r Queryer, galleryID int, sortBy strin
}, &findFilter)
}

func FindGalleryCover(ctx context.Context, r Queryer, galleryID int) (*models.Image, error) {
func FindGalleryCover(ctx context.Context, r Queryer, galleryID int, galleryCoverRegex string) (*models.Image, error) {
const useCoverJpg = true
img, err := findGalleryCover(ctx, r, galleryID, useCoverJpg)
img, err := findGalleryCover(ctx, r, galleryID, useCoverJpg, galleryCoverRegex)
if err != nil {
return nil, err
}
Expand All @@ -114,10 +109,10 @@ func FindGalleryCover(ctx context.Context, r Queryer, galleryID int) (*models.Im
}

// return the first image in the gallery
return findGalleryCover(ctx, r, galleryID, !useCoverJpg)
return findGalleryCover(ctx, r, galleryID, !useCoverJpg, galleryCoverRegex)
}

func findGalleryCover(ctx context.Context, r Queryer, galleryID int, useCoverJpg bool) (*models.Image, error) {
func findGalleryCover(ctx context.Context, r Queryer, galleryID int, useCoverJpg bool, galleryCoverRegex string) (*models.Image, error) {
// try to find cover.jpg in the gallery
perPage := 1
sortBy := "path"
Expand All @@ -138,8 +133,8 @@ func findGalleryCover(ctx context.Context, r Queryer, galleryID int, useCoverJpg

if useCoverJpg {
imageFilter.Path = &models.StringCriterionInput{
Value: coverFilenameSearchString,
Modifier: models.CriterionModifierEquals,
Value: "(?i)" + galleryCoverRegex,
Modifier: models.CriterionModifierMatchesRegex,
}
}

Expand Down