forked from Silverfish/proton-bridge
GODT-1141 Use attachment name from content type if not specified in content disposition
This commit is contained in:
committed by
Jakub Cuth
parent
4038752a9a
commit
ee961ae4a8
@ -503,12 +503,15 @@ func parseAttachment(h message.Header) (*pmapi.Attachment, error) {
|
|||||||
}
|
}
|
||||||
att.Header = mimeHeader
|
att.Header = mimeHeader
|
||||||
|
|
||||||
mimeType, _, err := h.ContentType()
|
mimeType, mimeTypeParams, err := h.ContentType()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
att.MIMEType = mimeType
|
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()
|
_, dispParams, dispErr := h.ContentDisposition()
|
||||||
if dispErr != nil {
|
if dispErr != nil {
|
||||||
ext, err := mime.ExtensionsByType(att.MIMEType)
|
ext, err := mime.ExtensionsByType(att.MIMEType)
|
||||||
@ -521,11 +524,13 @@ func parseAttachment(h message.Header) (*pmapi.Attachment, error) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
att.Name = dispParams["filename"]
|
att.Name = dispParams["filename"]
|
||||||
|
}
|
||||||
|
if att.Name == "" {
|
||||||
|
att.Name = mimeTypeParams["name"]
|
||||||
|
}
|
||||||
if att.Name == "" {
|
if att.Name == "" {
|
||||||
att.Name = "attachment.bin"
|
att.Name = "attachment.bin"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Only set ContentID if it should be inline;
|
// Only set ContentID if it should be inline;
|
||||||
// API infers content disposition based on whether ContentID is present.
|
// API infers content disposition based on whether ContentID is present.
|
||||||
|
|||||||
@ -239,6 +239,24 @@ func TestParseTextPlainWithOctetAttachmentBadFilename(t *testing.T) {
|
|||||||
assert.Equal(t, "attachment.bin", m.Attachments[0].Name)
|
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) {
|
func TestParseTextPlainWithPlainAttachment(t *testing.T) {
|
||||||
f := getFileReader("text_plain_plain_attachment.eml")
|
f := getFileReader("text_plain_plain_attachment.eml")
|
||||||
|
|
||||||
|
|||||||
14
pkg/message/testdata/text_plain_octet_attachment_name_conflict.eml
vendored
Normal file
14
pkg/message/testdata/text_plain_octet_attachment_name_conflict.eml
vendored
Normal 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--
|
||||||
14
pkg/message/testdata/text_plain_octet_attachment_name_in_contenttype.eml
vendored
Normal file
14
pkg/message/testdata/text_plain_octet_attachment_name_in_contenttype.eml
vendored
Normal 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--
|
||||||
Reference in New Issue
Block a user