From 6e9c00464d2c96333a752689701b1b070aae1a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Tue, 27 Aug 2024 23:05:06 +0800 Subject: [PATCH] Fix error when image type not support by minimagick. --- app/controllers/page_contents_controller.rb | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/controllers/page_contents_controller.rb b/app/controllers/page_contents_controller.rb index 1b698bc..e292ebf 100644 --- a/app/controllers/page_contents_controller.rb +++ b/app/controllers/page_contents_controller.rb @@ -46,16 +46,20 @@ class PageContentsController < OrbitAdminController image_file = Tempfile.new image_file.binmode image_file.write(res.body) - image = MiniMagick::Image.open(image_file.path) - IMG_INFO[img_src] = {width: image[:width], height: image[:height]} - if image[:width] - metas << {"property" => "og:image:width", "content" => image[:width].to_s} + begin + image = MiniMagick::Image.open(image_file.path) + IMG_INFO[img_src] = {width: image[:width], height: image[:height]} + if image[:width] + metas << {"property" => "og:image:width", "content" => image[:width].to_s} + end + if image[:height] + metas << {"property" => "og:image:height", "content" => image[:height].to_s} + end + image_file.close + image_file.unlink + rescue MiniMagick::Invalid => e + puts ["src = #{img_src}", e.to_s] end - if image[:height] - metas << {"property" => "og:image:height", "content" => image[:height].to_s} - end - image_file.close - image_file.unlink else IMG_INFO[img_src] = {} end