From ae0efcb489cab35ad88dde73119baa07d7aa080a Mon Sep 17 00:00:00 2001 From: saint Date: Thu, 27 Jun 2024 16:22:12 +1000 Subject: [PATCH] v0.16.6 Handling for broken input images... again. --- gossamer_forums.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 937860f..cbf143c 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.16.5 +# v0.16.6 require 'mysql2' require 'open-uri' @@ -395,12 +395,13 @@ class GossamerForumsImporter < ImportScripts::Base # Helper method to resize an image to specified dimensions def resize_image(file_path, max_width, max_height) resized_path = file_path.sub(File.extname(file_path), "_resized#{File.extname(file_path)}") - system("convert #{file_path} -resize #{max_width}x#{max_height} #{resized_path}") - resized_path - rescue => e - puts "Failed to resize image #{file_path}: #{e.message}" - nil - end + begin + system("convert #{file_path} -resize #{max_width}x#{max_height} #{resized_path}") + resized_path if File.exist?(resized_path) + rescue => e + puts "Failed to resize image #{file_path}: #{e.message}" + nil + end # Import user files (profile images) from Gossamer Forums to Discourse def import_user_files(user) @@ -457,15 +458,17 @@ class GossamerForumsImporter < ImportScripts::Base end # Resize the image for the avatar / profile picture (200x200) - resized_image_path = resize_image(temp_file.path, 200, 200) - if resized_image_path.nil? + begin + resized_image_path = resize_image(temp_file.path, 200, 200) + raise "Image resize failed" if resized_image_path.nil? + resized_temp_file = Tempfile.new(['user_image_resized', '.png']) + FileUtils.copy_file(resized_image_path, resized_temp_file.path) + rescue => e puts "Skipping image due to resize failure: #{temp_file.path}" temp_file.close temp_file.unlink next end - resized_temp_file = Tempfile.new(['user_image_resized', '.png']) - FileUtils.copy_file(resized_image_path, resized_temp_file.path) # Only handle the first image for avator and profile settings if images_imported == 0