diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-18 10:59:07 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-18 10:59:07 -0600 |
commit | 56530a8a66937194fb4e99af95bcea6bb0281f66 (patch) | |
tree | 9546829e36ac35a0daaed33d2f0ffc73fa85f4ad /srv/src/api/assets.go | |
parent | 69de76cb32cfd638672d4d5846d0659bf102316f (diff) |
Implement asset deletion and fix redirect logic
Diffstat (limited to 'srv/src/api/assets.go')
-rw-r--r-- | srv/src/api/assets.go | 42 |
1 files changed, 37 insertions, 5 deletions
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/") }) } |