diff --git a/controllers/api.go b/controllers/api.go index 9df28949..c0565a37 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -1,12 +1,14 @@ package controllers import ( + "bytes" "crypto/tls" "encoding/json" "errors" "fmt" "net/http" "strconv" + "strings" "text/template" "time" @@ -446,11 +448,38 @@ func API_Import_Email(w http.ResponseWriter, r *http.Request) { JSONResponse(w, models.Response{Success: false, Message: "Method not allowed"}, http.StatusBadRequest) return } - defer r.Body.Close() - e, err := email.NewEmailFromReader(r.Body) + ir := struct { + Content string `json:"content"` + ConvertLinks bool `json:"convert_links"` + }{} + err := json.NewDecoder(r.Body).Decode(&ir) + if err != nil { + JSONResponse(w, models.Response{Success: false, Message: "Error decoding JSON Request"}, http.StatusBadRequest) + return + } + e, err := email.NewEmailFromReader(strings.NewReader(ir.Content)) if err != nil { Logger.Println(err) } + // If the user wants to convert links to point to + // the landing page, let's make it happen by changing up + // e.HTML + if ir.ConvertLinks { + 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), diff --git a/static/js/app/templates.js b/static/js/app/templates.js index 2476144a..738253e6 100644 --- a/static/js/app/templates.js +++ b/static/js/app/templates.js @@ -230,15 +230,19 @@ function copy(idx) { function importEmail() { raw = $("#email_content").val() + convert_links = $("#convert_links_checkbox").prop("checked") if (!raw) { modalError("No Content Specified!") } else { $.ajax({ - type: "POST", + method: "POST", url: "/api/import/email", - data: raw, + data: JSON.stringify({ + content: raw, + convert_links: convert_links + }), dataType: "json", - contentType: "text/plain" + contentType: "application/json" }) .success(function(data) { $("#text_editor").val(data.text) diff --git a/templates/templates.html b/templates/templates.html index 0b3ed97d..ce2f4f40 100644 --- a/templates/templates.html +++ b/templates/templates.html @@ -140,6 +140,10 @@