v0.16 Significant user image handling improvement, including avatar resizing
This commit is contained in:
parent
0c7f521b15
commit
2c9eb66ce8
@ -1,5 +1,5 @@
|
|||||||
# gossamer threads migration-import code
|
# gossamer threads migration-import code
|
||||||
# v0.15.9
|
# v0.16
|
||||||
|
|
||||||
require 'mysql2'
|
require 'mysql2'
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
@ -382,6 +382,20 @@ class GossamerForumsImporter < ImportScripts::Base
|
|||||||
# print "Importing files for user #{user.username}... Done.\n"
|
# print "Importing files for user #{user.username}... Done.\n"
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
end
|
||||||
|
|
||||||
|
# Helper method to resize an image to specified dimensions
|
||||||
|
def resize_image(file_path, max_width, max_height)
|
||||||
|
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
|
||||||
|
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)
|
||||||
print "\rImporting files for user #{user.username}..."
|
print "\rImporting files for user #{user.username}..."
|
||||||
@ -397,32 +411,57 @@ class GossamerForumsImporter < ImportScripts::Base
|
|||||||
images_imported = 0
|
images_imported = 0
|
||||||
|
|
||||||
execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{original_gossamer_id}").each do |file|
|
execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{original_gossamer_id}").each do |file|
|
||||||
# encoded_filename = URI.encode(file['File_Name'])
|
|
||||||
|
# Encode the filename twice to match how it is stored on the server
|
||||||
double_encoded_filename = file['File_Name'].gsub('%', '%25')
|
double_encoded_filename = file['File_Name'].gsub('%', '%25')
|
||||||
encoded_filename = URI.encode_www_form_component(double_encoded_filename)
|
encoded_filename = URI.encode_www_form_component(double_encoded_filename)
|
||||||
|
|
||||||
|
# Construct the file URL
|
||||||
file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{encoded_filename}"
|
file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{encoded_filename}"
|
||||||
puts "User #{user.username} User ID: #{user.id} original_gossamer_id: #{original_gossamer_id} file_url: #{file_url}"
|
puts "User #{user.username} User ID: #{user.id} original_gossamer_id: #{original_gossamer_id} file_url: #{file_url}"
|
||||||
|
|
||||||
|
# Ensure the file is a user image and has a supported MIME type
|
||||||
next unless file['ForeignColName'] =~ /^user_image\d+$/
|
next unless file['ForeignColName'] =~ /^user_image\d+$/
|
||||||
puts "#A"
|
puts "#A"
|
||||||
next unless ['image/jpeg', 'image/png'].include?(file['File_MimeType'])
|
next unless ['image/jpeg', 'image/png', 'image/gif', 'image/tiff'].include?(file['File_MimeType'])
|
||||||
puts "#B"
|
puts "#B"
|
||||||
|
|
||||||
|
# Download the attachment
|
||||||
image_data = download_attachment(file_url)
|
image_data = download_attachment(file_url)
|
||||||
next if image_data.nil?
|
next if image_data.nil?
|
||||||
puts "#C"
|
puts "#C"
|
||||||
|
|
||||||
|
# Write the image data to a temporary file
|
||||||
temp_file = Tempfile.new(['user_image', File.extname(file['File_Name'])])
|
temp_file = Tempfile.new(['user_image', File.extname(file['File_Name'])])
|
||||||
temp_file.binmode
|
temp_file.binmode
|
||||||
temp_file.write(image_data)
|
temp_file.write(image_data)
|
||||||
temp_file.rewind
|
temp_file.rewind
|
||||||
|
|
||||||
|
# Convert TIFF to PNG if necessary
|
||||||
|
if file['File_MimeType'] == 'image/tiff'
|
||||||
|
temp_file.close
|
||||||
|
temp_file = File.open(convert_tiff_to_png(temp_file.path))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Resize the image for the avatar / profile picture (200x200)
|
||||||
|
resized_image_path = resize_image(temp_file.path, 200, 200)
|
||||||
|
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
|
||||||
if images_imported == 0
|
if images_imported == 0
|
||||||
puts "#D"
|
puts "#D"
|
||||||
|
|
||||||
|
# Upload the original image for profile header and user card background
|
||||||
upload = upload_attachment(user, temp_file, file['File_Name'], file_url)
|
upload = upload_attachment(user, temp_file, file['File_Name'], file_url)
|
||||||
next if upload.nil?
|
next if upload.nil?
|
||||||
|
|
||||||
user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: upload.id)
|
# Upload the resized image for the avatar
|
||||||
|
resized_upload = upload_attachment(user, resized_temp_file, file['File_Name'], file_url)
|
||||||
|
next if resized_upload.nil?
|
||||||
|
|
||||||
|
# Set the avatar usign the resized image
|
||||||
|
user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: resized_upload.id)
|
||||||
user.save!
|
user.save!
|
||||||
|
|
||||||
# Set the Profile Header
|
# Set the Profile Header
|
||||||
@ -433,13 +472,19 @@ class GossamerForumsImporter < ImportScripts::Base
|
|||||||
|
|
||||||
images_imported += 1
|
images_imported += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "#E"
|
puts "#E"
|
||||||
|
|
||||||
|
# Append the image to the user's bio
|
||||||
user.user_profile.bio_raw ||= ""
|
user.user_profile.bio_raw ||= ""
|
||||||
user.user_profile.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
|
user.user_profile.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
|
||||||
user.user_profile.save!
|
user.user_profile.save!
|
||||||
|
|
||||||
|
# Clean up temporary files
|
||||||
temp_file.close
|
temp_file.close
|
||||||
temp_file.unlink
|
temp_file.unlink
|
||||||
|
resized_temp_file.close
|
||||||
|
resized_temp_file.unlink
|
||||||
end
|
end
|
||||||
print "Importing files for user #{user.username}... Done.\n"
|
print "Importing files for user #{user.username}... Done.\n"
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user