diff options
author | r <r@freesoftwareextremist.com> | 2019-12-14 20:19:02 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2019-12-14 20:19:02 +0000 |
commit | e129ea922e6cbe8966f9a9f0b1c6c94401516c61 (patch) | |
tree | 35be3bc003b787db5898ac1ba8aacca3f944105e /service/transport.go | |
parent | ea66bd539dec12ef2846c23e505593f9f9d9fac3 (diff) | |
download | bloat-e129ea922e6cbe8966f9a9f0b1c6c94401516c61.tar.gz bloat-e129ea922e6cbe8966f9a9f0b1c6c94401516c61.zip |
Add attachments support
Diffstat (limited to 'service/transport.go')
-rw-r--r-- | service/transport.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/service/transport.go b/service/transport.go index 00f7430..d5a6ee8 100644 --- a/service/transport.go +++ b/service/transport.go @@ -3,6 +3,7 @@ package service import ( "context" "fmt" + "mime/multipart" "net/http" "path" @@ -153,9 +154,18 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) { ctx := getContextWithSession(context.Background(), req) - content := req.FormValue("content") - replyToID := req.FormValue("reply_to_id") - id, err := s.PostTweet(ctx, w, nil, content, replyToID) + + err := req.ParseMultipartForm(4 << 20) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + content := getMultipartFormValue(req.MultipartForm, "content") + replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id") + files := req.MultipartForm.File["attachments"] + + id, err := s.PostTweet(ctx, w, nil, content, replyToID, files) if err != nil { s.ServeErrorPage(ctx, w, err) return @@ -178,3 +188,14 @@ func NewHandler(s Service, staticDir string) http.Handler { return r } + +func getMultipartFormValue(mf *multipart.Form, key string) (val string) { + vals, ok := mf.Value[key] + if !ok { + return "" + } + if len(vals) < 1 { + return "" + } + return vals[0] +} |