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

@@ -26,9 +26,9 @@ module Downloads
end
end
def download!(tries: 3, **options)
def download!(url: uncached_url, tries: 3, **options)
Retriable.retriable(on: RETRIABLE_ERRORS, tries: tries, base_interval: 0) do
file = http_get_streaming(uncached_url, headers: strategy.headers, **options)
file = http_get_streaming(url, headers: strategy.headers, **options)
return [file, strategy]
end
end
@@ -43,6 +43,8 @@ module Downloads
size = 0
res = HTTParty.get(url, httparty_options) do |chunk|
next if chunk.code == 302
size += chunk.size
raise Error.new("File is too large (max size: #{max_size})") if size > max_size && max_size > 0
@@ -66,6 +68,10 @@ module Downloads
url
end
def preview_url
@preview_url ||= Addressable::URI.parse(strategy.preview_url)
end
def file_url
@file_url ||= Addressable::URI.parse(strategy.image_url)
end