From 96cb9e7664f3f87da8aea3143506b808fe54191b Mon Sep 17 00:00:00 2001 From: saint Date: Thu, 27 Jun 2024 02:14:09 +1000 Subject: [PATCH] v0.16.1 Tweak for rogue image handling --- gossamer_forums.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 6144869..f79c0e0 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.16 +# v0.16.1 require 'mysql2' require 'open-uri' @@ -387,6 +387,9 @@ class GossamerForumsImporter < ImportScripts::Base png_path = file_path.sub('.tiff', '.png').sub('.tif', '.png') system("convert #{file_path} #{png_path}") png_path + rescue => e + puts "Failed to convert image #{file_path}: #{e.message}" + nil end # Helper method to resize an image to specified dimensions @@ -394,6 +397,9 @@ class GossamerForumsImporter < ImportScripts::Base 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 # Import user files (profile images) from Gossamer Forums to Discourse @@ -439,12 +445,25 @@ class GossamerForumsImporter < ImportScripts::Base # Convert TIFF to PNG if necessary if file['File_MimeType'] == 'image/tiff' + converted_tiff_to_png_path = convert_tiff_to_png(temp_file.path) + if converted_tiff_to_png_path.nil? + puts "Skipping image due to convert failure: #{temp_file.path}" + temp_file.close + temp_file.unlink + next + end temp_file.close - temp_file = File.open(convert_tiff_to_png(temp_file.path)) + temp_file = File.open(converted_tiff_to_png_path) 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? + 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)