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:
@@ -145,10 +145,13 @@ module Sources
|
||||
end
|
||||
|
||||
# Returns the size of the image resource without actually downloading the file.
|
||||
def size
|
||||
http.head(image_url).content_length.to_i
|
||||
def remote_size
|
||||
response = http.head(image_url)
|
||||
return nil unless response.status == 200 && response.content_length.present?
|
||||
|
||||
response.content_length.to_i
|
||||
end
|
||||
memoize :size
|
||||
memoize :remote_size
|
||||
|
||||
# Download the file at the given url, or at the main image url by default.
|
||||
def download_file!(download_url = image_url)
|
||||
@@ -159,7 +162,7 @@ module Sources
|
||||
end
|
||||
|
||||
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
|
||||
memoize :http
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ class UploadService
|
||||
UploadPreprocessorDelayedStartJob.perform_later(url, ref, CurrentUser.user)
|
||||
|
||||
strategy = Sources::Strategies.find(url, ref)
|
||||
remote_size = strategy.size
|
||||
remote_size = strategy.remote_size
|
||||
|
||||
return [upload, remote_size]
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% if params[:url] %>
|
||||
<p id="upload-image-metadata">
|
||||
<strong>Size</strong>
|
||||
<% if @remote_size %>
|
||||
<% if @remote_size.present? %>
|
||||
<span id="upload-image-metadata-filesize"><%= number_to_human_size(@remote_size) %></span>
|
||||
<% end %>
|
||||
<span id="upload-image-metadata-resolution"></span>
|
||||
|
||||
Reference in New Issue
Block a user