v0.36.2 Rewrite broken upload_attachment due to 3.2.5 handling things differently
This commit is contained in:
parent
05541d3ecb
commit
f2b459b22c
@ -1,7 +1,7 @@
|
||||
# Federated Computer, Inc.
|
||||
# David Sainty <saint@federated.computer> 2024 A.D.
|
||||
# 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 'open-uri'
|
||||
@ -359,7 +359,7 @@ class GossamerForumsImporter < ImportScripts::Base
|
||||
|
||||
# Helper method to upload an attachment / image to Discourse
|
||||
def upload_attachment(user_id, file, filename, gossamer_url)
|
||||
begin
|
||||
# begin
|
||||
|
||||
# BAD
|
||||
# upload = Upload.create!(
|
||||
@ -381,15 +381,15 @@ 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)
|
||||
# # Ensure the file size is correctly handled
|
||||
# file_size = File.size(file.path)
|
||||
|
||||
# Create the upload record, adjusting fields as needed for 3.2.5 compatibility
|
||||
upload = Upload.create!(
|
||||
# upload = Upload.new(
|
||||
# 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,
|
||||
@ -397,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
|
||||
@ -413,23 +413,23 @@ 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
|
||||
# # 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}"
|
||||
nil
|
||||
end
|
||||
# rescue => e
|
||||
# puts "Failed to upload attachment #{filename} for user_id #{user_id}: #{e.message}"
|
||||
# nil
|
||||
# end
|
||||
end
|
||||
|
||||
# Helper method to upload an attachment / image to Discourse
|
||||
|
Loading…
Reference in New Issue
Block a user