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
20 changes: 20 additions & 0 deletions internal/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/models/paths"
"github.com/stashapp/stash/pkg/plugin/common/log"
)

var officialBuild string
Expand Down Expand Up @@ -129,6 +130,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)\.[^\.]+$`

// Interface options
MenuItems = "menu_items"

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

func (i *Instance) GetGalleryCoverRegex() string {
var regexString = i.getString(GalleryCoverRegex)

_, err := regexp.Compile(regexString)
if err != nil {
log.Warn(fmt.Sprintf("Gallery cover regex '%v' invalid, reverting to default.", regexString))
Ksrx01 marked this conversation as resolved.
Show resolved Hide resolved
return galleryCoverRegexDefault
}

return i.getString(GalleryCoverRegex)
}

func (i *Instance) GetScrapersPath() string {
return i.getString(ScrapersPath)
}
Expand Down Expand Up @@ -1440,6 +1457,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
1 change: 1 addition & 0 deletions ui/v2.5/src/docs/en/Manual/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ These options are typically not exposed in the UI and must be changed manually i
| `custom_ui_location` | The file system folder where the UI files will be served from, instead of using the embedded UI. Empty to disable. Stash must be restarted to take effect. |
| `max_upload_size` | Maximum file upload size for import files. Defaults to 1GB. |
| `theme_color` | Sets the `theme-color` property in the UI. |
| `gallery_cover_regex` | The regex responsible for selecting images as gallery covers |

### Custom served folders

Expand Down