From 22f881e7eae6fd0f639a1104e8b07bd099238019 Mon Sep 17 00:00:00 2001 From: David Maciejak Date: Sat, 7 Sep 2019 12:34:13 +0800 Subject: [PATCH] Import email templates from file --- controllers/api/import.go | 38 ++++++++++++++++++++++++++++++++++ controllers/api/server.go | 1 + static/js/src/app/templates.js | 32 +++++++++++++++++++++++++++- templates/templates.html | 9 +++++--- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/controllers/api/import.go b/controllers/api/import.go index f1b164d9..d521aa1b 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) { return } +// 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 9f2bec65..fe1b68b4 100644 --- a/controllers/api/server.go +++ b/controllers/api/server.go @@ -70,6 +70,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) as.handler = router } diff --git a/static/js/src/app/templates.js b/static/js/src/app/templates.js index 0d8927b5..c3bc38c0 100644 --- a/static/js/src/app/templates.js +++ b/static/js/src/app/templates.js @@ -213,6 +213,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) { @@ -406,4 +436,4 @@ $(document).ready(function () { }); load() -}) \ No newline at end of file +}) diff --git a/templates/templates.html b/templates/templates.html index 6ef478b5..a4e0145c 100644 --- a/templates/templates.html +++ b/templates/templates.html @@ -52,8 +52,11 @@
+ class="fa fa-paste"> + Paste Raw Email +
@@ -137,4 +140,4 @@ -{{end}} \ No newline at end of file +{{end}}