diff --git a/controllers/api/import.go b/controllers/api/import.go index efaf0178..b00fe76d 100644 --- a/controllers/api/import.go +++ b/controllers/api/import.go @@ -49,6 +49,44 @@ func (as *Server) ImportGroup(w http.ResponseWriter, r *http.Request) { JSONResponse(w, ts, http.StatusOK) } +// ImportEmailFile allows for the importing of email. +// Returns a Message object +func (as *Server) ImportEmailFile(w http.ResponseWriter, r *http.Request) { + + if r.Method != "POST" { + JSONResponse(w, models.Response{Success: false, Message: "Method not allowed"}, http.StatusBadRequest) + return + } + mr, nil := r.MultipartReader() + part, nil := mr.NextPart() + e, err := email.NewEmailFromReader(part) + if err != nil { + log.Error(err) + } + d, err := goquery.NewDocumentFromReader(bytes.NewReader(e.HTML)) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) + return + } + d.Find("a").Each(func(i int, a *goquery.Selection) { + a.SetAttr("href", "{{.URL}}") + }) + h, err := d.Html() + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) + return + } + e.HTML = []byte(h) + + er := emailResponse{ + Subject: e.Subject, + Text: string(e.Text), + HTML: string(e.HTML), + } + JSONResponse(w, er, http.StatusOK) + return +} + // ImportEmail allows for the importing of email. // Returns a Message object func (as *Server) ImportEmail(w http.ResponseWriter, r *http.Request) { diff --git a/controllers/api/server.go b/controllers/api/server.go index 4e21cf4b..67866141 100644 --- a/controllers/api/server.go +++ b/controllers/api/server.go @@ -82,6 +82,7 @@ func (as *Server) registerRoutes() { router.HandleFunc("/util/send_test_email", as.SendTestEmail) router.HandleFunc("/import/group", as.ImportGroup) router.HandleFunc("/import/email", as.ImportEmail) + router.HandleFunc("/import/emailfile", as.ImportEmailFile) router.HandleFunc("/import/site", as.ImportSite) router.HandleFunc("/webhooks/", mid.Use(as.Webhooks, mid.RequirePermission(models.PermissionModifySystem))) router.HandleFunc("/webhooks/{id:[0-9]+}/validate", mid.Use(as.ValidateWebhook, mid.RequirePermission(models.PermissionModifySystem))) diff --git a/static/js/src/app/templates.js b/static/js/src/app/templates.js index 154db7b8..817bff80 100644 --- a/static/js/src/app/templates.js +++ b/static/js/src/app/templates.js @@ -222,6 +222,36 @@ function edit(idx) { .remove() .draw(); }) + + // Handle file uploads + $("#emlupload").fileupload({ + url: "/api/import/emailfile", + dataType: "json", + beforeSend: function (xhr) { + xhr.setRequestHeader('Authorization', 'Bearer ' + user.api_key); + }, + add: function (e, data) { + $("#modal\\.flashes").empty() + var acceptFileTypes = /(eml)$/i; + var filename = data.originalFiles[0]['name'] + if (filename && !acceptFileTypes.test(filename.split(".").pop())) { + modalError("Unsupported file extension (use .eml)") + return false; + } + data.submit(); + }, + success: function (data) { + $("#text_editor").val(data.text) + $("#html_editor").val(data.html) + $("#subject").val(data.subject) + // If the HTML is provided, let's open that view in the editor + if (data.html) { + CKEDITOR.instances["html_editor"].setMode('wysiwyg') + $('.nav-tabs a[href="#html"]').click() + } + $("#importEmailModal").modal("hide") + } + }) } function copy(idx) { diff --git a/templates/templates.html b/templates/templates.html index 2a348019..27b31cce 100644 --- a/templates/templates.html +++ b/templates/templates.html @@ -52,8 +52,11 @@