v0.17.1 Handling of Gossamer text tags and TIFF handling fix

This commit is contained in:
David Sainty 2024-06-28 01:44:01 +10:00
parent b547e7fc6a
commit 2139ddc1fe

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.17 # v0.17.1
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -385,12 +385,14 @@ class GossamerForumsImporter < ImportScripts::Base
# Helper method to convert TIFF to PNG # Helper method to convert TIFF to PNG
def convert_tiff_to_png(file_path) def convert_tiff_to_png(file_path)
png_path = file_path.sub('.tiff', '.png').sub('.tif', '.png') png_path = file_path.sub('.tiff', '.png').sub('.tif', '.png')
begin
system("convert #{file_path} #{png_path}") system("convert #{file_path} #{png_path}")
png_path png_path if File.exist?(png_path)
rescue => e rescue => e
puts "Failed to convert image #{file_path}: #{e.message}" puts "Failed to convert image #{file_path}: #{e.message}"
nil nil
end end
end
# 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)
@ -468,16 +470,18 @@ class GossamerForumsImporter < ImportScripts::Base
# Convert TIFF to PNG if necessary # Convert TIFF to PNG if necessary
if file['File_MimeType'] == 'image/tiff' if file['File_MimeType'] == 'image/tiff'
begin
converted_tiff_to_png_path = convert_tiff_to_png(temp_file.path) converted_tiff_to_png_path = convert_tiff_to_png(temp_file.path)
if converted_tiff_to_png_path.nil? raise "Conversion of TIFF failed" if converted_tiff_to_png_path.nil?
temp_file.close
temp_file.unlink
temp_file = File.open(converted_tiff_to_png_path)
rescue => e
puts "Skipping image due to convert failure: #{temp_file.path}" puts "Skipping image due to convert failure: #{temp_file.path}"
temp_file.close temp_file.close
temp_file.unlink temp_file.unlink
next next
end end
temp_file.close
temp_file.unlink
temp_file = File.open(converted_tiff_to_png_path)
end end
# Resize the image for the avatar / profile picture (200x200) # Resize the image for the avatar / profile picture (200x200)
@ -598,6 +602,18 @@ class GossamerForumsImporter < ImportScripts::Base
title title
end end
# Convert Gossamer tags to Discourse markdown
def convert_gossamer_tags_to_markdown(text)
text.gsub!(/\[b\](.*?)\[\/b\]/, '**\1**')
text.gsub!(/\[i\](.*?)\[\/i\]/, '*\1*')
text.gsub!(/\[img\](.*?)\[\/img\]/, '![image](\1)')
text.gsub!(/\[quote\](.*?)\[\/quote\]/m, '[quote]\1[/quote]')
text.gsub!(/\[quote (.*?)\](.*?)\[\/quote\]/m, '[quote=\1]\2[/quote]')
text.gsub!(/\[font "(.*?)"\](.*?)\[\/font\]/m, '\2') # Ignoring font changes
text.gsub!(/\[size (\d+)\](.*?)\[\/size\]/m, '\2') # Ignoring size changes
text
end
# Import topics and posts from Gossamer Forums to Discourse # Import topics and posts from Gossamer Forums to Discourse
def import_topics_and_posts_with_attachments def import_topics_and_posts_with_attachments
puts "Importing topics and posts with attachments..." puts "Importing topics and posts with attachments..."
@ -683,6 +699,9 @@ class GossamerForumsImporter < ImportScripts::Base
# Ensure the raw post string contents itself is acceptable to Discourse # Ensure the raw post string contents itself is acceptable to Discourse
sanitized_post_message = row['post_message']&.tr("\0", '') || "" sanitized_post_message = row['post_message']&.tr("\0", '') || ""
# Convert Gossamer tags to Discourse markdown
sanitized_post_message = convert_gossamer_tags_to_markdown(sanitized_post_message)
# Remove the [signature] label from appearing at the end of the messages after import # Remove the [signature] label from appearing at the end of the messages after import
sanitized_post_message.sub(/\n?\[signature\]\n?\z/, '') sanitized_post_message.sub(/\n?\[signature\]\n?\z/, '')
post = Post.create!( post = Post.create!(
@ -733,6 +752,9 @@ class GossamerForumsImporter < ImportScripts::Base
# # If we do not change the "min personal message post length" to 1, we need this. # # If we do not change the "min personal message post length" to 1, we need this.
# sanitized_message = sanitized_message.ljust(10, ' ') if sanitized_message.length < 10 # sanitized_message = sanitized_message.ljust(10, ' ') if sanitized_message.length < 10
# Convert potential Gossamer tags to Discourse markdown
sanitized_message = convert_gossamer_tags_to_markdown(sanitized_message)
# Check and set a default title if the original title is nil or empty # Check and set a default title if the original title is nil or empty
title = row['msg_subject']&.strip title = row['msg_subject']&.strip
title = "<no subject>" if title.nil? || title.empty? title = "<no subject>" if title.nil? || title.empty?