GODT-1141 Use attachment name from content type if not specified in content disposition

This commit is contained in:
Michal Horejsek
2021-04-19 08:21:18 +02:00
committed by Jakub Cuth
parent 4038752a9a
commit ee961ae4a8
4 changed files with 56 additions and 5 deletions

View File

@ -503,12 +503,15 @@ func parseAttachment(h message.Header) (*pmapi.Attachment, error) {
}
att.Header = mimeHeader
mimeType, _, err := h.ContentType()
mimeType, mimeTypeParams, err := h.ContentType()
if err != nil {
return nil, err
}
att.MIMEType = mimeType
// Prefer attachment name from filename param in content disposition.
// If not available, try to get it from name param in content type.
// Otherwise fallback to attachment.bin.
_, dispParams, dispErr := h.ContentDisposition()
if dispErr != nil {
ext, err := mime.ExtensionsByType(att.MIMEType)
@ -521,10 +524,12 @@ func parseAttachment(h message.Header) (*pmapi.Attachment, error) {
}
} else {
att.Name = dispParams["filename"]
if att.Name == "" {
att.Name = "attachment.bin"
}
}
if att.Name == "" {
att.Name = mimeTypeParams["name"]
}
if att.Name == "" {
att.Name = "attachment.bin"
}
// Only set ContentID if it should be inline;

View File

@ -239,6 +239,24 @@ func TestParseTextPlainWithOctetAttachmentBadFilename(t *testing.T) {
assert.Equal(t, "attachment.bin", m.Attachments[0].Name)
}
func TestParseTextPlainWithOctetAttachmentNameInContentType(t *testing.T) {
f := getFileReader("text_plain_octet_attachment_name_in_contenttype.eml")
m, _, _, _, err := Parse(f) //nolint[dogsled]
require.NoError(t, err)
assert.Equal(t, "attachment-contenttype.txt", m.Attachments[0].Name)
}
func TestParseTextPlainWithOctetAttachmentNameConflict(t *testing.T) {
f := getFileReader("text_plain_octet_attachment_name_conflict.eml")
m, _, _, _, err := Parse(f) //nolint[dogsled]
require.NoError(t, err)
assert.Equal(t, "attachment-disposition.txt", m.Attachments[0].Name)
}
func TestParseTextPlainWithPlainAttachment(t *testing.T) {
f := getFileReader("text_plain_plain_attachment.eml")

View File

@ -0,0 +1,14 @@
From: Sender <sender@pm.me>
To: Receiver <receiver@pm.me>
Content-Type: multipart/mixed; boundary=longrandomstring
--longrandomstring
body
--longrandomstring
Content-Type: application/octet-stream; name="attachment-contenttype.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="attachment-disposition.txt"
aWYgeW91IGFyZSByZWFkaW5nIHRoaXMsIGhpIQ==
--longrandomstring--

View File

@ -0,0 +1,14 @@
From: Sender <sender@pm.me>
To: Receiver <receiver@pm.me>
Content-Type: multipart/mixed; boundary=longrandomstring
--longrandomstring
body
--longrandomstring
Content-Type: application/octet-stream; name="attachment-contenttype.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
aWYgeW91IGFyZSByZWFkaW5nIHRoaXMsIGhpIQ==
--longrandomstring--