fix(GODT-2473): Fix handling of complex mime types

When rebuilding attachments, ensure that more complicated mime types are
properly re-constructed.

If we fail to parse the mime type, set the value as is.
This commit is contained in:
Leander Beernaert
2023-03-10 15:49:10 +01:00
parent 7eaf1655b2
commit b57ca1506d
2 changed files with 49 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import (
"github.com/emersion/go-message"
"github.com/emersion/go-message/textproto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
@ -543,7 +544,17 @@ func getAttachmentPartHeader(att proton.Attachment) message.Header {
hdr := toMessageHeader(att.Headers)
// All attachments have a content type.
hdr.SetContentType(string(att.MIMEType), map[string]string{"name": mime.QEncoding.Encode("utf-8", att.Name)})
mimeType, params, err := mime.ParseMediaType(string(att.MIMEType))
if err != nil {
logrus.WithError(err).Errorf("Failed to parse mime type: '%v'", att.MIMEType)
hdr.Set("Content-Type", string(att.MIMEType))
} else {
// Merge the overridden name into the params
encodedName := mime.QEncoding.Encode("utf-8", att.Name)
params["name"] = encodedName
params["filename"] = encodedName
hdr.SetContentType(mimeType, params)
}
// All attachments have a content disposition.
hdr.SetContentDisposition(string(att.Disposition), map[string]string{"filename": mime.QEncoding.Encode("utf-8", att.Name)})