2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-06-22 23:19:19 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager"
|
2021-08-10 03:51:31 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager/config"
|
2020-06-22 23:19:19 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type performerRoutes struct {
|
|
|
|
txnManager models.TransactionManager
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func (rs performerRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{performerId}", func(r chi.Router) {
|
|
|
|
r.Use(PerformerCtx)
|
|
|
|
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" {
|
|
|
|
rs.txnManager.WithReadTxn(r.Context(), func(repo models.ReaderRepository) error {
|
|
|
|
image, _ = repo.Performer().GetImage(performer.ID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-11 23:19:27 +00:00
|
|
|
if len(image) == 0 || defaultParam == "true" {
|
2021-08-10 03:51:31 +00:00
|
|
|
image, _ = getRandomPerformerImageUsingName(performer.Name.String, performer.Gender.String, config.GetInstance().GetCustomPerformerImageLocation())
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
utils.ServeImage(image, w, r)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PerformerCtx(next http.Handler) http.Handler {
|
|
|
|
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
|
|
|
|
if err := manager.GetInstance().TxnManager.WithReadTxn(r.Context(), func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
performer, err = repo.Performer().Find(performerID)
|
|
|
|
return err
|
|
|
|
}); err != 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
|
|
|
}
|