mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
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
|
||||
|
||||
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;
|
||||
|
||||
@ -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")
|
||||
|
||||
|
||||
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