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"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager"
|
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"
|
2020-07-07 00:35:43 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type studioRoutes struct {
|
|
|
|
txnManager models.TransactionManager
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func (rs studioRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{studioId}", func(r chi.Router) {
|
|
|
|
r.Use(StudioCtx)
|
|
|
|
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" {
|
2021-09-20 23:34:25 +00:00
|
|
|
err := rs.txnManager.WithReadTxn(r.Context(), func(repo models.ReaderRepository) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
image, _ = repo.Studio().GetImage(studio.ID)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func StudioCtx(next http.Handler) http.Handler {
|
|
|
|
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
|
|
|
|
if err := manager.GetInstance().TxnManager.WithReadTxn(r.Context(), func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
studio, err = repo.Studio().Find(studioID)
|
|
|
|
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
|
|
|
}
|