fix: Update inline image processing logic to fix missing images when multiple inline images present (#7861)

This commit is contained in:
Shivam Mishra
2023-09-06 14:35:19 +05:30
committed by GitHub
parent a8009c7b39
commit e529e1206e
5 changed files with 151 additions and 57 deletions

View File

@@ -23,24 +23,47 @@ module MailboxHelper
def add_attachments_to_message
return if @message.blank?
processed_mail.attachments.last(Message::NUMBER_OF_PERMITTED_ATTACHMENTS).each do |mail_attachment|
if inline_attachment?(mail_attachment)
embed_inline_image_source(mail_attachment)
else
attachment = @message.attachments.new(
account_id: @conversation.account_id,
file_type: 'file'
)
attachment.file.attach(mail_attachment[:blob])
end
end
# ensure we don't add more than the permitted number of attachments
all_attachments = processed_mail.attachments.last(Message::NUMBER_OF_PERMITTED_ATTACHMENTS)
inline_attachments = all_attachments.select { |attachment| attachment[:original].inline? }
regular_attachments = all_attachments - inline_attachments
process_inline_attachments(inline_attachments) if inline_attachments.present?
process_regular_attachments(regular_attachments) if regular_attachments.present?
@message.save!
end
def process_regular_attachments(attachments)
attachments.each do |mail_attachment|
attachment = @message.attachments.new(
account_id: @conversation.account_id,
file_type: 'file'
)
attachment.file.attach(mail_attachment[:blob])
end
end
def process_inline_attachments(attachments)
# create an instance variable here, the `embed_inline_image_source`
# updates them directly. And then the value is eventaully used to update the message content
@html_content = processed_mail.serialized_data[:html_content][:full]
@text_content = processed_mail.serialized_data[:text_content][:reply]
attachments.each do |mail_attachment|
embed_inline_image_source(mail_attachment)
end
# update the message content with the updated html and text content
@message.content_attributes[:email][:html_content][:full] = @html_content
@message.content_attributes[:email][:text_content][:full] = @text_content
end
def embed_inline_image_source(mail_attachment)
if processed_mail.serialized_data[:html_content].present?
if @html_content.present?
upload_inline_image(mail_attachment)
elsif processed_mail.serialized_data[:text_content].present?
elsif @text_content.present?
embed_plain_text_email_with_inline_image(mail_attachment)
end
end
@@ -48,23 +71,15 @@ module MailboxHelper
def upload_inline_image(mail_attachment)
content_id = mail_attachment[:original].cid
@message.content_attributes[:email][:html_content][:full] = processed_mail.serialized_data[:html_content][:full].gsub(
"cid:#{content_id}", inline_image_url(mail_attachment[:blob]).to_s
)
@message.save!
end
def inline_attachment?(mail_attachment)
mail_attachment[:original].inline?
@html_content = @html_content.gsub("cid:#{content_id}", inline_image_url(mail_attachment[:blob]).to_s)
end
def embed_plain_text_email_with_inline_image(mail_attachment)
attachment_name = mail_attachment[:original].filename
@message.content_attributes[:email][:text_content][:full] = processed_mail.serialized_data[:text_content][:reply].gsub(
"[image: #{attachment_name}]", "<img src=\"#{inline_image_url(mail_attachment[:blob])}\" alt=\"#{attachment_name}>\""
@text_content = @text_content.gsub(
"[image: #{attachment_name}]", "<img src=\"#{inline_image_url(mail_attachment[:blob])}\" alt=\"#{attachment_name}\">"
)
@message.save!
end
def inline_image_url(blob)