v0.36.1 Rewrite broken upload_attachment due to 3.2.5 handling things differently

This commit is contained in:
David Sainty 2024-08-14 21:55:56 +10:00
parent 2fe8b483f7
commit 05541d3ecb

View File

@ -1,7 +1,7 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Migration-Import Script
# v0.36
# v0.36.1 Rewrite broken upload_attachment due to 3.2.5 handling things differently
require 'mysql2'
require 'open-uri'
@ -380,12 +380,16 @@ class GossamerForumsImporter < ImportScripts::Base
# Generate SHA1 hash for the file
sha1 = Digest::SHA1.file(file.path).hexdigest
# Ensure the file size is correctly handled
file_size = File.size(file.path)
# upload = Upload.create!(
upload = Upload.new(
# Create the upload record, adjusting fields as needed for 3.2.5 compatibility
upload = Upload.create!(
# upload = Upload.new(
user_id: user_id,
original_filename: filename,
filesize: file.size,
filesize: file_size,
sha1: sha1,
url: gossamer_url
# content_type: content_type,
@ -393,9 +397,9 @@ class GossamerForumsImporter < ImportScripts::Base
# retain_hours: nil
)
# Use FileStore::LocalStore to store the file
store = FileStore::LocalStore.new
upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path)))
# # Use FileStore::LocalStore to store the file
# store = FileStore::LocalStore.new
# upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path)))
# # Use Discourse's internal method to upload the file
# file.rewind
@ -408,6 +412,19 @@ class GossamerForumsImporter < ImportScripts::Base
# Save the upload object
upload.save!
# Check if further processing or compatibility handling is needed
if Discourse.respond_to?(:store)
store = Discourse.store
upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path)))
else
# Fallback for earlier file store methods
upload_path = Upload.get_path_for_file(upload.sha1)
FileUtils.mkdir_p(File.dirname(upload_path))
FileUtils.mv(file.path, upload_path)
end
# Return the upload object
upload
rescue => e
puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}"
@ -419,7 +436,10 @@ class GossamerForumsImporter < ImportScripts::Base
def upload_attachment_two(user_id, file, filename)
begin
upload = UploadCreator.new(file, filename, origin: 'import').create_for(user_id)
# Raise an erorr if the upload fails to catch issues early
raise "Upload failed" unless upload
upload
rescue => e
puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}"