2019-08-24 07:17:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-09-27 00:55:23 +00:00
|
|
|
"io"
|
2021-09-22 03:08:34 +00:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2020-04-24 23:54:42 +00:00
|
|
|
"strings"
|
2019-08-24 07:17:25 +00:00
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/manager/config"
|
2021-09-22 03:08:34 +00:00
|
|
|
"github.com/stashapp/stash/pkg/static"
|
2020-08-11 23:19:27 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-08-24 07:17:25 +00:00
|
|
|
)
|
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
type imageBox struct {
|
2021-09-22 03:08:34 +00:00
|
|
|
box fs.FS
|
2021-08-10 03:51:31 +00:00
|
|
|
files []string
|
|
|
|
}
|
2019-08-24 07:17:25 +00:00
|
|
|
|
2021-09-22 03:08:34 +00:00
|
|
|
func newImageBox(box fs.FS) (*imageBox, error) {
|
|
|
|
ret := &imageBox{
|
|
|
|
box: box,
|
2021-08-10 03:51:31 +00:00
|
|
|
}
|
2021-09-22 03:08:34 +00:00
|
|
|
|
|
|
|
err := fs.WalkDir(box, ".", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if !d.IsDir() {
|
|
|
|
ret.files = append(ret.files, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return ret, err
|
2019-08-24 07:17:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
var performerBox *imageBox
|
|
|
|
var performerBoxMale *imageBox
|
|
|
|
var performerBoxCustom *imageBox
|
2020-04-24 23:54:42 +00:00
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
func initialiseImages() {
|
2021-09-22 03:08:34 +00:00
|
|
|
var err error
|
|
|
|
performerBox, err = newImageBox(&static.Performer)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("error loading performer images: %v", err)
|
|
|
|
}
|
|
|
|
performerBoxMale, err = newImageBox(&static.PerformerMale)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("error loading male performer images: %v", err)
|
|
|
|
}
|
2021-08-10 03:51:31 +00:00
|
|
|
initialiseCustomImages()
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-09-22 03:08:34 +00:00
|
|
|
var err error
|
|
|
|
performerBoxCustom, err = newImageBox(os.DirFS(customPath))
|
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("error loading custom performer from %s: %v", customPath, err)
|
|
|
|
}
|
2021-08-10 03:51:31 +00:00
|
|
|
} else {
|
|
|
|
performerBoxCustom = nil
|
2020-04-24 23:54:42 +00:00
|
|
|
}
|
2019-08-24 07:17:25 +00:00
|
|
|
}
|
2020-08-11 23:19:27 +00:00
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
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
|
|
|
|
}
|
2020-08-11 23:19:27 +00:00
|
|
|
|
2021-08-10 03:51:31 +00:00
|
|
|
if box == nil {
|
|
|
|
switch strings.ToUpper(gender) {
|
|
|
|
case "FEMALE":
|
|
|
|
box = performerBox
|
|
|
|
case "MALE":
|
|
|
|
box = performerBoxMale
|
|
|
|
default:
|
|
|
|
box = performerBox
|
|
|
|
}
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
2021-08-10 03:51:31 +00:00
|
|
|
|
|
|
|
imageFiles := box.files
|
2020-08-11 23:19:27 +00:00
|
|
|
index := utils.IntFromString(name) % uint64(len(imageFiles))
|
2021-09-22 03:08:34 +00:00
|
|
|
img, err := box.box.Open(imageFiles[index])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer img.Close()
|
|
|
|
|
2021-09-27 00:55:23 +00:00
|
|
|
return io.ReadAll(img)
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|