2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-01 00:16:23 +00:00
|
|
|
"errors"
|
2019-02-09 12:30:49 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-11-01 00:16:23 +00:00
|
|
|
"syscall"
|
2020-06-22 23:19:19 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
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/studio"
|
|
|
|
"github.com/stashapp/stash/pkg/txn"
|
2020-07-07 00:35:43 +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 StudioFinder interface {
|
|
|
|
studio.Finder
|
|
|
|
GetImage(ctx context.Context, studioID int) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type studioRoutes struct {
|
2022-05-19 07:49:32 +00:00
|
|
|
txnManager txn.Manager
|
|
|
|
studioFinder StudioFinder
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func (rs studioRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{studioId}", func(r chi.Router) {
|
2022-05-19 07:49:32 +00:00
|
|
|
r.Use(rs.StudioCtx)
|
2019-02-09 12:30:49 +00:00
|
|
|
r.Get("/image", rs.Image)
|
|
|
|
})
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs studioRoutes) Image(w http.ResponseWriter, r *http.Request) {
|
2019-02-14 22:53:32 +00:00
|
|
|
studio := r.Context().Value(studioKey).(*models.Studio)
|
2020-08-11 23:19:27 +00:00
|
|
|
defaultParam := r.URL.Query().Get("default")
|
2020-11-26 21:01:56 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var image []byte
|
2020-11-26 21:01:56 +00:00
|
|
|
if defaultParam != "true" {
|
2022-05-19 07:49:32 +00:00
|
|
|
err := txn.WithTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
|
|
|
image, _ = rs.studioFinder.GetImage(ctx, studio.ID)
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-09-20 23:34:25 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("read transaction error while fetching studio image: %v", err)
|
|
|
|
}
|
2020-11-26 21:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(image) == 0 {
|
2022-03-17 00:33:59 +00:00
|
|
|
image, _ = utils.ProcessBase64Image(models.DefaultStudioImage)
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 23:34:25 +00:00
|
|
|
if err := utils.ServeImage(image, w, r); err != nil {
|
2021-11-01 00:16:23 +00:00
|
|
|
// Broken pipe errors are common when serving images and the remote
|
|
|
|
// connection closes the connection. Filter them out of the error
|
|
|
|
// messages, as they are benign.
|
|
|
|
if !errors.Is(err, syscall.EPIPE) {
|
|
|
|
logger.Warnf("cannot serve studio image: %v", err)
|
|
|
|
}
|
2021-09-20 23:34:25 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (rs studioRoutes) StudioCtx(next http.Handler) http.Handler {
|
2019-02-09 12:30:49 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
studioID, err := strconv.Atoi(chi.URLParam(r, "studioId"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(404), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var studio *models.Studio
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := txn.WithTxn(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
|
|
|
studio, err = rs.studioFinder.Find(ctx, studioID)
|
2021-01-18 01:23:20 +00:00
|
|
|
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(), studioKey, studio)
|
2019-02-09 12:30:49 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|