From d59e635a77044212daf9f995183ea1fb5e4538d5 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 10 Sep 2018 10:03:44 +0800 Subject: [PATCH 1/3] Rename `FileHelper.images` to `FileHelper.supported_images`. --- lib/file_helper.rb | 8 +++----- lib/validators/upload_validator.rb | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/file_helper.rb b/lib/file_helper.rb index 21c0cd79bce..d5410e00055 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -100,14 +100,12 @@ class FileHelper ).optimize_image!(filename) end - private - - def self.images - @@images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} + def self.supported_images + @@supported_images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} end def self.images_regexp - @@images_regexp ||= /\.(#{images.to_a.join("|")})$/i + @@images_regexp ||= /\.(#{supported_images.to_a.join("|")})$/i end end diff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rb index 60074a197f9..72bc1febadd 100644 --- a/lib/validators/upload_validator.rb +++ b/lib/validators/upload_validator.rb @@ -74,11 +74,11 @@ class Validators::UploadValidator < ActiveModel::Validator end def authorized_images(upload) - authorized_extensions(upload) & FileHelper.images + authorized_extensions(upload) & FileHelper.supported_images end def authorized_attachments(upload) - authorized_extensions(upload) - FileHelper.images + authorized_extensions(upload) - FileHelper.supported_images end def authorizes_all_extensions?(upload) From e1b16e445e523d652d6f94bb1549b4b01452ea62 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 10 Sep 2018 10:22:45 +0800 Subject: [PATCH 2/3] Rename `FileHelper.is_image?` -> `FileHelper.is_supported_image?`. --- app/controllers/uploads_controller.rb | 2 +- app/jobs/regular/crawl_topic_link.rb | 2 +- app/models/upload.rb | 4 ++-- lib/email/receiver.rb | 2 +- lib/file_helper.rb | 8 ++++---- lib/file_store/s3_store.rb | 2 +- lib/upload_creator.rb | 4 ++-- lib/url_helper.rb | 2 +- lib/validators/upload_validator.rb | 2 +- script/downsize_uploads.rb | 2 +- script/import_scripts/base/uploader.rb | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 092c6a4dc9d..922e4767e33 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -72,7 +72,7 @@ class UploadsController < ApplicationController content_type: MiniMime.lookup_by_filename(upload.original_filename)&.content_type, } opts[:disposition] = "inline" if params[:inline] - opts[:disposition] ||= "attachment" unless FileHelper.is_image?(upload.original_filename) + opts[:disposition] ||= "attachment" unless FileHelper.is_supported_image?(upload.original_filename) send_file(Discourse.store.path_for(upload), opts) else render_404 diff --git a/app/jobs/regular/crawl_topic_link.rb b/app/jobs/regular/crawl_topic_link.rb index 15c0c01ad09..54abf0a1108 100644 --- a/app/jobs/regular/crawl_topic_link.rb +++ b/app/jobs/regular/crawl_topic_link.rb @@ -26,7 +26,7 @@ module Jobs # Special case: Images # If the link is to an image, put the filename as the title - if FileHelper.is_image?(topic_link.url) + if FileHelper.is_supported_image?(topic_link.url) uri = URI(topic_link.url) filename = File.basename(uri.path) crawled = (TopicLink.where(id: topic_link.id).update_all(["title = ?, crawled_at = CURRENT_TIMESTAMP", filename]) == 1) diff --git a/app/models/upload.rb b/app/models/upload.rb index 00f17cbeadd..51bd464493c 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -112,7 +112,7 @@ class Upload < ActiveRecord::Base end def fix_dimensions! - return if !FileHelper.is_image?("image.#{extension}") + return if !FileHelper.is_supported_image?("image.#{extension}") path = if local? @@ -219,7 +219,7 @@ class Upload < ActiveRecord::Base upload.sha1 = Upload.generate_digest(path) end # optimize if image - FileHelper.optimize_image!(path) if FileHelper.is_image?(File.basename(path)) + FileHelper.optimize_image!(path) if FileHelper.is_supported_image?(File.basename(path)) # store to new location & update the filesize File.open(path) do |f| upload.url = Discourse.store.store_upload(f, upload) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 3419dbda062..0e445fdddcb 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -910,7 +910,7 @@ module Email end def attachment_markdown(upload) - if FileHelper.is_image?(upload.original_filename) + if FileHelper.is_supported_image?(upload.original_filename) "" else "#{upload.original_filename} (#{number_to_human_size(upload.filesize)})" diff --git a/lib/file_helper.rb b/lib/file_helper.rb index d5410e00055..331b2636d8f 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -11,8 +11,8 @@ class FileHelper ) end - def self.is_image?(filename) - filename =~ images_regexp + def self.is_supported_image?(filename) + filename =~ supported_images_regexp end class FakeIO @@ -104,8 +104,8 @@ class FileHelper @@supported_images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} end - def self.images_regexp - @@images_regexp ||= /\.(#{supported_images.to_a.join("|")})$/i + def self.supported_images_regexp + @@supported_images_regexp ||= /\.(#{supported_images.to_a.join("|")})$/i end end diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index aa444ba1fce..6aca50f9f4d 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -39,7 +39,7 @@ module FileStore content_type: opts[:content_type].presence || MiniMime.lookup_by_filename(filename)&.content_type } # add a "content disposition" header for "attachments" - options[:content_disposition] = "attachment; filename=\"#{filename}\"" unless FileHelper.is_image?(filename) + options[:content_disposition] = "attachment; filename=\"#{filename}\"" unless FileHelper.is_supported_image?(filename) # if this fails, it will throw an exception path = @s3_helper.upload(file, path, options) # return the upload url diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 25d2eef45d1..2b138950e7d 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -37,8 +37,8 @@ class UploadCreator # test for image regardless of input @image_info = FastImage.new(@file) rescue nil - is_image = FileHelper.is_image?(@filename) - is_image ||= @image_info && FileHelper.is_image?("test.#{@image_info.type}") + is_image = FileHelper.is_supported_image?(@filename) + is_image ||= @image_info && FileHelper.is_supported_image?("test.#{@image_info.type}") if is_image extract_image_info! diff --git a/lib/url_helper.rb b/lib/url_helper.rb index 5df27575f85..377c7d38cd1 100644 --- a/lib/url_helper.rb +++ b/lib/url_helper.rb @@ -36,7 +36,7 @@ class UrlHelper uri = URI.parse(url) filename = File.basename(uri.path) - is_attachment = !FileHelper.is_image?(filename) + is_attachment = !FileHelper.is_supported_image?(filename) no_cdn = SiteSetting.login_required || SiteSetting.prevent_anons_from_downloading_files diff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rb index 72bc1febadd..b546ebf1554 100644 --- a/lib/validators/upload_validator.rb +++ b/lib/validators/upload_validator.rb @@ -18,7 +18,7 @@ class Validators::UploadValidator < ActiveModel::Validator extension = File.extname(upload.original_filename)[1..-1] || "" if is_authorized?(upload, extension) - if FileHelper.is_image?(upload.original_filename) + if FileHelper.is_supported_image?(upload.original_filename) authorized_image_extension(upload, extension) maximum_image_file_size(upload) else diff --git a/script/downsize_uploads.rb b/script/downsize_uploads.rb index 9812ca7abf9..fae1ab4374e 100644 --- a/script/downsize_uploads.rb +++ b/script/downsize_uploads.rb @@ -11,7 +11,7 @@ Upload.where("lower(extension) in (?)", ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'ti count += 1 print "\r%8d".freeze % count absolute_path = Discourse.store.path_for(upload) - if absolute_path && FileHelper.is_image?(upload.original_filename) + if absolute_path && FileHelper.is_supported_image?(upload.original_filename) file = File.new(absolute_path) rescue nil next unless file diff --git a/script/import_scripts/base/uploader.rb b/script/import_scripts/base/uploader.rb index 2d25a3e9ae8..a9540112baf 100644 --- a/script/import_scripts/base/uploader.rb +++ b/script/import_scripts/base/uploader.rb @@ -40,7 +40,7 @@ module ImportScripts end def html_for_upload(upload, display_filename) - if FileHelper.is_image?(upload.url) + if FileHelper.is_supported_image?(upload.url) embedded_image_html(upload) else attachment_html(upload, display_filename) From 71caf7521d217babdcd2b87c2d9275d07de2d17c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 10 Sep 2018 10:49:01 +0800 Subject: [PATCH 3/3] Drop `tif`, `tiff`, `webp` and `bmp` from supported images. https://meta.discourse.org/t/cr2-raw-files-are-being-treated-as-tiff-files/96775/3?u=tgxworld --- lib/file_helper.rb | 2 +- spec/fixtures/images/webp_as.bin | Bin 0 -> 26 bytes spec/lib/upload_creator_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/images/webp_as.bin diff --git a/lib/file_helper.rb b/lib/file_helper.rb index 331b2636d8f..f7d2a0907d8 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -101,7 +101,7 @@ class FileHelper end def self.supported_images - @@supported_images ||= Set.new %w{jpg jpeg png gif tif tiff bmp svg webp ico} + @@supported_images ||= Set.new %w{jpg jpeg png gif svg ico} end def self.supported_images_regexp diff --git a/spec/fixtures/images/webp_as.bin b/spec/fixtures/images/webp_as.bin new file mode 100644 index 0000000000000000000000000000000000000000..2f8bf8fa1abcc8705d39a5815d7b0aad344c613f GIT binary patch literal 26 fcmWIYbaNA8U|