From 779575c1b4ffd60644a8dc2f30b1eaa1b8a37499 Mon Sep 17 00:00:00 2001 From: saint Date: Mon, 1 Jul 2024 21:07:58 +1000 Subject: [PATCH] v0.24 Improve attachment handling for posts --- gossamer_forums.rb | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 694f70a..6e49aef 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.23 +# v0.24 require 'mysql2' require 'open-uri' @@ -335,21 +335,38 @@ class GossamerForumsImporter < ImportScripts::Base # Helper method to upload an attachment / image to Discourse def upload_attachment(user_id, file, filename, gossamer_url) begin + +# BAD +# upload = Upload.create!( +# user_id: user_id, +# original_filename: filename, +# filesize: file.size, +# # filesize: File.size(file.path), +# # content_type: `file --brief --mime-type #{file.path}`.strip, +# # sha1: Digest::SHA1.file(file.path).hexdigest, +# # origin: 'user_avatar', +# # retain_hours: nil, +# url: gossamer_url +# ) +# # Error -- non-existent method upload.ensure_consistency! + + # Use the correct content type and SHA1 hash for the upload + content_type = `file --brief --mime-type #{file.path}`.strip + sha1 = Digest::SHA1.file(file.path).hexdigest + upload = Upload.create!( user_id: user_id, original_filename: filename, filesize: file.size, -# filesize: File.size(file.path), -# content_type: `file --brief --mime-type #{file.path}`.strip, -# sha1: Digest::SHA1.file(file.path).hexdigest, -# origin: 'user_avatar', -# retain_hours: nil, - url: gossamer_url + content_type: content_type, + sha1: sha1, + origin: 'composer', + retain_hours: nil ) -# Error -- non-existent method upload.ensure_consistency! - + # Move the file to the correct location -# FileUtils.mv(file.path, upload.path) + FileUtils.mv(file.path, upload.path) + upload.save! upload rescue => e @@ -376,7 +393,11 @@ class GossamerForumsImporter < ImportScripts::Base upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'], attachment_url) next unless upload - upload_url = upload.url +# BAD +# upload_url = upload.url + + # Get the URL of the uploaded file from Discourse + upload_url = Upload.get_from_url(upload.url).url puts "Appending to post.raw... #{upload_url} MIME type: #{mime_type}" if mime_type.start_with?('image/')