improve on detection of mime-type via Store object. fixes #1919

This commit is contained in:
Muhammad Nuzaihan 2018-04-16 17:58:03 +08:00
parent 7fac7501f7
commit c02393399a
No known key found for this signature in database
GPG key ID: 949F5D5715249C07
2 changed files with 89 additions and 6 deletions

View file

@ -105,13 +105,13 @@ module Channel::EmailBuild
attr[:attachments]&.each do |attachment|
if attachment.class == Hash
attachment['content-id'] = nil
mail.attachments[ attachment[:filename] ] = attachment
mail.attachments[attachment[:filename]] = attachment
else
next if attachment.preferences['Content-ID'].present?
filename = attachment.filename
encoded_filename = Mail::Encodings.decode_encode filename, :encode
disposition = attachment.preferences['Content-Disposition'] || 'attachment'
content_type = attachment.preferences['Content-Type'] || 'application/octet-stream'
content_type = attachment.preferences['Content-Type'] || attachment.preferences['Mime-Type'] || 'application/octet-stream'
mail.attachments[attachment.filename] = {
content_disposition: "#{disposition}; filename=\"#{encoded_filename}\"",
content_type: "#{content_type}; filename=\"#{encoded_filename}\"",

View file

@ -49,8 +49,8 @@ class EmailBuildTest < ActiveSupport::TestCase
{
'Mime-Type' => 'image/png',
:content => 'xxx',
:filename => 'somename.png',
},
:filename => 'somename.png'
}
],
)
@ -103,8 +103,8 @@ class EmailBuildTest < ActiveSupport::TestCase
{
'Mime-Type' => 'image/png',
:content => 'xxx',
:filename => 'somename.png',
},
:filename => 'somename.png'
}
],
)
@ -114,6 +114,7 @@ class EmailBuildTest < ActiveSupport::TestCase
>'
assert_equal(should, mail.text_part.body.to_s)
assert_nil(mail.html_part)
assert_equal('image/png; filename=somename.png', mail.attachments[0].content_type)
parser = Channel::EmailParser.new
data = parser.parse(mail.to_s)
@ -137,6 +138,88 @@ class EmailBuildTest < ActiveSupport::TestCase
end
end
test 'plain email + attachment check 2' do
ticket1 = Ticket.create!(
title: 'some article helper test1',
group: Group.lookup(name: 'Users'),
customer_id: 2,
state: Ticket::State.lookup(name: 'new'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,
created_by_id: 1,
)
assert(ticket1, 'ticket created')
# create inbound article #1
article1 = Ticket::Article.create!(
ticket_id: ticket1.id,
from: 'some_sender@example.com',
to: 'some_recipient@example.com',
subject: 'some subject',
message_id: 'some@id',
content_type: 'text/html',
body: 'some message article helper test1 <div><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>',
internal: false,
sender: Ticket::Article::Sender.find_by(name: 'Customer'),
type: Ticket::Article::Type.find_by(name: 'email'),
updated_by_id: 1,
created_by_id: 1,
)
store1 = Store.add(
object: 'Ticket::Article',
o_id: article1.id,
data: 'content_file1_normally_should_be_an_ics_calendar_file',
filename: 'schedule.ics',
preferences: {
'Mime-Type' => 'text/calendar'
},
created_by_id: 1,
)
text = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
mail = Channel::EmailBuild.build(
from: 'sender@example.com',
to: 'recipient@example.com',
body: text,
attachments: [
store1
],
)
should = '> Welcome!
>
> Thank you for installing Zammad. äöüß
>'
assert_equal(should, mail.text_part.body.to_s)
assert_nil(mail.html_part)
assert_equal('text/calendar; filename=schedule.ics', mail.attachments[0].content_type)
parser = Channel::EmailParser.new
data = parser.parse(mail.to_s)
# check body
assert_equal(should, data[:body])
# check count of attachments, 2
assert_equal(1, data[:attachments].length)
# check attachments
data[:attachments]&.each do |attachment|
if attachment[:filename] == 'schedule.ics'
assert(attachment[:preferences]['Content-ID'])
assert_nil(attachment[:preferences]['content-alternative'])
assert_equal('text/calendar', attachment[:preferences]['Mime-Type'])
assert_equal('UTF-8', attachment[:preferences]['Charset'])
else
assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
end
end
end
test 'plain email + without attachment check' do
text = '> Welcome!
>