v0.26.3 Modify upload attachment code to try and make Discourse happy

This commit is contained in:
David Sainty 2024-07-03 14:39:09 +10:00
parent 017f99c88b
commit 1036d2898b

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.26.2 # v0.26.3
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -397,6 +397,19 @@ class GossamerForumsImporter < ImportScripts::Base
end end
end end
# Helper method to upload an attachment / image to Discourse
def upload_attachment_two(user_id, file, filename)
begin
upload = UploadCreator.new(file, filename, origin: 'import').create_for(user_id)
raise "Upload failed" unless upload
upload
rescue => e
puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}"
nil
end
end
# Helper method to handle post attachments # Helper method to handle post attachments
def handle_post_attachments(gossamer_post_id, post, user_id) def handle_post_attachments(gossamer_post_id, post, user_id)
execute_query("SELECT * FROM gforum_PostAttachment WHERE post_id_fk = #{gossamer_post_id}").each do |att_row| execute_query("SELECT * FROM gforum_PostAttachment WHERE post_id_fk = #{gossamer_post_id}").each do |att_row|
@ -407,13 +420,14 @@ class GossamerForumsImporter < ImportScripts::Base
mime_type = att_row['postatt_content'] mime_type = att_row['postatt_content']
# Create a Tempfile to pass to the UploadCreator
temp_file = Tempfile.new(['attachment', File.extname(att_row['postatt_filename'])]) temp_file = Tempfile.new(['attachment', File.extname(att_row['postatt_filename'])])
temp_file.binmode temp_file.binmode
temp_file.write(attachment_data) temp_file.write(attachment_data)
temp_file.rewind temp_file.rewind
puts "Attempting upload..." puts "Attempting upload..."
upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'], attachment_url) upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'])
next unless upload next unless upload
# Get the URL of the uploaded file from Discourse # Get the URL of the uploaded file from Discourse