mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
* Update a number of dependencies (incl. CVE fixes) Includes some dependencies that were upgraded in #4106 as well as a few more dependencies. Some deps that have been upgraded had CVEs. Notably, upgrades deprecated dependencies such as: - `github.com/go-chi/chi` (replaced with `/v5`) - `github.com/gofrs/uuid` (replaced with `/v5`) - `github.com/hashicorp/golang-lru` (replaced with `/v2` which uses generics) * Upgraded a few more deps * lint * reverted yaml library to v2 * remove unnecessary mod replace * Update chromedp Fixes #3733
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
"github.com/stashapp/stash/pkg/models"
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
)
|
|
|
|
type PerformerFinder interface {
|
|
models.PerformerGetter
|
|
GetImage(ctx context.Context, performerID int) ([]byte, error)
|
|
}
|
|
|
|
type performerRoutes struct {
|
|
routes
|
|
performerFinder PerformerFinder
|
|
}
|
|
|
|
func getPerformerRoutes(repo models.Repository) chi.Router {
|
|
return performerRoutes{
|
|
routes: routes{txnManager: repo.TxnManager},
|
|
performerFinder: repo.Performer,
|
|
}.Routes()
|
|
}
|
|
|
|
func (rs performerRoutes) Routes() chi.Router {
|
|
r := chi.NewRouter()
|
|
|
|
r.Route("/{performerId}", func(r chi.Router) {
|
|
r.Use(rs.PerformerCtx)
|
|
r.Get("/image", rs.Image)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func (rs performerRoutes) Image(w http.ResponseWriter, r *http.Request) {
|
|
performer := r.Context().Value(performerKey).(*models.Performer)
|
|
defaultParam := r.URL.Query().Get("default")
|
|
|
|
var image []byte
|
|
if defaultParam != "true" {
|
|
readTxnErr := rs.withReadTxn(r, func(ctx context.Context) error {
|
|
var err error
|
|
image, err = rs.performerFinder.GetImage(ctx, performer.ID)
|
|
return err
|
|
})
|
|
if errors.Is(readTxnErr, context.Canceled) {
|
|
return
|
|
}
|
|
if readTxnErr != nil {
|
|
logger.Warnf("read transaction error on fetch performer image: %v", readTxnErr)
|
|
}
|
|
}
|
|
|
|
if len(image) == 0 {
|
|
image = getDefaultPerformerImage(performer.Name, performer.Gender)
|
|
}
|
|
|
|
utils.ServeImage(w, r, image)
|
|
}
|
|
|
|
func (rs performerRoutes) 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
|
|
}
|
|
|
|
var performer *models.Performer
|
|
_ = rs.withReadTxn(r, func(ctx context.Context) error {
|
|
var err error
|
|
performer, err = rs.performerFinder.Find(ctx, performerID)
|
|
return err
|
|
})
|
|
if performer == nil {
|
|
http.Error(w, http.StatusText(404), 404)
|
|
return
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), performerKey, performer)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|