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

This commit is contained in:
David Sainty 2024-08-14 22:13:31 +10:00
parent 05541d3ecb
commit f2b459b22c

View File

@ -1,7 +1,7 @@
# Federated Computer, Inc. # Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D. # David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Migration-Import Script # Gossamer Threads to Discourse -- Migration-Import Script
# v0.36.1 Rewrite broken upload_attachment due to 3.2.5 handling things differently # v0.36.2 Rewrite broken upload_attachment due to 3.2.5 handling things differently
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -359,7 +359,7 @@ class GossamerForumsImporter < ImportScripts::Base
# Helper method to upload an attachment / image to Discourse # Helper method to upload an attachment / image to Discourse
def upload_attachment(user_id, file, filename, gossamer_url) def upload_attachment(user_id, file, filename, gossamer_url)
begin # begin
# BAD # BAD
# upload = Upload.create!( # upload = Upload.create!(
@ -381,15 +381,15 @@ class GossamerForumsImporter < ImportScripts::Base
# Generate SHA1 hash for the file # Generate SHA1 hash for the file
sha1 = Digest::SHA1.file(file.path).hexdigest sha1 = Digest::SHA1.file(file.path).hexdigest
# Ensure the file size is correctly handled # # Ensure the file size is correctly handled
file_size = File.size(file.path) # file_size = File.size(file.path)
# Create the upload record, adjusting fields as needed for 3.2.5 compatibility # Create the upload record, adjusting fields as needed for 3.2.5 compatibility
upload = Upload.create!( # upload = Upload.create!(
# upload = Upload.new( upload = Upload.new(
user_id: user_id, user_id: user_id,
original_filename: filename, original_filename: filename,
filesize: file_size, filesize: file.size,
sha1: sha1, sha1: sha1,
url: gossamer_url url: gossamer_url
# content_type: content_type, # content_type: content_type,
@ -397,9 +397,9 @@ class GossamerForumsImporter < ImportScripts::Base
# retain_hours: nil # retain_hours: nil
) )
# # Use FileStore::LocalStore to store the file # Use FileStore::LocalStore to store the file
# store = FileStore::LocalStore.new store = FileStore::LocalStore.new
# upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path))) 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 # # Use Discourse's internal method to upload the file
# file.rewind # file.rewind
@ -413,23 +413,23 @@ class GossamerForumsImporter < ImportScripts::Base
# Save the upload object # Save the upload object
upload.save! upload.save!
# Check if further processing or compatibility handling is needed # # Check if further processing or compatibility handling is needed
if Discourse.respond_to?(:store) # if Discourse.respond_to?(:store)
store = Discourse.store # store = Discourse.store
upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path))) # upload.url = store.store_file(file, store.get_path_for('original', upload.id, upload.sha1, File.extname(file.path)))
else # else
# Fallback for earlier file store methods # # Fallback for earlier file store methods
upload_path = Upload.get_path_for_file(upload.sha1) # upload_path = Upload.get_path_for_file(upload.sha1)
FileUtils.mkdir_p(File.dirname(upload_path)) # FileUtils.mkdir_p(File.dirname(upload_path))
FileUtils.mv(file.path, upload_path) # FileUtils.mv(file.path, upload_path)
end # end
# Return the upload object # Return the upload object
upload upload
rescue => e # rescue => e
puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}" # puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}"
nil # nil
end # end
end end
# Helper method to upload an attachment / image to Discourse # Helper method to upload an attachment / image to Discourse