restore streaming behavior for Downloads::File#http_get_streaming

This commit is contained in:
r888888888
2017-07-13 15:30:35 -07:00
parent 79b21bbca8
commit ee6581ab7f
2 changed files with 12 additions and 12 deletions

View File

@@ -36,10 +36,9 @@ module Downloads
end end
def download! def download!
@source, @data = http_get_streaming(@source, @data) do |response| ::File.open(@file_path, "wb") do |out|
self.content_type = response["Content-Type"] @source, @data = http_get_streaming(@source, @data) do |response|
::File.open(@file_path, "wb") do |out| out.write(response)
out.write(response.body)
end end
end end
@downloaded_source = @source @downloaded_source = @source
@@ -69,7 +68,7 @@ module Downloads
end end
end end
def http_get_streaming(src, datums = {}, options = {}) def http_get_streaming(src, datums = {}, options = {}, &block)
max_size = options[:max_size] || Danbooru.config.max_file_size max_size = options[:max_size] || Danbooru.config.max_file_size
max_size = nil if max_size == 0 # unlimited max_size = nil if max_size == 0 # unlimited
limit = 4 limit = 4
@@ -90,13 +89,16 @@ module Downloads
validate_local_hosts(url) validate_local_hosts(url)
begin begin
res = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 10, headers: headers)) res = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(stream_body: true, timeout: 10, headers: headers), &block)
if res.success? if res.success?
if max_size if max_size
len = res["Content-Length"] len = res["Content-Length"]
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
end end
yield(res)
@content_type = res["Content-Type"]
return [src, datums] return [src, datums]
else else
raise Error.new("HTTP error code: #{res.code} #{res.message}") raise Error.new("HTTP error code: #{res.code} #{res.message}")

View File

@@ -15,7 +15,7 @@ module Downloads
context "that fails" do context "that fails" do
setup do setup do
Net::HTTP.stubs(:start).raises(Errno::ETIMEDOUT) HTTParty.stubs(:get).raises(Errno::ETIMEDOUT)
end end
should "retry three times" do should "retry three times" do
@@ -27,8 +27,7 @@ module Downloads
should "stream a file from an HTTP source" do should "stream a file from an HTTP source" do
@download.http_get_streaming(@source) do |resp| @download.http_get_streaming(@source) do |resp|
assert_equal("200", resp.code) assert(resp.size > 0)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
end end
end end
@@ -65,8 +64,7 @@ module Downloads
should "stream a file from an HTTPS source" do should "stream a file from an HTTPS source" do
@download.http_get_streaming(@source) do |resp| @download.http_get_streaming(@source) do |resp|
assert_equal("200", resp.code) assert(resp.size > 0)
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
end end
end end
end end