do not pull hotlinked images when max_image_size_kb == 0

This commit is contained in:
Régis Hanol 2013-11-13 17:30:48 +01:00
parent a01b423263
commit 7e7d951152
6 changed files with 11 additions and 10 deletions

View file

@ -14,7 +14,7 @@ class UploadsController < ApplicationController
# check the file size (note: this might also be done in the web server)
filesize = File.size(file.tempfile)
type = SiteSetting.authorized_image?(file) ? "image" : "attachment"
max_size_kb = SiteSetting.send("max_#{type}_size_kb") * 1024
max_size_kb = SiteSetting.send("max_#{type}_size_kb").kilobytes
return render status: 413, text: I18n.t("upload.#{type}s.too_large", max_size_kb: max_size_kb) if filesize > max_size_kb
upload = Upload.create_for(current_user.id, file, filesize)

View file

@ -4,7 +4,7 @@ module Jobs
def initialize
# maximum size of the file in bytes
@max_size = SiteSetting.max_image_size_kb * 1024
@max_size = SiteSetting.max_image_size_kb.kilobytes
end
def execute(args)
@ -34,7 +34,7 @@ module Jobs
upload = Upload.create_for(post.user_id, file, hotlinked.size, src)
downloaded_urls[src] = upload.url
else
Rails.logger.warn("Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}")
puts "Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}"
end
end
# have we successfuly downloaded that file?
@ -54,7 +54,7 @@ module Jobs
raw.gsub!(src, "<img src='#{url}'>")
end
rescue => e
Rails.logger.error("Failed to pull hotlinked image: #{src}\n" + e.message + "\n" + e.backtrace.join("\n"))
puts "Failed to pull hotlinked image: #{src}\n" + e.message + "\n" + e.backtrace.join("\n")
ensure
# close & delete the temp file
hotlinked && hotlinked.close!
@ -81,6 +81,7 @@ module Jobs
end
def download(url)
return if @max_size <= 0
extension = File.extname(URI.parse(url).path)
tmp = Tempfile.new(["discourse-hotlinked", extension])

View file

@ -56,7 +56,7 @@ class SiteSetting < ActiveRecord::Base
setting(:title_prettify, true)
client_setting(:max_image_size_kb, 2048)
client_setting(:max_attachment_size_kb, 1024)
client_setting(:max_attachment_size_kb, 1.kilobyte)
client_setting(:authorized_extensions, '.jpg|.jpeg|.png|.gif')
# settings only available server side

View file

@ -17,7 +17,7 @@ class UriAdapter
end
def copy_to_tempfile(src)
while data = src.read(16*1024)
while data = src.read(16.kilobytes)
tempfile.write(data)
end
src.close
@ -30,7 +30,7 @@ class UriAdapter
end
def build_uploaded_file
return if (SiteSetting.max_image_size_kb * 1024) < file_size
return if SiteSetting.max_image_size_kb.kilobytes < file_size
copy_to_tempfile(content)
content_type = content.content_type if content.respond_to?(:content_type)
@ -61,4 +61,4 @@ class TempfileFactory
def basename
File.basename(@name, extension).gsub(ILLEGAL_FILENAME_CHARACTERS, '_')
end
end
end

View file

@ -33,7 +33,7 @@ class AvatarUploadPolicy
end
def max_size_kb
SiteSetting.max_image_size_kb * 1024
SiteSetting.max_image_size_kb.kilobytes
end
def too_big?

View file

@ -8,7 +8,7 @@ class MemInfo
system = `uname`.strip
if system == "Darwin"
s = `sysctl -n hw.memsize`.strip
s.to_i / 1024
s.to_i / 1.kilobyte
else
s = `grep MemTotal /proc/meminfo`
/(\d+)/.match(s)[0].try(:to_i)