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
def download!
@source, @data = http_get_streaming(@source, @data) do |response|
self.content_type = response["Content-Type"]
::File.open(@file_path, "wb") do |out|
out.write(response.body)
::File.open(@file_path, "wb") do |out|
@source, @data = http_get_streaming(@source, @data) do |response|
out.write(response)
end
end
@downloaded_source = @source
@@ -69,7 +68,7 @@ module Downloads
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 = nil if max_size == 0 # unlimited
limit = 4
@@ -90,13 +89,16 @@ module Downloads
validate_local_hosts(url)
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 max_size
len = res["Content-Length"]
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
end
yield(res)
@content_type = res["Content-Type"]
return [src, datums]
else
raise Error.new("HTTP error code: #{res.code} #{res.message}")

View File

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