From 2f5d80a52158ab1f2e1cf93295b9c089a44f5b13 Mon Sep 17 00:00:00 2001 From: WiszniewskiMateusz Date: Thu, 29 Dec 2016 19:13:27 +0100 Subject: [PATCH] Added data about message attachments to REST API (#46) --- rest/apiv1_controller.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rest/apiv1_controller.go b/rest/apiv1_controller.go index 5e5d543..f2aecb3 100644 --- a/rest/apiv1_controller.go +++ b/rest/apiv1_controller.go @@ -10,6 +10,10 @@ import ( "github.com/jhillyerd/inbucket/httpd" "github.com/jhillyerd/inbucket/log" "github.com/jhillyerd/inbucket/smtpd" + "strconv" + "crypto/md5" + "encoding/hex" + "io/ioutil" ) // JSONMessageHeaderV1 contains the basic header data for a message @@ -34,6 +38,15 @@ type JSONMessageV1 struct { Size int64 `json:"size"` Body *JSONMessageBodyV1 `json:"body"` Header mail.Header `json:"header"` + Attachments []*JSONMessageAttachmentV1 `json:"attachments"` +} + +type JSONMessageAttachmentV1 struct { + FileName string `json:"filename"` + ContentType string `json:"content-type"` + DownloadLink string `json:"download-link"` + ViewLink string `json:"view-link"` + MD5 string `json:"md5"` } // JSONMessageBodyV1 contains the Text and HTML versions of the message body @@ -107,6 +120,20 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) return fmt.Errorf("ReadBody(%q) failed: %v", id, err) } + attachments := make([]*JSONMessageAttachmentV1, len(mime.Attachments)) + for i, att := range mime.Attachments { + var content []byte + content, err = ioutil.ReadAll(att) + var checksum = md5.Sum(content) + attachments[i] = &JSONMessageAttachmentV1{ + ContentType: att.ContentType, + FileName: att.FileName, + DownloadLink: "http://" + req.Host + "/mailbox/dattach/" + name + "/" + id + "/" + strconv.Itoa(i) + "/" + att.FileName, + ViewLink: "http://" + req.Host + "/mailbox/vattach/" + name + "/" + id + "/" + strconv.Itoa(i) + "/" + att.FileName, + MD5: hex.EncodeToString(checksum[:]), + } + } + return httpd.RenderJSON(w, &JSONMessageV1{ Mailbox: name, @@ -121,6 +148,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) Text: mime.Text, HTML: mime.HTML, }, + Attachments: attachments, }) }