From 73e33ce2437000da99b95c20522d3a5d2886368f Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Thu, 14 Nov 2019 08:20:36 -0700 Subject: [PATCH] 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 --- .../lib/discourse_narrative_bot/certificate_generator.rb | 7 ++++++- plugins/discourse-narrative-bot/plugin.rb | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb index a66dcba191..a158521149 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb @@ -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 diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb index 31949915f4..54e97a05e7 100644 --- a/plugins/discourse-narrative-bot/plugin.rb +++ b/plugins/discourse-narrative-bot/plugin.rb @@ -110,11 +110,11 @@ after_initialize do def fetch_avatar_url(user) avatar_url = UrlHelper.absolute(Discourse.base_uri + user.avatar_template.gsub('{size}', '250')) FileHelper.download( - avatar_url, + avatar_url.to_s, max_file_size: SiteSetting.max_image_size_kb.kilobytes, tmp_file_name: 'narrative-bot-avatar', follow_redirect: true - ).read + )&.read rescue OpenURI::HTTPError # Ignore if fetching image returns a non 200 response end