diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index e2f490657..718e3f73e 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -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 diff --git a/app/logical/upload_service/controller_helper.rb b/app/logical/upload_service/controller_helper.rb index 9eeb6d442..378ca07f3 100644 --- a/app/logical/upload_service/controller_helper.rb +++ b/app/logical/upload_service/controller_helper.rb @@ -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 diff --git a/app/views/uploads/_image.html.erb b/app/views/uploads/_image.html.erb index 7b0a2208e..4ef2a8d6d 100644 --- a/app/views/uploads/_image.html.erb +++ b/app/views/uploads/_image.html.erb @@ -1,7 +1,7 @@ <% if params[:url] %>

Size - <% if @remote_size %> + <% if @remote_size.present? %> <%= number_to_human_size(@remote_size) %> <% end %> diff --git a/test/unit/sources/moebooru_test.rb b/test/unit/sources/moebooru_test.rb index 08d3f0558..868375295 100644 --- a/test/unit/sources/moebooru_test.rb +++ b/test/unit/sources/moebooru_test.rb @@ -14,7 +14,7 @@ module Sources assert_equal(page_url, site.page_url) if page_url.present? assert_equal(tags.sort, site.tags.map(&:first).sort) 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 } end diff --git a/test/unit/sources/pixiv_test.rb b/test/unit/sources/pixiv_test.rb index 4660fde8a..e58cd029c 100644 --- a/test/unit/sources/pixiv_test.rb +++ b/test/unit/sources/pixiv_test.rb @@ -97,6 +97,10 @@ module Sources assert_equal("uroobnad", @site.artist_name) end + should "get the remote image size" do + assert_equal(854_653, @site.remote_size) + end + 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) end