From 78bb367b0a704927ca251ffd7bef8e6fdf1a8a9a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 14 Jul 2017 15:16:29 +0900 Subject: [PATCH] FIX: Discobot's certificate shouldn't blow up on non 200 response. https://meta.discourse.org/t/discobot-assets-certificate-svg/65523/6 --- .../certificate_generator.rb | 20 ++++++++++++++++--- 1 file changed, 17 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 f36c264720e..5308f6aa72b 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 @@ -464,7 +464,7 @@ module DiscourseNarrativeBot - + #{name} @@ -530,7 +530,7 @@ module DiscourseNarrativeBot - + #{@discobot_user.username} @@ -568,7 +568,7 @@ module DiscourseNarrativeBot <<~URL - + URL rescue URI::InvalidURIError @@ -576,6 +576,20 @@ module DiscourseNarrativeBot end end + def base64_image_link(url) + if image = fetch_image(url) + "xlink:href=\"data:image/png;base64,#{Base64.strict_encode64(image)}\"" + else + "" + end + end + + def fetch_image(url) + URI(url).open('rb', redirect: true, allow_redirections: :all).read + rescue OpenURI::HTTPError + # Ignore if fetching image returns a non 200 response + end + def avatar_url UrlHelper.absolute(Discourse.base_uri + @user.avatar_template.gsub('{size}', '250')) end