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

Move REST JSON model into its own package for #43

This commit is contained in:
James Hillyerd
2017-01-05 05:59:12 +00:00
parent 61c6e7c2e9
commit fa6b0a3227
2 changed files with 56 additions and 50 deletions

View File

@@ -4,57 +4,18 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/mail"
"time"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"github.com/jhillyerd/inbucket/httpd"
"github.com/jhillyerd/inbucket/log"
"github.com/jhillyerd/inbucket/smtpd"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"github.com/jhillyerd/inbucket/httpd"
"github.com/jhillyerd/inbucket/log"
"github.com/jhillyerd/inbucket/rest/model"
"github.com/jhillyerd/inbucket/smtpd"
) )
// JSONMessageHeaderV1 contains the basic header data for a message
type JSONMessageHeaderV1 struct {
Mailbox string `json:"mailbox"`
ID string `json:"id"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Date time.Time `json:"date"`
Size int64 `json:"size"`
}
// JSONMessageV1 contains the same data as the header plus a JSONMessageBody
type JSONMessageV1 struct {
Mailbox string `json:"mailbox"`
ID string `json:"id"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Date time.Time `json:"date"`
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
type JSONMessageBodyV1 struct {
Text string `json:"text"`
HTML string `json:"html"`
}
// MailboxListV1 renders a list of messages in a mailbox // MailboxListV1 renders a list of messages in a mailbox
func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (err error) { func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (err error) {
// Don't have to validate these aren't empty, Gorilla returns 404 // Don't have to validate these aren't empty, Gorilla returns 404
@@ -74,9 +35,9 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
} }
log.Tracef("Got %v messsages", len(messages)) log.Tracef("Got %v messsages", len(messages))
jmessages := make([]*JSONMessageHeaderV1, len(messages)) jmessages := make([]*model.JSONMessageHeaderV1, len(messages))
for i, msg := range messages { for i, msg := range messages {
jmessages[i] = &JSONMessageHeaderV1{ jmessages[i] = &model.JSONMessageHeaderV1{
Mailbox: name, Mailbox: name,
ID: msg.ID(), ID: msg.ID(),
From: msg.From(), From: msg.From(),
@@ -120,12 +81,12 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
return fmt.Errorf("ReadBody(%q) failed: %v", id, err) return fmt.Errorf("ReadBody(%q) failed: %v", id, err)
} }
attachments := make([]*JSONMessageAttachmentV1, len(mime.Attachments)) attachments := make([]*model.JSONMessageAttachmentV1, len(mime.Attachments))
for i, att := range mime.Attachments { for i, att := range mime.Attachments {
var content []byte var content []byte
content, err = ioutil.ReadAll(att) content, err = ioutil.ReadAll(att)
var checksum = md5.Sum(content) var checksum = md5.Sum(content)
attachments[i] = &JSONMessageAttachmentV1{ attachments[i] = &model.JSONMessageAttachmentV1{
ContentType: att.ContentType, ContentType: att.ContentType,
FileName: att.FileName, FileName: att.FileName,
DownloadLink: "http://" + req.Host + "/mailbox/dattach/" + name + "/" + id + "/" + strconv.Itoa(i) + "/" + att.FileName, DownloadLink: "http://" + req.Host + "/mailbox/dattach/" + name + "/" + id + "/" + strconv.Itoa(i) + "/" + att.FileName,
@@ -135,7 +96,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
} }
return httpd.RenderJSON(w, return httpd.RenderJSON(w,
&JSONMessageV1{ &model.JSONMessageV1{
Mailbox: name, Mailbox: name,
ID: msg.ID(), ID: msg.ID(),
From: msg.From(), From: msg.From(),
@@ -144,7 +105,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
Date: msg.Date(), Date: msg.Date(),
Size: msg.Size(), Size: msg.Size(),
Header: header.Header, Header: header.Header,
Body: &JSONMessageBodyV1{ Body: &model.JSONMessageBodyV1{
Text: mime.Text, Text: mime.Text,
HTML: mime.HTML, HTML: mime.HTML,
}, },

45
rest/model/apiv1_model.go Normal file
View File

@@ -0,0 +1,45 @@
package model
import (
"net/mail"
"time"
)
// JSONMessageHeaderV1 contains the basic header data for a message
type JSONMessageHeaderV1 struct {
Mailbox string `json:"mailbox"`
ID string `json:"id"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Date time.Time `json:"date"`
Size int64 `json:"size"`
}
// JSONMessageV1 contains the same data as the header plus a JSONMessageBody
type JSONMessageV1 struct {
Mailbox string `json:"mailbox"`
ID string `json:"id"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Date time.Time `json:"date"`
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
type JSONMessageBodyV1 struct {
Text string `json:"text"`
HTML string `json:"html"`
}