DEV: send url string to FileHelper and refactor another open-uri call

FileHelper.download requires a string not a URI. I also found another
instance of using open-uri directly and swapped it out to use
FileHelper.

I also updated it to not `read` a file if it comes back nil.

Follow up to: fe01099a38
This commit is contained in:
Blake Erickson 2019-11-14 08:20:36 -07:00
parent f72730703a
commit 73e33ce243
2 changed files with 8 additions and 3 deletions

View File

@ -93,7 +93,12 @@ module DiscourseNarrativeBot
end end
def fetch_image(url) def fetch_image(url)
URI(url).open('rb', redirect: true, allow_redirections: :all).read FileHelper.download(
url.to_s,
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: 'narrative-bot-logo',
follow_redirect: true
)&.read
rescue OpenURI::HTTPError rescue OpenURI::HTTPError
# Ignore if fetching image returns a non 200 response # Ignore if fetching image returns a non 200 response
end end

View File

@ -110,11 +110,11 @@ after_initialize do
def fetch_avatar_url(user) def fetch_avatar_url(user)
avatar_url = UrlHelper.absolute(Discourse.base_uri + user.avatar_template.gsub('{size}', '250')) avatar_url = UrlHelper.absolute(Discourse.base_uri + user.avatar_template.gsub('{size}', '250'))
FileHelper.download( FileHelper.download(
avatar_url, avatar_url.to_s,
max_file_size: SiteSetting.max_image_size_kb.kilobytes, max_file_size: SiteSetting.max_image_size_kb.kilobytes,
tmp_file_name: 'narrative-bot-avatar', tmp_file_name: 'narrative-bot-avatar',
follow_redirect: true follow_redirect: true
).read )&.read
rescue OpenURI::HTTPError rescue OpenURI::HTTPError
# Ignore if fetching image returns a non 200 response # Ignore if fetching image returns a non 200 response
end end