2020-03-10 03:28:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-19 05:01:40 +00:00
|
|
|
"errors"
|
2020-03-10 03:28:15 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-09-20 23:34:25 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2020-03-10 03:28:15 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/txn"
|
2020-06-22 23:19:19 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2020-03-10 03:28:15 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type MovieFinder interface {
|
|
|
|
GetFrontImage(ctx context.Context, movieID int) ([]byte, error)
|
|
|
|
GetBackImage(ctx context.Context, movieID int) ([]byte, error)
|
|
|
|
Find(ctx context.Context, id int) (*models.Movie, error)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type movieRoutes struct {
|
2022-05-19 07:49:32 +00:00
|
|
|
txnManager txn.Manager
|
|
|
|
movieFinder MovieFinder
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2020-03-10 03:28:15 +00:00
|
|
|
|
|
|
|
func (rs movieRoutes) Routes() chi.Router {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Route("/{movieId}", func(r chi.Router) {
|
2022-05-19 07:49:32 +00:00
|
|
|
r.Use(rs.MovieCtx)
|
2020-03-10 03:28:15 +00:00
|
|
|
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" {
|
2022-11-20 19:49:10 +00:00
|
|
|
readTxnErr := txn.WithReadTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
var err error
|
|
|
|
image, err = rs.movieFinder.GetFrontImage(ctx, movie.ID)
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
})
|
2022-09-19 05:01:40 +00:00
|
|
|
if errors.Is(readTxnErr, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if readTxnErr != nil {
|
|
|
|
logger.Warnf("read transaction error on fetch movie front image: %v", readTxnErr)
|
2021-09-20 23:34:25 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(image) == 0 {
|
2022-03-17 00:33:59 +00:00
|
|
|
image, _ = utils.ProcessBase64Image(models.DefaultMovieImage)
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 03:01:32 +00:00
|
|
|
utils.ServeImage(w, r, image)
|
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" {
|
2022-11-20 19:49:10 +00:00
|
|
|
readTxnErr := txn.WithReadTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
var err error
|
|
|
|
image, err = rs.movieFinder.GetBackImage(ctx, movie.ID)
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
})
|
2022-09-19 05:01:40 +00:00
|
|
|
if errors.Is(readTxnErr, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if readTxnErr != nil {
|
|
|
|
logger.Warnf("read transaction error on fetch movie back image: %v", readTxnErr)
|
2021-09-20 23:34:25 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(image) == 0 {
|
2022-03-17 00:33:59 +00:00
|
|
|
image, _ = utils.ProcessBase64Image(models.DefaultMovieImage)
|
2020-08-11 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 03:01:32 +00:00
|
|
|
utils.ServeImage(w, r, image)
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (rs movieRoutes) MovieCtx(next http.Handler) http.Handler {
|
2020-03-10 03:28:15 +00:00
|
|
|
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
|
2022-11-20 19:49:10 +00:00
|
|
|
_ = txn.WithReadTxn(r.Context(), rs.txnManager, func(ctx context.Context) error {
|
2022-09-19 05:01:40 +00:00
|
|
|
movie, _ = rs.movieFinder.Find(ctx, movieID)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if movie == 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))
|
|
|
|
})
|
|
|
|
}
|