From 0a08c18a1437f71156e540e63eb89e7ea4ab3825 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 24 May 2017 13:46:57 -0400 Subject: [PATCH] FIX: Don't rate limit gravatar downloads --- app/models/user_avatar.rb | 3 ++- lib/file_helper.rb | 14 ++++++++++++-- lib/final_destination.rb | 4 +++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index 8005e7d2491..87e674d0cff 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -23,7 +23,8 @@ class UserAvatar < ActiveRecord::Base tempfile = FileHelper.download( gravatar_url, max_file_size: SiteSetting.max_image_size_kb.kilobytes, - tmp_file_name: "gravatar" + tmp_file_name: "gravatar", + skip_rate_limit: true ) if tempfile upload = UploadCreator.new(tempfile, 'gravatar.png', origin: gravatar_url, type: "avatar").create_for(user_id) diff --git a/lib/file_helper.rb b/lib/file_helper.rb index 4641a200def..d87db228bbd 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -7,11 +7,21 @@ class FileHelper filename =~ images_regexp end - def self.download(url, max_file_size:, tmp_file_name:, follow_redirect: false, read_timeout: 5) + def self.download(url, + max_file_size:, + tmp_file_name:, + follow_redirect: false, + read_timeout: 5, + skip_rate_limit: false) + url = "https:" + url if url.start_with?("//") raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\// - uri = FinalDestination.new(url, max_redirects: follow_redirect ? 5 : 1).resolve + uri = FinalDestination.new( + url, + max_redirects: follow_redirect ? 5 : 1, + skip_rate_limit: skip_rate_limit + ).resolve return unless uri.present? extension = File.extname(uri.path) diff --git a/lib/final_destination.rb b/lib/final_destination.rb index 529c906dcb0..8ce054d19b7 100644 --- a/lib/final_destination.rb +++ b/lib/final_destination.rb @@ -99,7 +99,9 @@ class FinalDestination end # Rate limit how often this IP can be crawled - RateLimiter.new(nil, "crawl-destination-ip:#{address_s}", 100, 1.hour).performed! + unless @opts[:skip_rate_limit] + RateLimiter.new(nil, "crawl-destination-ip:#{address_s}", 100, 1.hour).performed! + end true rescue RateLimiter::LimitExceeded