Refactor FileHelper to use keyword arguments.

This commit is contained in:
Robin Ward
2017-05-24 13:42:52 -04:00
parent 87ac758f05
commit cdbe027c1c
12 changed files with 81 additions and 15 deletions

View File

@@ -102,7 +102,12 @@ class StaticController < ApplicationController
data = DistributedMemoizer.memoize('favicon' + SiteSetting.favicon_url, 60*30) do
begin
file = FileHelper.download(SiteSetting.favicon_url, 50.kilobytes, "favicon.png", true)
file = FileHelper.download(
SiteSetting.favicon_url,
max_file_size: 50.kilobytes,
tmp_file_name: "favicon.png",
follow_redirect: true
)
data = file.read
file.unlink
data

View File

@@ -62,7 +62,11 @@ class UploadsController < ApplicationController
if file.nil?
if url.present? && is_api?
maximum_upload_size = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes
tempfile = FileHelper.download(url, maximum_upload_size, "discourse-upload-#{type}") rescue nil
tempfile = FileHelper.download(
url,
max_file_size: maximum_upload_size,
tmp_file_name: "discourse-upload-#{type}"
) rescue nil
filename = File.basename(URI.parse(url).path)
end
else

View File

@@ -134,7 +134,13 @@ class UserAvatarsController < ApplicationController
unless File.exist? path
FileUtils.mkdir_p PROXY_PATH
tmp = FileHelper.download(url, 1.megabyte, filename, true, 10)
tmp = FileHelper.download(
url,
max_file_size: 1.megabyte,
tmp_file_name: filename,
follow_redirect: true,
read_timeout: 10
)
FileUtils.mv tmp.path, path
end

View File

@@ -36,7 +36,12 @@ module Jobs
# have we already downloaded that file?
unless downloaded_urls.include?(src)
begin
hotlinked = FileHelper.download(src, @max_size, "discourse-hotlinked", true)
hotlinked = FileHelper.download(
src,
max_file_size: @max_size,
tmp_file_name: "discourse-hotlinked",
follow_redirect: true
)
rescue Discourse::InvalidParameters
end
if hotlinked

View File

@@ -246,7 +246,12 @@ class OptimizedImage < ActiveRecord::Base
# download if external
if external
url = SiteSetting.scheme + ":" + previous_url
file = FileHelper.download(url, max_file_size_kb, "discourse", true) rescue nil
file = FileHelper.download(
url,
max_file_size: max_file_size_kb,
tmp_file_name: "discourse",
follow_redirect: true
) rescue nil
path = file.path
else
path = local_store.path_for(optimized_image)

View File

@@ -93,7 +93,12 @@ class Upload < ActiveRecord::Base
# download if external
if external
url = SiteSetting.scheme + ":" + previous_url
file = FileHelper.download(url, max_file_size_kb, "discourse", true) rescue nil
file = FileHelper.download(
url,
max_file_size: max_file_size_kb,
tmp_file_name: "discourse",
follow_redirect: true
) rescue nil
path = file.path
else
path = local_store.path_for(upload)

View File

@@ -20,7 +20,11 @@ class UserAvatar < ActiveRecord::Base
max = Discourse.avatar_sizes.max
gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404"
tempfile = FileHelper.download(gravatar_url, SiteSetting.max_image_size_kb.kilobytes, "gravatar")
tempfile = FileHelper.download(
gravatar_url,
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: "gravatar"
)
if tempfile
upload = UploadCreator.new(tempfile, 'gravatar.png', origin: gravatar_url, type: "avatar").create_for(user_id)
@@ -63,7 +67,12 @@ class UserAvatar < ActiveRecord::Base
end
def self.import_url_for_user(avatar_url, user, options=nil)
tempfile = FileHelper.download(avatar_url, SiteSetting.max_image_size_kb.kilobytes, "sso-avatar", true)
tempfile = FileHelper.download(
avatar_url,
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: "sso-avatar",
follow_redirect: true
)
ext = FastImage.type(tempfile).to_s
tempfile.rewind