2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-14 07:26:08 -05:00
|
|
|
module Jobs
|
|
|
|
class MigrateUrlSiteSettings < Jobs::Onceoff
|
2018-11-15 20:19:58 -05:00
|
|
|
SETTINGS = [
|
|
|
|
['logo_url', 'logo'],
|
|
|
|
['logo_small_url', 'logo_small'],
|
|
|
|
['digest_logo_url', 'digest_logo'],
|
|
|
|
['mobile_logo_url', 'mobile_logo'],
|
|
|
|
['large_icon_url', 'large_icon'],
|
|
|
|
['favicon_url', 'favicon'],
|
|
|
|
['apple_touch_icon_url', 'apple_touch_icon'],
|
|
|
|
['default_opengraph_image_url', 'opengraph_image'],
|
|
|
|
['twitter_summary_large_image_url', 'twitter_summary_large_image'],
|
|
|
|
['push_notifications_icon_url', 'push_notifications_icon'],
|
|
|
|
]
|
|
|
|
|
2018-11-14 07:26:08 -05:00
|
|
|
def execute_onceoff(args)
|
2018-11-15 20:19:58 -05:00
|
|
|
SETTINGS.each do |old_setting, new_setting|
|
2018-11-14 07:26:08 -05:00
|
|
|
old_url = DB.query_single(
|
|
|
|
"SELECT value FROM site_settings WHERE name = '#{old_setting}'"
|
|
|
|
).first
|
|
|
|
|
|
|
|
next if old_url.blank?
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
file = nil
|
|
|
|
sleep_interval = 5
|
|
|
|
|
|
|
|
loop do
|
|
|
|
url = UrlHelper.absolute_without_cdn(old_url)
|
|
|
|
|
|
|
|
begin
|
|
|
|
file = FileHelper.download(
|
|
|
|
url,
|
|
|
|
max_file_size: [
|
|
|
|
SiteSetting.max_image_size_kb.kilobytes,
|
|
|
|
20.megabytes
|
|
|
|
].max,
|
|
|
|
tmp_file_name: 'tmp_site_setting_logo',
|
|
|
|
skip_rate_limit: true,
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2018-11-15 02:16:14 -05:00
|
|
|
rescue OpenURI::HTTPError,
|
2018-11-21 23:14:27 -05:00
|
|
|
OpenSSL::SSL::SSLError,
|
2018-11-15 02:16:14 -05:00
|
|
|
Net::OpenTimeout,
|
|
|
|
Net::ReadTimeout,
|
|
|
|
Errno::ECONNREFUSED,
|
2018-11-21 23:16:52 -05:00
|
|
|
EOFError,
|
2018-11-19 00:07:34 -05:00
|
|
|
SocketError,
|
2018-11-15 02:16:14 -05:00
|
|
|
Discourse::InvalidParameters => e
|
|
|
|
|
2018-11-20 18:55:50 -05:00
|
|
|
logger.warn(
|
2018-11-14 07:26:08 -05:00
|
|
|
"Error encountered when trying to download file " +
|
|
|
|
"for #{new_setting}.\n#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
break if file || (file.blank? && count >= 3)
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Failed to download upload from #{url} for #{new_setting}. Retrying..."
|
|
|
|
)
|
|
|
|
|
|
|
|
sleep(count * sleep_interval)
|
|
|
|
end
|
|
|
|
|
|
|
|
next if file.blank?
|
|
|
|
|
|
|
|
upload = UploadCreator.new(
|
|
|
|
file,
|
|
|
|
"#{new_setting}",
|
|
|
|
origin: UrlHelper.absolute(old_url),
|
|
|
|
for_site_setting: true
|
|
|
|
).create_for(Discourse.system_user.id)
|
|
|
|
|
2019-05-06 21:00:09 -04:00
|
|
|
SiteSetting.set(new_setting, upload)
|
2018-11-14 07:26:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def logger
|
|
|
|
Rails.logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|