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: fe01099a38f791124130d0adc35f755fb0d5a11b
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
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
# Ignore if fetching image returns a non 200 response
end