mirror of
https://github.com/stashapp/stash.git
synced 2025-12-18 04:44:37 +03:00
Improve gallery performance (#3183)
* Improve cover resolver performance * Deprecate and remove usage of slow gallery images
This commit is contained in:
@@ -7,6 +7,11 @@ 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)
|
||||
}
|
||||
@@ -96,3 +101,56 @@ func FindByGalleryID(ctx context.Context, r Queryer, galleryID int, sortBy strin
|
||||
},
|
||||
}, &findFilter)
|
||||
}
|
||||
|
||||
func FindGalleryCover(ctx context.Context, r Queryer, galleryID int) (*models.Image, error) {
|
||||
const useCoverJpg = true
|
||||
img, err := findGalleryCover(ctx, r, galleryID, useCoverJpg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if img != nil {
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// return the first image in the gallery
|
||||
return findGalleryCover(ctx, r, galleryID, !useCoverJpg)
|
||||
}
|
||||
|
||||
func findGalleryCover(ctx context.Context, r Queryer, galleryID int, useCoverJpg bool) (*models.Image, error) {
|
||||
// try to find cover.jpg in the gallery
|
||||
perPage := 1
|
||||
sortBy := "path"
|
||||
sortDir := models.SortDirectionEnumAsc
|
||||
|
||||
findFilter := models.FindFilterType{
|
||||
PerPage: &perPage,
|
||||
Sort: &sortBy,
|
||||
Direction: &sortDir,
|
||||
}
|
||||
|
||||
imageFilter := &models.ImageFilterType{
|
||||
Galleries: &models.MultiCriterionInput{
|
||||
Value: []string{strconv.Itoa(galleryID)},
|
||||
Modifier: models.CriterionModifierIncludes,
|
||||
},
|
||||
}
|
||||
|
||||
if useCoverJpg {
|
||||
imageFilter.Path = &models.StringCriterionInput{
|
||||
Value: coverFilenameSearchString,
|
||||
Modifier: models.CriterionModifierEquals,
|
||||
}
|
||||
}
|
||||
|
||||
imgs, err := Query(ctx, r, imageFilter, &findFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(imgs) > 0 {
|
||||
return imgs[0], nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user