Downloads::File: fix following 302 redirects during download.

Fixes downloading yande.re preview images for iqdb. Yande.re previews
return a 302 redirect to the actual file. Before the html in the body of
the 302 response would get prepended to the image file.

https://files.yande.re/data/preview/12/8f/128fb480d8589be26d1dce7e1d841bcb.jpg
=> https://assets.yande.re/data/preview/12/8f/128fb480d8589be26d1dce7e1d841bcb.jpg
This commit is contained in:
evazion
2019-10-26 14:04:19 -05:00
parent a15bbe4264
commit a6efaa54a1
2 changed files with 22 additions and 5 deletions

View File

@@ -49,14 +49,19 @@ module Downloads
should "return an uncorrupted file on the second try" do
bomb = stub("bomb")
bomb.expects(:size).raises(IOError)
bomb.stubs(:code).raises(IOError)
resp = stub("resp", success?: true)
HTTParty.expects(:get).twice.multiple_yields("a", bomb, "b", "c").then.multiple_yields("a", "b", "c").returns(resp)
chunk = stub("a")
chunk.stubs(:code).returns(200)
chunk.stubs(:size).returns(1)
chunk.stubs(:to_s).returns("a")
HTTParty.expects(:get).twice.multiple_yields(chunk, bomb).then.multiple_yields(chunk, chunk).returns(resp)
@download.stubs(:is_cloudflare?).returns(false)
tempfile, _ = @download.download!
assert_equal("abc", tempfile.read)
assert_equal("aa", tempfile.read)
end
end
@@ -70,6 +75,12 @@ module Downloads
tempfile, strategy = @download.download!
assert_operator(tempfile.size, :>, 0, "should have data")
end
should "correctly save the file when following 302 redirects" do
download = Downloads::File.new("https://yande.re/post/show/578014")
file, strategy = download.download!(url: download.preview_url)
assert_equal(19134, file.size)
end
end
end
end