From e165cb0ab2931067e1e6a15a0811b4bdd5d861b6 Mon Sep 17 00:00:00 2001 From: Toks Date: Sat, 15 Aug 2015 17:12:00 -0400 Subject: [PATCH] fixes #2491 --- app/logical/downloads/rewrite_strategies/base.rb | 15 ++++++++++----- .../downloads/rewrite_strategies/tumblr.rb | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/logical/downloads/rewrite_strategies/base.rb b/app/logical/downloads/rewrite_strategies/base.rb index b1d8cd365..bd17fc0ce 100644 --- a/app/logical/downloads/rewrite_strategies/base.rb +++ b/app/logical/downloads/rewrite_strategies/base.rb @@ -14,16 +14,21 @@ module Downloads end protected - def http_exists?(url, headers) - exists = false + def http_head_request(url, headers) uri = URI.parse(url) Net::HTTP.start(uri.host, uri.port) do |http| http.request_head(uri.request_uri, headers) do |res| - if res.is_a?(Net::HTTPSuccess) - exists = true - end + return res end end + end + + def http_exists?(url, headers) + exists = false + res = http_head_request(url, headers) + if res.is_a?(Net::HTTPSuccess) + exists = true + end exists end end diff --git a/app/logical/downloads/rewrite_strategies/tumblr.rb b/app/logical/downloads/rewrite_strategies/tumblr.rb index 7ea3c8296..9771fc7be 100644 --- a/app/logical/downloads/rewrite_strategies/tumblr.rb +++ b/app/logical/downloads/rewrite_strategies/tumblr.rb @@ -12,13 +12,22 @@ module Downloads protected def rewrite_thumbnails(url, headers) - if url =~ %r{^https?://.+\.tumblr\.com/(?:\w+/)?(?:tumblr_)?(\w+_)(\d+)\..+$} + if url =~ %r{^https?://.+\.tumblr\.com/(?:\w+/)?(?:tumblr_)?(\w+_)(\d+)(\..+)$} match = $1 given_size = $2 + file_ext = $3 big_1280_url = url.sub(match + given_size, match + "1280") - if http_exists?(big_1280_url, headers) - return [big_1280_url, headers] + if file_ext == ".gif" + res = http_head_request(big_1280_url, headers) + # Sometimes the 1280 version of a gif is actually a static jpeg. We don't want that so we only use the 1280 version if it really is a gif. + if res.is_a?(Net::HTTPSuccess) && res["content-type"] == "image/gif" + return [big_1280_url, headers] + end + else + if http_exists?(big_1280_url, headers) + return [big_1280_url, headers] + end end end