FIX: don't try to optimize large PNGs (takes too much time)
This commit is contained in:
parent
5f747a74a1
commit
4d981cec53
|
@ -51,13 +51,16 @@ class UploadsController < ApplicationController
|
||||||
render nothing: true, status: 404
|
render nothing: true, status: 404
|
||||||
end
|
end
|
||||||
|
|
||||||
|
MAXIMUM_UPLOAD_SIZE ||= 10.megabytes
|
||||||
|
DOWNSIZE_RATIO ||= 0.8
|
||||||
|
|
||||||
def create_upload(type, file, url)
|
def create_upload(type, file, url)
|
||||||
begin
|
begin
|
||||||
# ensure we have a file
|
# ensure we have a file
|
||||||
if file.nil?
|
if file.nil?
|
||||||
# API can provide a URL
|
# API can provide a URL
|
||||||
if url.present? && is_api?
|
if url.present? && is_api?
|
||||||
tempfile = FileHelper.download(url, 10.megabytes, "discourse-upload-#{type}") rescue nil
|
tempfile = FileHelper.download(url, MAXIMUM_UPLOAD_SIZE, "discourse-upload-#{type}") rescue nil
|
||||||
filename = File.basename(URI.parse(url).path)
|
filename = File.basename(URI.parse(url).path)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -69,22 +72,23 @@ class UploadsController < ApplicationController
|
||||||
return { errors: I18n.t("upload.file_missing") } if tempfile.nil?
|
return { errors: I18n.t("upload.file_missing") } if tempfile.nil?
|
||||||
|
|
||||||
# allow users to upload (not that) large images that will be automatically reduced to allowed size
|
# allow users to upload (not that) large images that will be automatically reduced to allowed size
|
||||||
|
if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename)
|
||||||
uploaded_size = File.size(tempfile.path)
|
uploaded_size = File.size(tempfile.path)
|
||||||
if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) && uploaded_size > 0 && uploaded_size < 10.megabytes
|
if 0 < uploaded_size && uploaded_size < MAXIMUM_UPLOAD_SIZE && Upload.should_optimize?(tempfile.path)
|
||||||
attempt = 2
|
attempt = 2
|
||||||
allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails
|
allow_animation = type == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails
|
||||||
while attempt > 0
|
while attempt > 0
|
||||||
downsized_size = File.size(tempfile.path)
|
downsized_size = File.size(tempfile.path)
|
||||||
break if downsized_size > uploaded_size
|
break if uploaded_size < downsized_size || downsized_size < SiteSetting.max_image_size_kb.kilobytes
|
||||||
break if downsized_size < SiteSetting.max_image_size_kb.kilobytes
|
|
||||||
image_info = FastImage.new(tempfile.path) rescue nil
|
image_info = FastImage.new(tempfile.path) rescue nil
|
||||||
w, h = *(image_info.try(:size) || [0, 0])
|
w, h = *(image_info.try(:size) || [0, 0])
|
||||||
break if w == 0 || h == 0
|
break if w == 0 || h == 0
|
||||||
dimensions = "#{(w * 0.8).floor}x#{(h * 0.8).floor}"
|
dimensions = "#{(w * DOWNSIZE_RATIO).floor}x#{(h * DOWNSIZE_RATIO).floor}"
|
||||||
OptimizedImage.downsize(tempfile.path, tempfile.path, dimensions, filename: filename, allow_animation: allow_animation)
|
OptimizedImage.downsize(tempfile.path, tempfile.path, dimensions, filename: filename, allow_animation: allow_animation)
|
||||||
attempt -= 1
|
attempt -= 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
upload = Upload.create_for(current_user.id, tempfile, filename, File.size(tempfile.path), content_type: content_type, image_type: type)
|
upload = Upload.create_for(current_user.id, tempfile, filename, File.size(tempfile.path), content_type: content_type, image_type: type)
|
||||||
|
|
||||||
|
|
|
@ -73,8 +73,8 @@ class Upload < ActiveRecord::Base
|
||||||
w = svg["width"].to_i
|
w = svg["width"].to_i
|
||||||
h = svg["height"].to_i
|
h = svg["height"].to_i
|
||||||
else
|
else
|
||||||
# fix orientation first (but not for GIFs)
|
# fix orientation first
|
||||||
fix_image_orientation(file.path) unless filename =~ /\.GIF$/i
|
fix_image_orientation(file.path) if should_optimize?(file.path)
|
||||||
# retrieve image info
|
# retrieve image info
|
||||||
image_info = FastImage.new(file) rescue nil
|
image_info = FastImage.new(file) rescue nil
|
||||||
w, h = *(image_info.try(:size) || [0, 0])
|
w, h = *(image_info.try(:size) || [0, 0])
|
||||||
|
@ -107,8 +107,8 @@ class Upload < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# optimize image (but not for GIFs)
|
# optimize image (except GIFs and large PNGs)
|
||||||
if filename !~ /\.GIF$/i
|
if should_optimize?(file.path)
|
||||||
ImageOptim.new.optimize_image!(file.path) rescue nil
|
ImageOptim.new.optimize_image!(file.path) rescue nil
|
||||||
# update the file size
|
# update the file size
|
||||||
filesize = File.size(file.path)
|
filesize = File.size(file.path)
|
||||||
|
@ -163,6 +163,18 @@ class Upload < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
LARGE_PNG_SIZE ||= 3.megabytes
|
||||||
|
|
||||||
|
def self.should_optimize?(path)
|
||||||
|
# don't optimize GIFs
|
||||||
|
return false if path =~ /\.gif$/i
|
||||||
|
return true if path !~ /\.png$/i
|
||||||
|
image_info = FastImage.new(path) rescue nil
|
||||||
|
w, h = *(image_info.try(:size) || [0, 0])
|
||||||
|
# don't optimize large PNGs
|
||||||
|
w > 0 && h > 0 && w * h < LARGE_PNG_SIZE
|
||||||
|
end
|
||||||
|
|
||||||
def self.is_dimensionless_image?(filename, width, height)
|
def self.is_dimensionless_image?(filename, width, height)
|
||||||
FileHelper.is_image?(filename) && (width.blank? || width == 0 || height.blank? || height == 0)
|
FileHelper.is_image?(filename) && (width.blank? || width == 0 || height.blank? || height == 0)
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,7 +62,6 @@ describe Upload do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "computes width & height for images" do
|
it "computes width & height for images" do
|
||||||
FastImage.any_instance.expects(:size).returns([100, 200])
|
|
||||||
ImageSizer.expects(:resize)
|
ImageSizer.expects(:resize)
|
||||||
image.expects(:rewind).twice
|
image.expects(:rewind).twice
|
||||||
Upload.create_for(user_id, image, image_filename, image_filesize)
|
Upload.create_for(user_id, image, image_filename, image_filesize)
|
||||||
|
|
Loading…
Reference in New Issue