mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
Improve handling of inlined images in incoming emails
This commit is contained in:
@ -194,7 +194,7 @@ module Email
|
|||||||
end
|
end
|
||||||
|
|
||||||
markdown, elided_markdown = if html.present?
|
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)
|
markdown = trim_discourse_markers(markdown)
|
||||||
EmailReplyTrimmer.trim(markdown, true)
|
EmailReplyTrimmer.trim(markdown, true)
|
||||||
end
|
end
|
||||||
@ -573,8 +573,14 @@ module Email
|
|||||||
upload = Upload.create_for(options[:user].id, tmp, attachment.filename, tmp.size, opts)
|
upload = Upload.create_for(options[:user].id, tmp, attachment.filename, tmp.size, opts)
|
||||||
if upload && upload.errors.empty?
|
if upload && upload.errors.empty?
|
||||||
# try to inline images
|
# try to inline images
|
||||||
if attachment.content_type.start_with?("image/") && options[:raw][/\[image: .+ \d+\]/]
|
if attachment.content_type.start_with?("image/")
|
||||||
options[:raw].sub!(/\[image: .+ \d+\]/, attachment_markdown(upload))
|
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
|
else
|
||||||
options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n"
|
options[:raw] << "\n\n#{attachment_markdown(upload)}\n\n"
|
||||||
end
|
end
|
||||||
|
@ -302,14 +302,22 @@ describe Email::Receiver do
|
|||||||
expect(topic.posts.last.raw).to eq("This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>···</summary>\n---Original Message---\nThis part should not be included\n</details>")
|
expect(topic.posts.last.raw).to eq("This is a reply :)\n\n<details class='elided'>\n<summary title='Show trimmed content'>···</summary>\n---Original Message---\nThis part should not be included\n</details>")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports attached images" do
|
it "supports attached images in TEXT part" do
|
||||||
SiteSetting.queue_jobs = true
|
SiteSetting.queue_jobs = true
|
||||||
|
|
||||||
expect { process(:no_body_with_image) }.to change { topic.posts.count }
|
expect { process(:no_body_with_image) }.to change { topic.posts.count }
|
||||||
expect(topic.posts.last.raw).to match(/<img/)
|
expect(topic.posts.last.raw).to match(/<img/)
|
||||||
|
|
||||||
expect { process(:inline_image) }.to change { topic.posts.count }
|
expect { process(:inline_image) }.to change { topic.posts.count }
|
||||||
expect(topic.posts.last.raw).to match(/Before\s+<img.+\s+After/m)
|
expect(topic.posts.last.raw).to match(/Before\s+<img.+>\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+<img.+>\s+\*After\*/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports attachments" do
|
it "supports attachments" do
|
||||||
|
4
spec/fixtures/emails/inline_image.eml
vendored
4
spec/fixtures/emails/inline_image.eml
vendored
@ -21,8 +21,8 @@ After
|
|||||||
--001a114b2eccff1836052998ec67
|
--001a114b2eccff1836052998ec67
|
||||||
Content-Type: text/html; charset=UTF-8
|
Content-Type: text/html; charset=UTF-8
|
||||||
|
|
||||||
<div dir="ltr">Before<div><br></div><div><img src="cid:ii_1525434659ddb4cb" alt="内嵌图片 1" style="margin-right: 0px;"><br>
|
<div dir="ltr"><b>Before</b><div><br></div><div><img src="cid:ii_1525434659ddb4cb" alt="内嵌图片 1"><br></div><div><br></div><div><i>After</i>
|
||||||
<div><br></div><div>After</div></div></div>
|
</div></div>
|
||||||
|
|
||||||
--001a114b2eccff1836052998ec67--
|
--001a114b2eccff1836052998ec67--
|
||||||
--001a114b2eccff183a052998ec68
|
--001a114b2eccff183a052998ec68
|
||||||
|
Reference in New Issue
Block a user