2020-03-10 03:28:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager"
|
2020-03-10 03:28:15 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2020-06-22 23:19:19 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2020-03-10 03:28:15 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type movieRoutes struct {
|
|
|
|
txnManager models.TransactionManager
|
|
|
|
}
|
2020-03-10 03:28:15 +00:00
|
|
|
|
|
|
|
func (rs movieRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{movieId}", func(r chi.Router) {
|
|
|
|
r.Use(MovieCtx)
|
|
|
|
r.Get("/frontimage", rs.FrontImage)
|
|
|
|
r.Get("/backimage", rs.BackImage)
|
|
|
|
})
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs movieRoutes) FrontImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
movie := r.Context().Value(movieKey).(*models.Movie)
|
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.Movie().GetFrontImage(movie.ID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(image) == 0 {
|
2020-08-11 23:19:27 +00:00
|
|
|
_, image, _ = utils.ProcessBase64Image(models.DefaultMovieImage)
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
utils.ServeImage(image, w, r)
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs movieRoutes) BackImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
movie := r.Context().Value(movieKey).(*models.Movie)
|
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.Movie().GetBackImage(movie.ID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(image) == 0 {
|
2020-08-11 23:19:27 +00:00
|
|
|
_, image, _ = utils.ProcessBase64Image(models.DefaultMovieImage)
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
utils.ServeImage(image, w, r)
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MovieCtx(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
movieID, err := strconv.Atoi(chi.URLParam(r, "movieId"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(404), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var movie *models.Movie
|
|
|
|
if err := manager.GetInstance().TxnManager.WithReadTxn(r.Context(), func(repo models.ReaderRepository) error {
|
|
|
|
var err error
|
|
|
|
movie, err = repo.Movie().Find(movieID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
2020-03-10 03:28:15 +00:00
|
|
|
http.Error(w, http.StatusText(404), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), movieKey, movie)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|