From e129ea922e6cbe8966f9a9f0b1c6c94401516c61 Mon Sep 17 00:00:00 2001 From: r Date: Sat, 14 Dec 2019 20:19:02 +0000 Subject: Add attachments support --- service/transport.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'service/transport.go') 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] +} -- cgit v1.2.3