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
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
|
|
|
"github.com/stashapp/stash/internal/static"
|
|
|
|
"github.com/stashapp/stash/pkg/hash"
|
2021-08-10 03:51:31 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2022-10-31 03:58:01 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
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-11-17 00:58:49 +00:00
|
|
|
var imageExtensions = []string{
|
|
|
|
".jpg",
|
|
|
|
".jpeg",
|
|
|
|
".png",
|
|
|
|
".gif",
|
|
|
|
".svg",
|
|
|
|
".webp",
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-09-14 04:20:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-17 00:58:49 +00:00
|
|
|
if d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
baseName := strings.ToLower(d.Name())
|
|
|
|
for _, ext := range imageExtensions {
|
|
|
|
if strings.HasSuffix(baseName, ext) {
|
|
|
|
ret.files = append(ret.files, path)
|
|
|
|
break
|
|
|
|
}
|
2021-09-22 03:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-10-31 03:58:01 +00:00
|
|
|
func getRandomPerformerImageUsingName(name string, gender models.GenderEnum, customPath string) ([]byte, error) {
|
2021-08-10 03:51:31 +00:00
|
|
|
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 {
|
2022-10-31 03:58:01 +00:00
|
|
|
switch gender {
|
|
|
|
case models.GenderEnumFemale:
|
2021-08-10 03:51:31 +00:00
|
|
|
box = performerBox
|
2022-10-31 03:58:01 +00:00
|
|
|
case models.GenderEnumMale:
|
2021-08-10 03:51:31 +00:00
|
|
|
box = performerBoxMale
|
|
|
|
default:
|
|
|
|
box = performerBox
|
|
|
|
}
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
2021-08-10 03:51:31 +00:00
|
|
|
|
|
|
|
imageFiles := box.files
|
2022-03-17 00:33:59 +00:00
|
|
|
index := hash.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
|
|
|
}
|