FIX: Ensure oneboxed secure images which are optimized and also lightboxed optimized images are embedded in email (#11061)

We had an issue where onebox thumbnail was too large and thus was optimized, and we are using the image URLs in post to redact and re-embed, based on the sha1 in the URL. Optimized image URLs have extra stuff on the end like _99x99 so we were not parsing out the sha1 correctly. Another issue I found was for posts that have giant images, the original was being used to embed in the email and thus would basically never get included because it is huge.

For example the URL 787b17ea61_2_690x335.jpeg was not parsed correctly; we would end up with 787b17ea6140f4f022eb7f1509a692f2873cfe35_2_690x335.jpeg as the sha1 which would not find the image to re-embed that was already attached to the email.

This fix will use the first optimized image of the detected upload when we are redacting and then re-embedding to make sure we are not sending giant things in email. Also, I detect if it is a onebox thumbnail or the site icon and force appropriate sizes and styles.
This commit is contained in:
Martin Brennan
2020-11-02 09:52:21 +10:00
committed by GitHub
parent b6aaff74be
commit 3655062c60
5 changed files with 152 additions and 32 deletions

View File

@@ -272,22 +272,25 @@ module Email
return if max_email_size == 0
email_size = 0
post.uploads.each do |upload|
if FileHelper.is_supported_image?(upload.original_filename) &&
!should_attach_image?(upload)
post.uploads.each do |original_upload|
optimized_1X = original_upload.optimized_images.first
if FileHelper.is_supported_image?(original_upload.original_filename) &&
!should_attach_image?(original_upload, optimized_1X)
next
end
next if email_size + upload.filesize > max_email_size
attached_upload = optimized_1X || original_upload
next if email_size + original_upload.filesize > max_email_size
begin
path = if upload.local?
Discourse.store.path_for(upload)
path = if attached_upload.local?
Discourse.store.path_for(attached_upload)
else
Discourse.store.download(upload).path
Discourse.store.download(attached_upload).path
end
@message.attachments[upload.original_filename] = File.read(path)
@message.attachments[original_upload.original_filename] = File.read(path)
email_size += File.size(path)
rescue => e
Discourse.warn_exception(
@@ -295,8 +298,8 @@ module Email
message: "Failed to attach file to email",
env: {
post_id: post.id,
upload_id: upload.id,
filename: upload.original_filename
upload_id: original_upload.id,
filename: original_upload.original_filename
}
)
end
@@ -305,9 +308,9 @@ module Email
fix_parts_after_attachments!
end
def should_attach_image?(upload)
def should_attach_image?(upload, optimized_1X = nil)
return if !SiteSetting.secure_media_allow_embed_images_in_emails || !upload.secure?
return if upload.filesize > SiteSetting.secure_media_max_email_embed_image_size_kb.kilobytes
return if (optimized_1X&.filesize || upload.filesize) > SiteSetting.secure_media_max_email_embed_image_size_kb.kilobytes
true
end