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 21c0cd79bce..f7d2a0907d8 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 @@ -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 svg ico} end - def self.images_regexp - @@images_regexp ||= /\.(#{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 60074a197f9..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 @@ -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) 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) diff --git a/spec/fixtures/images/webp_as.bin b/spec/fixtures/images/webp_as.bin new file mode 100644 index 00000000000..2f8bf8fa1ab Binary files /dev/null and b/spec/fixtures/images/webp_as.bin differ diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb index 614916ec4fd..827288abb7b 100644 --- a/spec/lib/upload_creator_spec.rb +++ b/spec/lib/upload_creator_spec.rb @@ -43,6 +43,27 @@ RSpec.describe UploadCreator do expect(File.extname(upload.url)).to eq('.png') expect(upload.original_filename).to eq('png_as.png') end + + describe 'for webp format' do + before do + SiteSetting.authorized_extensions = '.webp|.bin' + end + + let(:filename) { "webp_as.bin" } + let(:file) { file_from_fixtures(filename) } + + it 'should not correct the coerce filename' do + expect do + UploadCreator.new(file, filename).create_for(user.id) + end.to change { Upload.count }.by(1) + + upload = Upload.last + + expect(upload.extension).to eq('bin') + expect(File.extname(upload.url)).to eq('.bin') + expect(upload.original_filename).to eq('webp_as.bin') + end + end end describe 'converting to jpeg' do