diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 15:54:20 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-17 15:54:20 -0600 |
commit | 69de76cb32cfd638672d4d5846d0659bf102316f (patch) | |
tree | fd2a1f9fdd815a70eb514b54dbcacc24b8e402cc /srv/src/api/assets.go | |
parent | e406ad6e7c82592d3bbaa1cf93ffc1612e4f196c (diff) |
Add asset file upload form, plus related necessary refactors
Diffstat (limited to 'srv/src/api/assets.go')
-rw-r--r-- | srv/src/api/assets.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/srv/src/api/assets.go b/srv/src/api/assets.go index e94d324..c0a6fd9 100644 --- a/srv/src/api/assets.go +++ b/srv/src/api/assets.go @@ -111,3 +111,31 @@ func (a *api) servePostAssetHandler() http.Handler { }) } + +func (a *api) uploadPostAssetHandler() http.Handler { + + renderIndex := a.renderPostAssetsIndexHandler() + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := r.PostFormValue("id") + if id == "" { + apiutil.BadRequest(rw, r, errors.New("id is required")) + return + } + + file, _, err := r.FormFile("file") + if err != nil { + apiutil.BadRequest(rw, r, fmt.Errorf("reading multipart file: %w", err)) + return + } + defer file.Close() + + if err := a.params.PostAssetStore.Set(id, file); err != nil { + apiutil.InternalServerError(rw, r, fmt.Errorf("storing file: %w", err)) + return + } + + renderIndex.ServeHTTP(rw, r) + }) +} |