mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 00:41:16 +08:00
FIX: don't automagically downsize uploaded images that are larger than 10MB
FIX: don't optimize GIFs since ImageOption was disabled for GIFs (too slow)
This commit is contained in:
@ -71,11 +71,15 @@ 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 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) && File.size(tempfile.path) > 0
|
uploaded_size = File.size(tempfile.path)
|
||||||
|
if SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) && uploaded_size > 0 && uploaded_size < 10.megabytes
|
||||||
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 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes
|
while attempt > 0
|
||||||
|
downsized_size = File.size(tempfile.path)
|
||||||
|
break if downsized_size > uploaded_size
|
||||||
|
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
|
||||||
|
@ -107,13 +107,13 @@ class Upload < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# optimize image
|
# optimize image (but not for GIFs)
|
||||||
|
if filename !~ /\.GIF$/i
|
||||||
ImageOptim.new.optimize_image!(file.path) rescue nil
|
ImageOptim.new.optimize_image!(file.path) rescue nil
|
||||||
|
# update the file size
|
||||||
# correct size so it displays the optimized image size which is the only
|
|
||||||
# one that is stored
|
|
||||||
filesize = File.size(file.path)
|
filesize = File.size(file.path)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# compute the sha of the file
|
# compute the sha of the file
|
||||||
sha1 = Digest::SHA1.file(file).hexdigest
|
sha1 = Digest::SHA1.file(file).hexdigest
|
||||||
|
Reference in New Issue
Block a user