v0.16.6 Handling for broken input images... again.

This commit is contained in:
David Sainty 2024-06-27 16:22:12 +10:00
parent a94dfbb550
commit ae0efcb489

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.16.5 # v0.16.6
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -395,12 +395,13 @@ class GossamerForumsImporter < ImportScripts::Base
# Helper method to resize an image to specified dimensions # Helper method to resize an image to specified dimensions
def resize_image(file_path, max_width, max_height) def resize_image(file_path, max_width, max_height)
resized_path = file_path.sub(File.extname(file_path), "_resized#{File.extname(file_path)}") 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}") begin
resized_path system("convert #{file_path} -resize #{max_width}x#{max_height} #{resized_path}")
rescue => e resized_path if File.exist?(resized_path)
puts "Failed to resize image #{file_path}: #{e.message}" rescue => e
nil puts "Failed to resize image #{file_path}: #{e.message}"
end nil
end
# Import user files (profile images) from Gossamer Forums to Discourse # Import user files (profile images) from Gossamer Forums to Discourse
def import_user_files(user) def import_user_files(user)
@ -457,15 +458,17 @@ class GossamerForumsImporter < ImportScripts::Base
end end
# Resize the image for the avatar / profile picture (200x200) # Resize the image for the avatar / profile picture (200x200)
resized_image_path = resize_image(temp_file.path, 200, 200) begin
if resized_image_path.nil? 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}" puts "Skipping image due to resize failure: #{temp_file.path}"
temp_file.close temp_file.close
temp_file.unlink temp_file.unlink
next next
end 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 # Only handle the first image for avator and profile settings
if images_imported == 0 if images_imported == 0