Merge pull request #6377 from tgxworld/remove_tif_tiff

Drop `tif`, `tiff`, `webp` and `bmp` from supported images.
This commit is contained in:
Guo Xiang Tan 2018-09-12 09:32:32 +08:00 committed by GitHub
commit 71185c13b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 41 additions and 22 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)
"<img src='#{upload.url}' width='#{upload.width}' height='#{upload.height}'>"
else
"<a class='attachment' href='#{upload.url}'>#{upload.original_filename}</a> (#{number_to_human_size(upload.filesize)})"

View File

@ -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

View File

@ -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

View File

@ -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!

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

BIN
spec/fixtures/images/webp_as.bin vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 B

View File

@ -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