uploads: fix incorrect remote sizes on pixiv uploads.

Bug: the uploads page showed a remote size of 146 bytes for Pixiv uploads.

Cause: we didn't spoof the Referer header when making the HEAD request
for the image, causing Pixiv to return a 403 error.

Also fix the case where the Content-Length header is absent.
This commit is contained in:
evazion
2020-06-24 02:54:30 -05:00
parent 8eac82a971
commit 4074cc99f9
5 changed files with 14 additions and 7 deletions

View File

@@ -145,10 +145,13 @@ module Sources
end end
# Returns the size of the image resource without actually downloading the file. # Returns the size of the image resource without actually downloading the file.
def size def remote_size
http.head(image_url).content_length.to_i response = http.head(image_url)
return nil unless response.status == 200 && response.content_length.present?
response.content_length.to_i
end end
memoize :size memoize :remote_size
# Download the file at the given url, or at the main image url by default. # Download the file at the given url, or at the main image url by default.
def download_file!(download_url = image_url) def download_file!(download_url = image_url)
@@ -159,7 +162,7 @@ module Sources
end end
def http def http
Danbooru::Http.public_only.timeout(30).max_size(Danbooru.config.max_file_size) Danbooru::Http.headers(headers).public_only.timeout(30).max_size(Danbooru.config.max_file_size)
end end
memoize :http memoize :http

View File

@@ -8,7 +8,7 @@ class UploadService
UploadPreprocessorDelayedStartJob.perform_later(url, ref, CurrentUser.user) UploadPreprocessorDelayedStartJob.perform_later(url, ref, CurrentUser.user)
strategy = Sources::Strategies.find(url, ref) strategy = Sources::Strategies.find(url, ref)
remote_size = strategy.size remote_size = strategy.remote_size
return [upload, remote_size] return [upload, remote_size]
end end

View File

@@ -1,7 +1,7 @@
<% if params[:url] %> <% if params[:url] %>
<p id="upload-image-metadata"> <p id="upload-image-metadata">
<strong>Size</strong> <strong>Size</strong>
<% if @remote_size %> <% if @remote_size.present? %>
<span id="upload-image-metadata-filesize"><%= number_to_human_size(@remote_size) %></span> <span id="upload-image-metadata-filesize"><%= number_to_human_size(@remote_size) %></span>
<% end %> <% end %>
<span id="upload-image-metadata-resolution"></span> <span id="upload-image-metadata-resolution"></span>

View File

@@ -14,7 +14,7 @@ module Sources
assert_equal(page_url, site.page_url) if page_url.present? assert_equal(page_url, site.page_url) if page_url.present?
assert_equal(tags.sort, site.tags.map(&:first).sort) assert_equal(tags.sort, site.tags.map(&:first).sort)
assert_equal(profile_url.to_s, site.profile_url.to_s) assert_equal(profile_url.to_s, site.profile_url.to_s)
assert_equal(size, site.size) assert_equal(size, site.remote_size)
assert_nothing_raised { site.to_h } assert_nothing_raised { site.to_h }
end end

View File

@@ -97,6 +97,10 @@ module Sources
assert_equal("uroobnad", @site.artist_name) assert_equal("uroobnad", @site.artist_name)
end end
should "get the remote image size" do
assert_equal(854_653, @site.remote_size)
end
should "get the full size image url" do should "get the full size image url" do
assert_equal("https://i.pximg.net/img-original/img/2017/11/21/05/12/37/65981735_p0.jpg", @site.image_url) assert_equal("https://i.pximg.net/img-original/img/2017/11/21/05/12/37/65981735_p0.jpg", @site.image_url)
end end