From 73088ab400b4ac96851c797ee32d1c8058749300 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Sat, 26 Apr 2014 11:50:34 +0100 Subject: [PATCH] Add message download --- mailhog/http/api/v1.go | 39 +++++++++++++++++++++++++++++++++++++- mailhog/templates/index.go | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/mailhog/http/api/v1.go b/mailhog/http/api/v1.go index e8b4386..fa25123 100644 --- a/mailhog/http/api/v1.go +++ b/mailhog/http/api/v1.go @@ -28,6 +28,7 @@ func CreateAPIv1(exitCh chan int, conf *config.Config, server *http.Server) *API server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/delete/?$"), apiv1.delete_all) server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/?$"), apiv1.message) server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/delete/?$"), apiv1.delete_one) + server.Handler.(*handler.RegexpHandler).HandleFunc(regexp.MustCompile("^/api/v1/messages/([0-9a-f]+)/download/?$"), apiv1.download) return apiv1 } @@ -70,8 +71,40 @@ func (apiv1 *APIv1) message(w http.ResponseWriter, r *http.Request, route *handl w.Header().Set("Content-Type", "text/json") w.Write(bytes) default: + // FIXME 404? w.Header().Set("Content-Type", "text/json") - w.Write([]byte("[]")) + w.Write([]byte("{}")) + } +} + +func (apiv1 *APIv1) download(w http.ResponseWriter, r *http.Request, route *handler.Route) { + match := route.Pattern.FindStringSubmatch(r.URL.Path) + id := match[1] + log.Printf("[APIv1] GET /api/v1/messages/%s/download\n", id) + + w.Header().Set("Content-Type", "message/rfc822") + w.Header().Set("Content-Disposition", "attachment; filename=\"" + id + ".eml\"") + + switch apiv1.config.Storage.(type) { + case *storage.MongoDB: + message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id) + for h, l := range message.Content.Headers { + for _, v := range l { + w.Write([]byte(h + ": " + v + "\r\n")) + } + } + w.Write([]byte("\r\n" + message.Content.Body)) + case *storage.Memory: + message, _ := apiv1.config.Storage.(*storage.Memory).Load(id) + for h, l := range message.Content.Headers { + for _, v := range l { + w.Write([]byte(h + ": " + v + "\r\n")) + } + } + w.Write([]byte("\r\n" + message.Content.Body)) + default: + // FIXME 404? + w.Write([]byte("")) } } @@ -84,6 +117,8 @@ func (apiv1 *APIv1) delete_all(w http.ResponseWriter, r *http.Request, route *ha apiv1.config.Storage.(*storage.MongoDB).DeleteAll() case *storage.Memory: apiv1.config.Storage.(*storage.Memory).DeleteAll() + default: + // FIXME 404? } } @@ -98,5 +133,7 @@ func (apiv1 *APIv1) delete_one(w http.ResponseWriter, r *http.Request, route *ha apiv1.config.Storage.(*storage.MongoDB).DeleteOne(id) case *storage.Memory: apiv1.config.Storage.(*storage.Memory).DeleteOne(id) + default: + // FIXME 404? } } diff --git a/mailhog/templates/index.go b/mailhog/templates/index.go index 6ee05ae..296cfe3 100644 --- a/mailhog/templates/index.go +++ b/mailhog/templates/index.go @@ -100,6 +100,7 @@ func Index() string { +