do not pull hotlinked images when max_image_size_kb == 0
This commit is contained in:
parent
a01b423263
commit
7e7d951152
|
@ -14,7 +14,7 @@ class UploadsController < ApplicationController
|
||||||
# check the file size (note: this might also be done in the web server)
|
# check the file size (note: this might also be done in the web server)
|
||||||
filesize = File.size(file.tempfile)
|
filesize = File.size(file.tempfile)
|
||||||
type = SiteSetting.authorized_image?(file) ? "image" : "attachment"
|
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
|
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)
|
upload = Upload.create_for(current_user.id, file, filesize)
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Jobs
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
# maximum size of the file in bytes
|
# maximum size of the file in bytes
|
||||||
@max_size = SiteSetting.max_image_size_kb * 1024
|
@max_size = SiteSetting.max_image_size_kb.kilobytes
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
|
@ -34,7 +34,7 @@ module Jobs
|
||||||
upload = Upload.create_for(post.user_id, file, hotlinked.size, src)
|
upload = Upload.create_for(post.user_id, file, hotlinked.size, src)
|
||||||
downloaded_urls[src] = upload.url
|
downloaded_urls[src] = upload.url
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
# have we successfuly downloaded that file?
|
# have we successfuly downloaded that file?
|
||||||
|
@ -54,7 +54,7 @@ module Jobs
|
||||||
raw.gsub!(src, "<img src='#{url}'>")
|
raw.gsub!(src, "<img src='#{url}'>")
|
||||||
end
|
end
|
||||||
rescue => e
|
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
|
ensure
|
||||||
# close & delete the temp file
|
# close & delete the temp file
|
||||||
hotlinked && hotlinked.close!
|
hotlinked && hotlinked.close!
|
||||||
|
@ -81,6 +81,7 @@ module Jobs
|
||||||
end
|
end
|
||||||
|
|
||||||
def download(url)
|
def download(url)
|
||||||
|
return if @max_size <= 0
|
||||||
extension = File.extname(URI.parse(url).path)
|
extension = File.extname(URI.parse(url).path)
|
||||||
tmp = Tempfile.new(["discourse-hotlinked", extension])
|
tmp = Tempfile.new(["discourse-hotlinked", extension])
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ class SiteSetting < ActiveRecord::Base
|
||||||
setting(:title_prettify, true)
|
setting(:title_prettify, true)
|
||||||
|
|
||||||
client_setting(:max_image_size_kb, 2048)
|
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')
|
client_setting(:authorized_extensions, '.jpg|.jpeg|.png|.gif')
|
||||||
|
|
||||||
# settings only available server side
|
# settings only available server side
|
||||||
|
|
|
@ -17,7 +17,7 @@ class UriAdapter
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_to_tempfile(src)
|
def copy_to_tempfile(src)
|
||||||
while data = src.read(16*1024)
|
while data = src.read(16.kilobytes)
|
||||||
tempfile.write(data)
|
tempfile.write(data)
|
||||||
end
|
end
|
||||||
src.close
|
src.close
|
||||||
|
@ -30,7 +30,7 @@ class UriAdapter
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_uploaded_file
|
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)
|
copy_to_tempfile(content)
|
||||||
content_type = content.content_type if content.respond_to?(:content_type)
|
content_type = content.content_type if content.respond_to?(:content_type)
|
||||||
|
@ -61,4 +61,4 @@ class TempfileFactory
|
||||||
def basename
|
def basename
|
||||||
File.basename(@name, extension).gsub(ILLEGAL_FILENAME_CHARACTERS, '_')
|
File.basename(@name, extension).gsub(ILLEGAL_FILENAME_CHARACTERS, '_')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,7 @@ class AvatarUploadPolicy
|
||||||
end
|
end
|
||||||
|
|
||||||
def max_size_kb
|
def max_size_kb
|
||||||
SiteSetting.max_image_size_kb * 1024
|
SiteSetting.max_image_size_kb.kilobytes
|
||||||
end
|
end
|
||||||
|
|
||||||
def too_big?
|
def too_big?
|
||||||
|
|
|
@ -8,7 +8,7 @@ class MemInfo
|
||||||
system = `uname`.strip
|
system = `uname`.strip
|
||||||
if system == "Darwin"
|
if system == "Darwin"
|
||||||
s = `sysctl -n hw.memsize`.strip
|
s = `sysctl -n hw.memsize`.strip
|
||||||
s.to_i / 1024
|
s.to_i / 1.kilobyte
|
||||||
else
|
else
|
||||||
s = `grep MemTotal /proc/meminfo`
|
s = `grep MemTotal /proc/meminfo`
|
||||||
/(\d+)/.match(s)[0].try(:to_i)
|
/(\d+)/.match(s)[0].try(:to_i)
|
||||||
|
|
Loading…
Reference in New Issue