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
# v0.17
# v0.17.1
require 'mysql2'
require 'open-uri'
@ -385,11 +385,13 @@ class GossamerForumsImporter < ImportScripts::Base
# Helper method to convert TIFF to PNG
def convert_tiff_to_png(file_path)
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
begin
system("convert #{file_path} #{png_path}")
png_path if File.exist?(png_path)
rescue => e
puts "Failed to convert image #{file_path}: #{e.message}"
nil
end
end
# Helper method to resize an image to specified dimensions
@ -468,16 +470,18 @@ 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?
begin
converted_tiff_to_png_path = convert_tiff_to_png(temp_file.path)
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}"
temp_file.close
temp_file.unlink
next
end
temp_file.close
temp_file.unlink
temp_file = File.open(converted_tiff_to_png_path)
end
# Resize the image for the avatar / profile picture (200x200)
@ -598,6 +602,18 @@ class GossamerForumsImporter < ImportScripts::Base
title
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
def import_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
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
sanitized_post_message.sub(/\n?\[signature\]\n?\z/, '')
post = Post.create!(
@ -730,8 +749,11 @@ class GossamerForumsImporter < ImportScripts::Base
# Set default message body if the sanitized message is blank
sanitized_message = " " if sanitized_message.strip.empty?
# # 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
# # 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
# 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
title = row['msg_subject']&.strip