FIX: correct svg handling for images

We regressed and optimized images no longer worked with svg

The following adds the correct logic to simply copy file for svgs
and bypasses resizing for svg avatars
This commit is contained in:
Sam
2018-11-07 15:29:14 +11:00
parent 7534042427
commit 0a442e319c
4 changed files with 21 additions and 5 deletions

View File

@ -184,6 +184,7 @@ class UserAvatarsController < ApplicationController
def get_optimized_image(upload, size) def get_optimized_image(upload, size)
return if !upload return if !upload
return upload if upload.extension == "svg"
upload.get_optimized_image(size, size, allow_animation: SiteSetting.allow_animated_avatars) upload.get_optimized_image(size, size, allow_animation: SiteSetting.allow_animated_avatars)
# TODO decide if we want to detach here # TODO decide if we want to detach here

View File

@ -21,6 +21,7 @@ class OptimizedImage < ActiveRecord::Base
end end
def self.create_for(upload, width, height, opts = {}) def self.create_for(upload, width, height, opts = {})
return unless width > 0 && height > 0 return unless width > 0 && height > 0
return if upload.try(:sha1).blank? return if upload.try(:sha1).blank?
@ -29,7 +30,7 @@ class OptimizedImage < ActiveRecord::Base
upload.fix_image_extension upload.fix_image_extension
end end
if !upload.extension.match?(IM_DECODERS) if !upload.extension.match?(IM_DECODERS) && upload.extension != "svg"
if !opts[:raise_on_error] if !opts[:raise_on_error]
# nothing to do ... bad extension, not an image # nothing to do ... bad extension, not an image
return return
@ -76,7 +77,7 @@ class OptimizedImage < ActiveRecord::Base
temp_file = Tempfile.new(["discourse-thumbnail", extension]) temp_file = Tempfile.new(["discourse-thumbnail", extension])
temp_path = temp_file.path temp_path = temp_file.path
if extension =~ /\.svg$/i if upload.extension == "svg"
FileUtils.cp(original_path, temp_path) FileUtils.cp(original_path, temp_path)
resized = true resized = true
elsif opts[:crop] elsif opts[:crop]
@ -86,7 +87,6 @@ class OptimizedImage < ActiveRecord::Base
end end
if resized if resized
thumbnail = OptimizedImage.create!( thumbnail = OptimizedImage.create!(
upload_id: upload.id, upload_id: upload.id,
sha1: Upload.generate_digest(temp_path), sha1: Upload.generate_digest(temp_path),

View File

@ -44,7 +44,7 @@ class UploadCreator
extract_image_info! extract_image_info!
return @upload if @upload.errors.present? return @upload if @upload.errors.present?
if @filename[/\.svg$/i] if @image_info.type.to_s == "svg"
whitelist_svg! whitelist_svg!
elsif !Rails.env.test? || @opts[:force_optimize] elsif !Rails.env.test? || @opts[:force_optimize]
convert_to_jpeg! if should_convert_to_jpeg? convert_to_jpeg! if should_convert_to_jpeg?
@ -131,7 +131,7 @@ class UploadCreator
end end
end end
if @upload.errors.empty? && is_image && @opts[:type] == "avatar" if @upload.errors.empty? && is_image && @opts[:type] == "avatar" && @upload.extension != "svg"
Jobs.enqueue(:create_avatar_thumbnails, upload_id: @upload.id, user_id: user_id) Jobs.enqueue(:create_avatar_thumbnails, upload_id: @upload.id, user_id: user_id)
end end

View File

@ -185,6 +185,21 @@ describe OptimizedImage do
describe ".create_for" do describe ".create_for" do
it "is able to 'optimize' an svg" do
# we don't really optimize anything, we simply copy
# but at least this confirms this actually works
SiteSetting.authorized_extensions = 'svg'
svg = file_from_fixtures('image.svg')
upload = UploadCreator.new(svg, 'image.svg').create_for(Discourse.system_user.id)
resized = upload.get_optimized_image(50, 50, {})
# we perform some basic svg mangling but expect the string Discourse to be there
expect(File.read(Discourse.store.path_for(resized))).to include("Discourse")
expect(File.read(Discourse.store.path_for(resized))).to eq(File.read(Discourse.store.path_for(upload)))
end
context "when using an internal store" do context "when using an internal store" do
let(:store) { FakeInternalStore.new } let(:store) { FakeInternalStore.new }