From bf322281e32e45f5529e232f9ab965dd4d959851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 3 May 2017 22:54:26 +0200 Subject: [PATCH] Improve handling of inlined images in incoming emails --- lib/email/receiver.rb | 12 +++++++++--- spec/components/email/receiver_spec.rb | 12 ++++++++++-- spec/fixtures/emails/inline_image.eml | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 3d10c9c2780..810e682a14f 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -194,7 +194,7 @@ module Email end markdown, elided_markdown = if html.present? - markdown = HtmlToMarkdown.new(html, keep_img_tags: true).to_markdown + markdown = HtmlToMarkdown.new(html, keep_img_tags: true, keep_cid_imgs: true).to_markdown markdown = trim_discourse_markers(markdown) EmailReplyTrimmer.trim(markdown, true) end @@ -573,8 +573,14 @@ module Email upload = Upload.create_for(options[:user].id, tmp, attachment.filename, tmp.size, opts) if upload && upload.errors.empty? # try to inline images - if attachment.content_type.start_with?("image/") && options[:raw][/\[image: .+ \d+\]/] - options[:raw].sub!(/\[image: .+ \d+\]/, attachment_markdown(upload)) + if attachment.content_type.start_with?("image/") + if options[:raw][attachment.url] + options[:raw].sub!(attachment.url, upload.url) + elsif options[:raw][/\[image:.*?\d+[^\]]*\]/i] + options[:raw].sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload)) + else + options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n" + end else options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n" end diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index c0735fbc35a..cfbbe692de2 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -302,14 +302,22 @@ describe Email::Receiver do expect(topic.posts.last.raw).to eq("This is a reply :)\n\n
\n···\n---Original Message---\nThis part should not be included\n
") end - it "supports attached images" do + it "supports attached images in TEXT part" do SiteSetting.queue_jobs = true expect { process(:no_body_with_image) }.to change { topic.posts.count } expect(topic.posts.last.raw).to match(/\s+After/) + end + + it "supports attached images in HTML part" do + SiteSetting.queue_jobs = true + SiteSetting.incoming_email_prefer_html = true + + expect { process(:inline_image) }.to change { topic.posts.count } + expect(topic.posts.last.raw).to match(/\*\*Before\*\*\s+\s+\*After\*/) end it "supports attachments" do diff --git a/spec/fixtures/emails/inline_image.eml b/spec/fixtures/emails/inline_image.eml index 52188b2a787..af3283702a9 100644 --- a/spec/fixtures/emails/inline_image.eml +++ b/spec/fixtures/emails/inline_image.eml @@ -21,8 +21,8 @@ After --001a114b2eccff1836052998ec67 Content-Type: text/html; charset=UTF-8 -
Before

内嵌图片 1
-

After
+
Before

内嵌图片 1

After +
--001a114b2eccff1836052998ec67-- --001a114b2eccff183a052998ec68