1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

Added data about message attachments to REST API (#46)

This commit is contained in:
WiszniewskiMateusz
2016-12-29 19:13:27 +01:00
committed by James Hillyerd
parent 364e7a0b80
commit 2f5d80a521

View File

@@ -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,
})
}