2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-31 22:20:14 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2019-02-09 12:30:49 +00:00
|
|
|
"github.com/go-chi/chi"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2019-02-09 12:30:49 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-01-31 22:20:14 +00:00
|
|
|
"strings"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type performerRoutes struct{}
|
|
|
|
|
|
|
|
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-01-31 22:20:14 +00:00
|
|
|
etag := fmt.Sprintf("%x", md5.Sum(performer.Image))
|
|
|
|
|
|
|
|
if match := r.Header.Get("If-None-Match"); match != "" {
|
|
|
|
if strings.Contains(match, etag) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Etag", etag)
|
2019-02-09 12:30:49 +00:00
|
|
|
_, _ = w.Write(performer.Image)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
qb := models.NewPerformerQueryBuilder()
|
|
|
|
performer, err := qb.Find(performerID)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
}
|