From 1036d2898b8008ef43b37207b0f43926e360db70 Mon Sep 17 00:00:00 2001 From: saint Date: Wed, 3 Jul 2024 14:39:09 +1000 Subject: [PATCH] v0.26.3 Modify upload attachment code to try and make Discourse happy --- gossamer_forums.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index bc58951..0968098 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.26.2 +# v0.26.3 require 'mysql2' require 'open-uri' @@ -397,6 +397,19 @@ class GossamerForumsImporter < ImportScripts::Base 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 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| @@ -407,13 +420,14 @@ class GossamerForumsImporter < ImportScripts::Base 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.binmode temp_file.write(attachment_data) temp_file.rewind 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 # Get the URL of the uploaded file from Discourse