From 68cb27f0c154c143eab56b897cff56b9a396ab9d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 6 Nov 2017 17:43:06 +0100 Subject: [PATCH] Improved file name detection of attachments. --- app/models/channel/email_parser.rb | 72 +++++++++++++++++++++++------- test/fixtures/mail62.box | 44 ++++++++++++++++++ test/unit/email_parser_test.rb | 28 ++++++++++++ 3 files changed, 128 insertions(+), 16 deletions(-) diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 11281df87..8b97388d6 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -376,13 +376,7 @@ class Channel::EmailParser # generate file name based on content type if filename.blank? && headers_store['Content-Type'].present? - if headers_store['Content-Type'] =~ %r{^message/delivery-status$}i - filename = if headers_store['Content-Description'].present? - "#{headers_store['Content-Description']}.txt" - else - 'delivery-status.txt' - end - elsif headers_store['Content-Type'] =~ %r{^message/rfc822$}i + if headers_store['Content-Type'] =~ %r{^message/rfc822}i begin parser = Channel::EmailParser.new mail_local = parser.parse(file.body.to_s) @@ -397,20 +391,66 @@ class Channel::EmailParser filename = 'Mail.eml' end end + + # e. g. Content-Type: video/quicktime; name="Video.MOV"; + if filename.blank? + ['name="(.+?)"(;|$)', "name='(.+?)'(;|$)", 'name=(.+?)(;|$)'].each do |regexp| + if headers_store['Content-Type'] =~ /#{regexp}/i + filename = $1 + break + end + end + end + + # e. g. Content-Type: video/quicktime + if filename.blank? + map = { + 'message/delivery-status': ['txt', 'delivery-status'], + 'text/plain': %w(txt document), + 'text/html': %w(html document), + 'video/quicktime': %w(mov video), + 'image/jpeg': %w(jpg image), + 'image/jpg': %w(jpg image), + 'image/png': %w(png image), + 'image/gif': %w(gif image), + } + map.each do |type, ext| + next if headers_store['Content-Type'] !~ /^#{Regexp.quote(type)}/i + filename = if headers_store['Content-Description'].present? + "#{headers_store['Content-Description']}.#{ext[0]}" + else + "#{ext[1]}.#{ext[0]}" + end + break + end + end end if filename.blank? - attachment_count = 0 - (1..1000).each do |count| - filename_exists = false - filename = 'file-' + count.to_s - attachments.each do |attachment| - if attachment[:filename] == filename - filename_exists = true - end + filename = 'file' + end + + attachment_count = 0 + local_filename = '' + local_extention = '' + if filename =~ /^(.*?)\.(.+?)$/ + local_filename = $1 + local_extention = $2 + end + + (1..1000).each do |count| + filename_exists = false + attachments.each do |attachment| + if attachment[:filename] == filename + filename_exists = true end - break if filename_exists == false end + break if filename_exists == false + filename = if local_extention.present? + "#{local_filename}#{count}.#{local_extention}" + else + "#{local_filename}#{count}" + end end # get mime type diff --git a/test/fixtures/mail62.box b/test/fixtures/mail62.box index a17469a78..5027ac544 100644 --- a/test/fixtures/mail62.box +++ b/test/fixtures/mail62.box @@ -236,4 +236,48 @@ lrsUFSlElRLIySfNZDR+m0x1xk2SEGXFBa0BoYUR4J/uaKKWbm8wpaMMNRWEMMNpbabTtQhIwEge AKi3GJHmMbJDSXAO4zwaKKqygFDcsxkhgRIKdM2OWhpUq2R31MHc2p1G4pP5PHxUyRYrVMmNTpFv juyY4HpOqQCpGO4xRRXcJOwSZOWNyeQFJKVDIPYg81UI0dppsEIscIAnJwyKKKsBI6QJ/9= +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: text/html; charset="us-ascii" +Content-Transfer-Encoding: 7bit + +some html + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: text/html; charset="us-ascii" +Content-Transfer-Encoding: 7bit + +some html 2 + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: video/quicktime; name="Video1.MOV"; +Content-Disposition: attachment; filename="Video1.MOV" +Content-Transfer-Encoding: base64 + +/9j/4AAQSkZJ + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: video/quicktime; name="Video2.MOV"; + x-apple-part-url=FCCF8C80-2770-4929-B45B-4A5981ACF6DB +Content-Transfer-Encoding: base64 + +/9j/4AAQSkZJABC + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: video/quicktime; name="Video3.MOV" +Content-Transfer-Encoding: base64 + +/9j/4AAQSkZJABC123 + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: video/quicktime; +Content-Transfer-Encoding: base64 + +/9j/4AAQSkZJABC123456 + +------_=_NextPart_001_01CDB122.15B956D2 +Content-Type: video/quicktime; +Content-Transfer-Encoding: base64 + +/9j/4AAQSkZJABC123456789 + ------_=_NextPart_001_01CDB122.15B956D2-- diff --git a/test/unit/email_parser_test.rb b/test/unit/email_parser_test.rb index b8a813d75..1bfa8f3f6 100644 --- a/test/unit/email_parser_test.rb +++ b/test/unit/email_parser_test.rb @@ -1209,6 +1209,34 @@ Old programmers never die. They just branch to a new address." filename: 'image000.jpg', cid: 'image000.jpg@01CDB132.D8A510F0', }, + { + md5: '6db2535038171c72f97b060a24c8fe06', + filename: 'document.html', + }, + { + md5: '489ca24a0a54ca9189ea8a5256242fdd', + filename: 'document1.html', + }, + { + md5: 'b7e1651b0f31312a4e882d289c529ce8', + filename: 'Video1.MOV', + }, + { + md5: 'dcf4626b3dae9c47a8fd8f001c5d927f', + filename: 'Video2.MOV', + }, + { + md5: 'a93f87f52ef7cb56cf0576b804364e1e', + filename: 'Video3.MOV', + }, + { + md5: '370e0cb399d28515ab0692fa76c13b85', + filename: 'video.mov', + }, + { + md5: '9052bb9367a0bf45e4d3d10635bce8be', + filename: 'video1.mov', + }, ], params: { from: 'Smith Sepp ',