stash/internal/api/routes_downloads.go
DingDongSoLong4 33f2ebf2a3
Model refactor, part 3 (#4152)
* Remove manager.Repository
* Refactor other repositories
* Fix tests and add database mock
* Add AssertExpectations method
* Refactor routes
* Move default movie image to internal/static and add convenience methods
* Refactor default performer image boxes
2023-10-16 14:26:34 +11:00

45 lines
959 B
Go

package api
import (
"context"
"net/http"
"github.com/go-chi/chi"
"github.com/stashapp/stash/internal/manager"
)
type downloadsRoutes struct{}
func getDownloadsRoutes() chi.Router {
return downloadsRoutes{}.Routes()
}
func (rs downloadsRoutes) Routes() chi.Router {
r := chi.NewRouter()
r.Route("/{downloadHash}", func(r chi.Router) {
r.Use(downloadCtx)
r.Get("/{filename}", rs.file)
})
return r
}
func (rs downloadsRoutes) file(w http.ResponseWriter, r *http.Request) {
hash := r.Context().Value(downloadKey).(string)
if hash == "" {
http.Error(w, http.StatusText(404), 404)
return
}
manager.GetInstance().DownloadStore.Serve(hash, w, r)
}
func downloadCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
downloadHash := chi.URLParam(r, "downloadHash")
ctx := context.WithValue(r.Context(), downloadKey, downloadHash)
next.ServeHTTP(w, r.WithContext(ctx))
})
}