v0.16.1 Tweak for rogue image handling

This commit is contained in:
David Sainty 2024-06-27 02:14:09 +10:00
parent 2c9eb66ce8
commit 96cb9e7664

View File

@ -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 = File.open(convert_tiff_to_png(temp_file.path))
temp_file.unlink
next
end
temp_file.close
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)