mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 07:36:44 +00:00
feat: set mime type
This commit is contained in:
@ -1,76 +0,0 @@
|
|||||||
// Copyright (c) 2020 Proton Technologies AG
|
|
||||||
//
|
|
||||||
// This file is part of ProtonMail Bridge.
|
|
||||||
//
|
|
||||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package message
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
escape "html"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/andybalholm/cascadia"
|
|
||||||
"golang.org/x/net/html"
|
|
||||||
)
|
|
||||||
|
|
||||||
func plaintextToHTML(text string) (output string) {
|
|
||||||
text = escape.EscapeString(text)
|
|
||||||
text = strings.Replace(text, "\n\r", "<br>", -1)
|
|
||||||
text = strings.Replace(text, "\r\n", "<br>", -1)
|
|
||||||
text = strings.Replace(text, "\n", "<br>", -1)
|
|
||||||
text = strings.Replace(text, "\r", "<br>", -1)
|
|
||||||
|
|
||||||
return "<div>" + text + "</div>"
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripHTML(input string) (stripped string, err error) {
|
|
||||||
reader := strings.NewReader(input)
|
|
||||||
doc, _ := html.Parse(reader)
|
|
||||||
body := cascadia.MustCompile("body").MatchFirst(doc)
|
|
||||||
if body == nil {
|
|
||||||
err = errors.New("failed to find necessary html element")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var buf1 bytes.Buffer
|
|
||||||
if err = html.Render(&buf1, body); err != nil {
|
|
||||||
stripped = input
|
|
||||||
return
|
|
||||||
}
|
|
||||||
stripped = buf1.String()
|
|
||||||
// Handle double body tags edge case.
|
|
||||||
if strings.Index(stripped, "<body") == 0 {
|
|
||||||
startIndex := strings.Index(stripped, ">")
|
|
||||||
if startIndex < 5 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
stripped = stripped[startIndex+1:]
|
|
||||||
// Closing body tag is optional.
|
|
||||||
closingIndex := strings.Index(stripped, "</body>")
|
|
||||||
if closingIndex > -1 {
|
|
||||||
stripped = stripped[:closingIndex]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func addOuterHTMLTags(input string) (output string) {
|
|
||||||
return "<html><head></head><body>" + input + "</body></html>"
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeEmbeddedImageHTML(cid, name string) (output string) {
|
|
||||||
return "<img class=\"proton-embedded\" alt=\"" + name + "\" src=\"cid:" + cid + "\">"
|
|
||||||
}
|
|
||||||
@ -47,10 +47,18 @@ func Parse(r io.Reader, key, keyName string) (m *pmapi.Message, mime, plain stri
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Body, plain, err = collectBodyParts(p); err != nil {
|
var isHTML bool
|
||||||
|
|
||||||
|
if m.Body, plain, isHTML, err = collectBodyParts(p); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isHTML {
|
||||||
|
m.MIMEType = "text/html"
|
||||||
|
} else {
|
||||||
|
m.MIMEType = "text/plain"
|
||||||
|
}
|
||||||
|
|
||||||
if key != "" {
|
if key != "" {
|
||||||
attachPublicKey(p.Root(), key, keyName)
|
attachPublicKey(p.Root(), key, keyName)
|
||||||
}
|
}
|
||||||
@ -84,7 +92,7 @@ func collectAttachments(p *parser.Parser) (atts []*pmapi.Attachment, data []io.R
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectBodyParts(p *parser.Parser) (body, plain string, err error) {
|
func collectBodyParts(p *parser.Parser) (body, plain string, isHTML bool, err error) {
|
||||||
var parts, plainParts []string
|
var parts, plainParts []string
|
||||||
|
|
||||||
w := p.
|
w := p.
|
||||||
@ -96,6 +104,7 @@ func collectBodyParts(p *parser.Parser) (body, plain string, err error) {
|
|||||||
}).
|
}).
|
||||||
WithContentTypeHandler("text/html", func(p *parser.Part) (err error) {
|
WithContentTypeHandler("text/html", func(p *parser.Part) (err error) {
|
||||||
parts = append(parts, string(p.Body))
|
parts = append(parts, string(p.Body))
|
||||||
|
isHTML = true
|
||||||
|
|
||||||
text, err := html2text.FromString(string(p.Body))
|
text, err := html2text.FromString(string(p.Body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,7 +119,7 @@ func collectBodyParts(p *parser.Parser) (body, plain string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(parts, "\r\n"), strings.Join(plainParts, "\r\n"), nil
|
return strings.Join(parts, "\r\n"), strings.Join(plainParts, "\r\n"), isHTML, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeMIMEMessage(p *parser.Parser) (mime string, err error) {
|
func writeMIMEMessage(p *parser.Parser) (mime string, err error) {
|
||||||
|
|||||||
@ -7,6 +7,13 @@ import (
|
|||||||
"github.com/emersion/go-message"
|
"github.com/emersion/go-message"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO: Set this to something that handles charsets.
|
||||||
|
func init() { // nolint[gochecknoinits]
|
||||||
|
message.CharsetReader = func(string, io.Reader) (io.Reader, error) {
|
||||||
|
panic("not implemented")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
stack []*Part
|
stack []*Part
|
||||||
root *Part
|
root *Part
|
||||||
|
|||||||
Reference in New Issue
Block a user