Changes to Default Performer Images (for Accessibility) (#1489)

* Changes to support custom URL paths
* Refactor image resolver code
* Initialise box files at startup
* Update packr

Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
capnrowdy
2021-08-10 13:51:31 +10:00
committed by GitHub
parent 3e78d642a2
commit 404eaa32d2
487 changed files with 24741 additions and 29244 deletions

View File

@@ -1,49 +1,67 @@
package api
import (
"math/rand"
"strings"
"github.com/gobuffalo/packr/v2"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/utils"
)
var performerBox *packr.Box
var performerBoxMale *packr.Box
type imageBox struct {
box *packr.Box
files []string
}
func newImageBox(box *packr.Box) *imageBox {
return &imageBox{
box: box,
files: box.List(),
}
}
var performerBox *imageBox
var performerBoxMale *imageBox
var performerBoxCustom *imageBox
func initialiseImages() {
performerBox = packr.New("Performer Box", "../../static/performer")
performerBoxMale = packr.New("Male Performer Box", "../../static/performer_male")
performerBox = newImageBox(packr.New("Performer Box", "../../static/performer"))
performerBoxMale = newImageBox(packr.New("Male Performer Box", "../../static/performer_male"))
initialiseCustomImages()
}
func getRandomPerformerImage(gender string) ([]byte, error) {
var box *packr.Box
switch strings.ToUpper(gender) {
case "FEMALE":
box = performerBox
case "MALE":
box = performerBoxMale
default:
box = performerBox
func initialiseCustomImages() {
customPath := config.GetInstance().GetCustomPerformerImageLocation()
if customPath != "" {
logger.Debugf("Loading custom performer images from %s", customPath)
// We need to set performerBoxCustom at runtime, as this is a custom path, and store it in a pointer.
performerBoxCustom = newImageBox(packr.Folder(customPath))
} else {
performerBoxCustom = nil
}
imageFiles := box.List()
index := rand.Intn(len(imageFiles))
return box.Find(imageFiles[index])
}
func getRandomPerformerImageUsingName(name, gender string) ([]byte, error) {
var box *packr.Box
switch strings.ToUpper(gender) {
case "FEMALE":
box = performerBox
case "MALE":
box = performerBoxMale
default:
box = performerBox
func getRandomPerformerImageUsingName(name, gender, customPath string) ([]byte, error) {
var box *imageBox
// If we have a custom path, we should return a new box in the given path.
if performerBoxCustom != nil && len(performerBoxCustom.files) > 0 {
box = performerBoxCustom
}
imageFiles := box.List()
if box == nil {
switch strings.ToUpper(gender) {
case "FEMALE":
box = performerBox
case "MALE":
box = performerBoxMale
default:
box = performerBox
}
}
imageFiles := box.files
index := utils.IntFromString(name) % uint64(len(imageFiles))
return box.Find(imageFiles[index])
return box.box.Find(imageFiles[index])
}