v0.24 Improve attachment handling for posts

This commit is contained in:
David Sainty 2024-07-01 21:07:58 +10:00
parent 101d7cc0cb
commit 779575c1b4

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.23 # v0.24
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -335,21 +335,38 @@ 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
# upload = Upload.create!(
# user_id: user_id,
# original_filename: filename,
# filesize: file.size,
# # filesize: File.size(file.path),
# # content_type: `file --brief --mime-type #{file.path}`.strip,
# # sha1: Digest::SHA1.file(file.path).hexdigest,
# # origin: 'user_avatar',
# # retain_hours: nil,
# url: gossamer_url
# )
# # 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
upload = Upload.create!( upload = Upload.create!(
user_id: user_id, user_id: user_id,
original_filename: filename, original_filename: filename,
filesize: file.size, filesize: file.size,
# filesize: File.size(file.path), content_type: content_type,
# content_type: `file --brief --mime-type #{file.path}`.strip, sha1: sha1,
# sha1: Digest::SHA1.file(file.path).hexdigest, origin: 'composer',
# origin: 'user_avatar', retain_hours: nil
# retain_hours: nil,
url: gossamer_url
) )
# Error -- non-existent method upload.ensure_consistency!
# Move the file to the correct location # Move the file to the correct location
# FileUtils.mv(file.path, upload.path) FileUtils.mv(file.path, upload.path)
upload.save! upload.save!
upload upload
rescue => e rescue => e
@ -376,7 +393,11 @@ class GossamerForumsImporter < ImportScripts::Base
upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'], attachment_url) upload = upload_attachment(user_id, temp_file, att_row['postatt_filename'], attachment_url)
next unless upload next unless upload
upload_url = upload.url # BAD
# upload_url = upload.url
# Get the URL of the uploaded file from Discourse
upload_url = Upload.get_from_url(upload.url).url
puts "Appending to post.raw... #{upload_url} MIME type: #{mime_type}" puts "Appending to post.raw... #{upload_url} MIME type: #{mime_type}"
if mime_type.start_with?('image/') if mime_type.start_with?('image/')