2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-19 05:01:40 +00:00
|
|
|
"errors"
|
2019-02-09 12:30:49 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-06-22 23:19:19 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
2021-09-20 23:34:25 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2020-06-22 23:19:19 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/txn"
|
2020-06-22 23:19:19 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type PerformerFinder interface {
|
2023-09-01 00:39:29 +00:00
|
|
|
models.PerformerGetter
|
2022-05-19 07:49:32 +00:00
|
|
|
GetImage(ctx context.Context, performerID int) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type performerRoutes struct {
|
2022-05-19 07:49:32 +00:00
|
|
|
txnManager txn.Manager
|
|
|
|
performerFinder PerformerFinder
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func (rs performerRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{performerId}", func(r chi.Router) {
|
2022-05-19 07:49:32 +00:00
|
|
|
r.Use(rs.PerformerCtx)
|
2019-02-09 12:30:49 +00:00
|
|
|
r.Get("/image", rs.Image)
|
|
|
|
})
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs performerRoutes) Image(w http.ResponseWriter, r *http.Request) {
|
2019-02-14 22:53:32 +00:00
|
|
|
performer := r.Context().Value(performerKey).(*models.Performer)
|
2020-08-11 23:19:27 +00:00
|
|
|
defaultParam := r.URL.Query().Get("default")
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
var image []byte
|
|
|
|
if defaultParam != "true" {
|
2022-11-20 19:49:10 +00:00
|
|
|
readTxnErr := txn.WithReadTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
var err error
|
|
|
|
image, err = rs.performerFinder.GetImage(ctx, performer.ID)
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
})
|
2022-09-19 05:01:40 +00:00
|
|
|
if errors.Is(readTxnErr, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
2021-09-20 23:34:25 +00:00
|
|
|
if readTxnErr != nil {
|
2022-09-19 05:01:40 +00:00
|
|
|
logger.Warnf("read transaction error on fetch performer image: %v", readTxnErr)
|
2021-09-20 23:34:25 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 03:01:32 +00:00
|
|
|
if len(image) == 0 {
|
2022-10-31 03:58:01 +00:00
|
|
|
image, _ = getRandomPerformerImageUsingName(performer.Name, performer.Gender, config.GetInstance().GetCustomPerformerImageLocation())
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 03:01:32 +00:00
|
|
|
utils.ServeImage(w, r, image)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (rs performerRoutes) PerformerCtx(next http.Handler) http.Handler {
|
2019-02-09 12:30:49 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
performerID, err := strconv.Atoi(chi.URLParam(r, "performerId"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(404), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var performer *models.Performer
|
2022-11-20 19:49:10 +00:00
|
|
|
_ = txn.WithReadTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
var err error
|
2022-05-19 07:49:32 +00:00
|
|
|
performer, err = rs.performerFinder.Find(ctx, performerID)
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
2022-09-19 05:01:40 +00:00
|
|
|
})
|
|
|
|
if performer == nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
http.Error(w, http.StatusText(404), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
ctx := context.WithValue(r.Context(), performerKey, performer)
|
2019-02-09 12:30:49 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|