v0.17.1 Handling of Gossamer text tags and TIFF handling fix
This commit is contained in:
parent
b547e7fc6a
commit
2139ddc1fe
@ -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,11 +385,13 @@ 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')
|
||||||
system("convert #{file_path} #{png_path}")
|
begin
|
||||||
png_path
|
system("convert #{file_path} #{png_path}")
|
||||||
rescue => e
|
png_path if File.exist?(png_path)
|
||||||
puts "Failed to convert image #{file_path}: #{e.message}"
|
rescue => e
|
||||||
nil
|
puts "Failed to convert image #{file_path}: #{e.message}"
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Helper method to resize an image to specified dimensions
|
# Helper method to resize an image to specified dimensions
|
||||||
@ -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'
|
||||||
converted_tiff_to_png_path = convert_tiff_to_png(temp_file.path)
|
begin
|
||||||
if converted_tiff_to_png_path.nil?
|
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}"
|
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!(
|
||||||
@ -730,8 +749,11 @@ class GossamerForumsImporter < ImportScripts::Base
|
|||||||
# Set default message body if the sanitized message is blank
|
# Set default message body if the sanitized message is blank
|
||||||
sanitized_message = " " if sanitized_message.strip.empty?
|
sanitized_message = " " if sanitized_message.strip.empty?
|
||||||
|
|
||||||
# # 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
|
||||||
|
Loading…
Reference in New Issue
Block a user