From 1600e91ba4e39a92762d56026d8d7517854c74c4 Mon Sep 17 00:00:00 2001 From: saint Date: Tue, 2 Jul 2024 13:15:07 +1000 Subject: [PATCH] v0.25 Post image handling tweaks --- gossamer_forums.rb | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 6e49aef..00dcf78 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.24 +# v0.25 require 'mysql2' require 'open-uri' @@ -351,21 +351,25 @@ class GossamerForumsImporter < ImportScripts::Base # # 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 +# content_type = `file --brief --mime-type #{file.path}`.strip +# sha1 = Digest::SHA1.file(file.path).hexdigest - upload = Upload.create!( +# upload = Upload.create!( + upload = Upload.new( user_id: user_id, original_filename: filename, filesize: file.size, - content_type: content_type, - sha1: sha1, - origin: 'composer', - retain_hours: nil + sha1: Digest::SHA1.file(file.path).hexdigest +# content_type: content_type, +# origin: 'composer', +# retain_hours: nil ) # Move the file to the correct location - FileUtils.mv(file.path, upload.path) + upload_path = Upload.get_path_for_file(upload.sha1) + FileUtils.mkdir_p(File.dirname(upload_path)) + FileUtils.mv(file.path, upload_path) +# FileUtils.mv(file.path, upload.path) upload.save! upload @@ -384,6 +388,7 @@ class GossamerForumsImporter < ImportScripts::Base next unless attachment_data mime_type = att_row['postatt_content'] + temp_file = Tempfile.new(['attachment', File.extname(att_row['postatt_filename'])]) temp_file.binmode temp_file.write(attachment_data) @@ -393,11 +398,9 @@ class GossamerForumsImporter < ImportScripts::Base upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'], attachment_url) next unless upload -# BAD -# upload_url = upload.url - # Get the URL of the uploaded file from Discourse - upload_url = Upload.get_from_url(upload.url).url +# upload_url = Upload.get_from_url(upload.url).url + upload_url = upload.url puts "Appending to post.raw... #{upload_url} MIME type: #{mime_type}" if mime_type.start_with?('image/')