mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
FIX: Use markdown for images and attachments in Email::Receiver
.
This commit is contained in:
@ -1031,6 +1031,15 @@ module Email
|
|||||||
if attachment.content_type&.start_with?("image/")
|
if attachment.content_type&.start_with?("image/")
|
||||||
if raw[attachment.url]
|
if raw[attachment.url]
|
||||||
raw.sub!(attachment.url, upload.url)
|
raw.sub!(attachment.url, upload.url)
|
||||||
|
|
||||||
|
InlineUploads.match_img(raw) do |match, src, replacement, _|
|
||||||
|
if src == upload.url
|
||||||
|
raw = raw.sub(
|
||||||
|
match,
|
||||||
|
""
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
|
elsif raw[/\[image:.*?\d+[^\]]*\]/i]
|
||||||
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
|
raw.sub!(/\[image:.*?\d+[^\]]*\]/i, attachment_markdown(upload))
|
||||||
else
|
else
|
||||||
@ -1074,9 +1083,9 @@ module Email
|
|||||||
|
|
||||||
def attachment_markdown(upload)
|
def attachment_markdown(upload)
|
||||||
if FileHelper.is_supported_image?(upload.original_filename)
|
if FileHelper.is_supported_image?(upload.original_filename)
|
||||||
"<img src='#{upload.url}' width='#{upload.width}' height='#{upload.height}'>"
|
""
|
||||||
else
|
else
|
||||||
"<a class='attachment' href='#{upload.url}'>#{upload.original_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
"[#{upload.original_filename}|attachment](#{upload.short_url}) (#{number_to_human_size(upload.filesize)})"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -553,38 +553,77 @@ describe Email::Receiver do
|
|||||||
SiteSetting.incoming_email_prefer_html = false
|
SiteSetting.incoming_email_prefer_html = false
|
||||||
|
|
||||||
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/)
|
|
||||||
|
post = topic.posts.last
|
||||||
|
upload = post.uploads.first
|
||||||
|
|
||||||
|
expect(post.raw).to include(
|
||||||
|
""
|
||||||
|
)
|
||||||
|
|
||||||
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/)
|
|
||||||
|
post = topic.posts.last
|
||||||
|
upload = post.uploads.first
|
||||||
|
|
||||||
|
expect(post.raw).to include(
|
||||||
|
""
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports attached images in HTML part" do
|
it "supports attached images in HTML part" do
|
||||||
SiteSetting.incoming_email_prefer_html = true
|
SiteSetting.incoming_email_prefer_html = true
|
||||||
|
|
||||||
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\*/)
|
|
||||||
|
post = topic.posts.last
|
||||||
|
upload = post.uploads.last
|
||||||
|
|
||||||
|
expect(post.raw).to eq(<<~MD.chomp)
|
||||||
|
**Before**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
*After*
|
||||||
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports attachments" do
|
it "supports attachments" do
|
||||||
SiteSetting.authorized_extensions = "txt|jpg"
|
SiteSetting.authorized_extensions = "txt|jpg"
|
||||||
expect { process(:attached_txt_file) }.to change { topic.posts.count }
|
expect { process(:attached_txt_file) }.to change { topic.posts.count }
|
||||||
post = topic.posts.last
|
post = topic.posts.last
|
||||||
expect(post.raw).to match(/\APlease find some text file attached\.\s+<a class='attachment' href='\/uploads\/default\/original\/.+?txt'>text\.txt<\/a> \(20 Bytes\)\z/)
|
upload = post.uploads.first
|
||||||
expect(post.uploads.size).to eq 1
|
|
||||||
|
expect(post.raw).to eq(<<~MD.chomp)
|
||||||
|
Please find some text file attached.
|
||||||
|
|
||||||
|
[#{upload.original_filename}|attachment](#{upload.short_url}) (20 Bytes)
|
||||||
|
MD
|
||||||
|
|
||||||
expect { process(:apple_mail_attachment) }.to change { topic.posts.count }
|
expect { process(:apple_mail_attachment) }.to change { topic.posts.count }
|
||||||
post = topic.posts.last
|
post = topic.posts.last
|
||||||
expect(post.raw).to match(/\APicture below\.\s+<img.+?src="\/uploads\/default\/original\/.+?jpeg" class="">\s+Picture above\.\z/)
|
upload = post.uploads.first
|
||||||
expect(post.uploads.size).to eq 1
|
|
||||||
|
expect(post.raw).to eq(<<~MD.chomp)
|
||||||
|
Picture below.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Picture above.
|
||||||
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports eml attachments" do
|
it "supports eml attachments" do
|
||||||
SiteSetting.authorized_extensions = "eml"
|
SiteSetting.authorized_extensions = "eml"
|
||||||
expect { process(:attached_eml_file) }.to change { topic.posts.count }
|
expect { process(:attached_eml_file) }.to change { topic.posts.count }
|
||||||
post = topic.posts.last
|
post = topic.posts.last
|
||||||
expect(post.raw).to match(/\APlease find the eml file attached\.\s+<a class='attachment' href='\/uploads\/default\/original\/.+?eml'>sample\.eml<\/a> \(193 Bytes\)\z/)
|
upload = post.uploads.first
|
||||||
expect(post.uploads.size).to eq 1
|
|
||||||
|
expect(post.raw).to eq(<<~MD.chomp)
|
||||||
|
Please find the eml file attached.
|
||||||
|
|
||||||
|
[#{upload.original_filename}|attachment](#{upload.short_url}) (193 Bytes)
|
||||||
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when attachment is rejected" do
|
context "when attachment is rejected" do
|
||||||
@ -613,8 +652,11 @@ describe Email::Receiver do
|
|||||||
SiteSetting.authorized_extensions = "pdf"
|
SiteSetting.authorized_extensions = "pdf"
|
||||||
expect { process(:attached_pdf_file) }.to change { topic.posts.count }
|
expect { process(:attached_pdf_file) }.to change { topic.posts.count }
|
||||||
post = topic.posts.last
|
post = topic.posts.last
|
||||||
expect(post.raw).to match(/\A\s+<a class='attachment' href='\/uploads\/default\/original\/.+?pdf'>discourse\.pdf<\/a> \(64 KB\)\z/)
|
upload = post.uploads.last
|
||||||
expect(post.uploads.size).to eq 1
|
|
||||||
|
expect(post.raw).to include(
|
||||||
|
"[#{upload.original_filename}|attachment](#{upload.short_url}) (64 KB)"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports liking via email" do
|
it "supports liking via email" do
|
||||||
@ -759,8 +801,13 @@ describe Email::Receiver do
|
|||||||
it "supports any kind of attachments when 'allow_all_attachments_for_group_messages' is enabled" do
|
it "supports any kind of attachments when 'allow_all_attachments_for_group_messages' is enabled" do
|
||||||
SiteSetting.allow_all_attachments_for_group_messages = true
|
SiteSetting.allow_all_attachments_for_group_messages = true
|
||||||
expect { process(:attached_rb_file) }.to change(Topic, :count)
|
expect { process(:attached_rb_file) }.to change(Topic, :count)
|
||||||
expect(Post.last.raw).to match(/<a\sclass='attachment'[^>]*>discourse\.rb<\/a>/)
|
|
||||||
expect(Post.last.uploads.length).to eq 1
|
post = Topic.last.first_post
|
||||||
|
upload = post.uploads.first
|
||||||
|
|
||||||
|
expect(post.raw).to include(
|
||||||
|
"[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.filesize} Bytes)"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reenables user's PM email notifications when user emails new topic to group" do
|
it "reenables user's PM email notifications when user emails new topic to group" do
|
||||||
|
Reference in New Issue
Block a user