parent
0210a7f2bf
commit
ca1fd774a1
|
@ -25,54 +25,57 @@ class FileHelper
|
||||||
follow_redirect: false,
|
follow_redirect: false,
|
||||||
read_timeout: 5,
|
read_timeout: 5,
|
||||||
skip_rate_limit: false,
|
skip_rate_limit: false,
|
||||||
verbose: false)
|
verbose: nil)
|
||||||
|
|
||||||
|
# verbose logging is default while debugging onebox
|
||||||
|
verbose = verbose.nil? ? true : verbose
|
||||||
|
|
||||||
url = "https:" + url if url.start_with?("//")
|
url = "https:" + url if url.start_with?("//")
|
||||||
raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\//
|
raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\//
|
||||||
|
|
||||||
tmp = nil
|
dest = FinalDestination.new(
|
||||||
|
|
||||||
fd = FinalDestination.new(
|
|
||||||
url,
|
url,
|
||||||
max_redirects: follow_redirect ? 5 : 1,
|
max_redirects: follow_redirect ? 5 : 1,
|
||||||
skip_rate_limit: skip_rate_limit,
|
skip_rate_limit: skip_rate_limit,
|
||||||
verbose: verbose
|
verbose: verbose
|
||||||
)
|
)
|
||||||
|
uri = dest.resolve
|
||||||
|
|
||||||
fd.get do |response, chunk, uri|
|
if !uri && dest.status_code.to_i >= 400
|
||||||
if tmp.nil?
|
# attempt error API compatability
|
||||||
# error handling
|
io = FakeIO.new
|
||||||
if uri.blank?
|
io.status = [dest.status_code.to_s, ""]
|
||||||
if response.code.to_i >= 400
|
|
||||||
# attempt error API compatibility
|
|
||||||
io = FakeIO.new
|
|
||||||
io.status = [response.code, ""]
|
|
||||||
raise OpenURI::HTTPError.new("#{response.code} Error", io)
|
|
||||||
else
|
|
||||||
log(:error, "FinalDestination did not work for: #{url}") if verbose
|
|
||||||
throw :done
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# first run
|
# TODO perhaps translate and add Discourse::DownloadError
|
||||||
tmp_file_ext = File.extname(uri.path)
|
raise OpenURI::HTTPError.new("#{dest.status_code} Error", io)
|
||||||
|
end
|
||||||
|
|
||||||
if tmp_file_ext.blank? && response.content_type.present?
|
unless uri
|
||||||
ext = MiniMime.lookup_by_content_type(response.content_type)&.extension
|
log(:error, "FinalDestination did not work for: #{url}") if verbose
|
||||||
ext = "jpg" if ext == "jpe"
|
return
|
||||||
tmp_file_ext = "." + ext if ext.present?
|
end
|
||||||
end
|
|
||||||
|
|
||||||
tmp = Tempfile.new([tmp_file_name, tmp_file_ext])
|
downloaded = uri.open("rb", read_timeout: read_timeout)
|
||||||
tmp.binmode
|
|
||||||
|
extension = File.extname(uri.path)
|
||||||
|
|
||||||
|
if extension.blank? && downloaded.content_type.present?
|
||||||
|
ext = MiniMime.lookup_by_content_type(downloaded.content_type)&.extension
|
||||||
|
ext = "jpg" if ext == "jpe"
|
||||||
|
extension = "." + ext if ext.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
tmp = Tempfile.new([tmp_file_name, extension])
|
||||||
|
|
||||||
|
File.open(tmp.path, "wb") do |f|
|
||||||
|
while f.size <= max_file_size && data = downloaded.read(512.kilobytes)
|
||||||
|
f.write(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
tmp.write(chunk)
|
|
||||||
|
|
||||||
throw :done if tmp.size > max_file_size
|
|
||||||
end
|
end
|
||||||
|
|
||||||
tmp
|
tmp
|
||||||
|
ensure
|
||||||
|
downloaded&.close
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.optimize_image!(filename)
|
def self.optimize_image!(filename)
|
||||||
|
|
Loading…
Reference in New Issue