v0.36.1 Rewrite broken upload_attachment due to 3.2.5 handling things differently
This commit is contained in:
		@@ -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
 | 
					# v0.36.1 Rewrite broken upload_attachment due to 3.2.5 handling things differently
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require 'mysql2'
 | 
					require 'mysql2'
 | 
				
			||||||
require 'open-uri'
 | 
					require 'open-uri'
 | 
				
			||||||
@@ -381,11 +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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#      upload = Upload.create!(
 | 
					      # Ensure the file size is correctly handled
 | 
				
			||||||
      upload = Upload.new(
 | 
					      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(
 | 
				
			||||||
        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,
 | 
				
			||||||
@@ -393,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
 | 
				
			||||||
@@ -408,6 +412,19 @@ 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
 | 
				
			||||||
 | 
					      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
 | 
					      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}"
 | 
				
			||||||
@@ -419,7 +436,10 @@ class GossamerForumsImporter < ImportScripts::Base
 | 
				
			|||||||
  def upload_attachment_two(user_id, file, filename)
 | 
					  def upload_attachment_two(user_id, file, filename)
 | 
				
			||||||
    begin
 | 
					    begin
 | 
				
			||||||
      upload = UploadCreator.new(file, filename, origin: 'import').create_for(user_id)
 | 
					      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
 | 
					      raise "Upload failed" unless upload
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      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}"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user