From 56530a8a66937194fb4e99af95bcea6bb0281f66 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 18 May 2022 10:59:07 -0600 Subject: Implement asset deletion and fix redirect logic --- srv/src/api/assets.go | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'srv/src/api/assets.go') diff --git a/srv/src/api/assets.go b/srv/src/api/assets.go index c0a6fd9..7065ff6 100644 --- a/srv/src/api/assets.go +++ b/srv/src/api/assets.go @@ -50,12 +50,19 @@ func resizeImage(out io.Writer, in io.Reader, maxWidth float64) error { } } -func (a *api) servePostAssetHandler() http.Handler { +func (a *api) getPostAssetHandler() http.Handler { + + renderHandler := a.renderPostAssetsIndexHandler() return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := filepath.Base(r.URL.Path) + if id == "/" { + renderHandler.ServeHTTP(rw, r) + return + } + maxWidth, err := apiutil.StrToInt(r.FormValue("w"), 0) if err != nil { apiutil.BadRequest(rw, r, fmt.Errorf("invalid w parameter: %w", err)) @@ -112,9 +119,7 @@ func (a *api) servePostAssetHandler() http.Handler { }) } -func (a *api) uploadPostAssetHandler() http.Handler { - - renderIndex := a.renderPostAssetsIndexHandler() +func (a *api) postPostAssetHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { @@ -136,6 +141,33 @@ func (a *api) uploadPostAssetHandler() http.Handler { return } - renderIndex.ServeHTTP(rw, r) + a.executeRedirectTpl(rw, r, "assets/") + }) +} + +func (a *api) deletePostAssetHandler() http.Handler { + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := filepath.Base(r.URL.Path) + + if id == "" { + apiutil.BadRequest(rw, r, errors.New("id is required")) + return + } + + err := a.params.PostAssetStore.Delete(id) + + if errors.Is(err, post.ErrAssetNotFound) { + http.Error(rw, "Asset not found", 404) + return + } else if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("deleting asset with id %q: %w", id, err), + ) + return + } + + a.executeRedirectTpl(rw, r, "assets/") }) } -- cgit v1.2.3