Skip to content

Commit

Permalink
DEV: Improve uploads_importer script (discourse#25971)
Browse files Browse the repository at this point in the history
* Print instructions when the `sqlite3` gem can't be loaded
* Use `display_filename` instead of `filename` if available
* Support uploading for a multisite
  • Loading branch information
gschlager authored Mar 5, 2024
1 parent ccf0b61 commit 38ff1a3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
54 changes: 47 additions & 7 deletions script/bulk_import/uploads_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
require_relative "../../config/environment"

require "etc"
require "sqlite3"
require "colored2"

begin
require "sqlite3"
rescue LoadError
STDERR.puts "",
"ERROR: Failed to load required gems.",
"",
"You need to enable the `generic_import` group in your Gemfile.",
"Execute the following command to do so:",
"",
"\tbundle config set --local with generic_import && bundle install",
""
exit 1
end

module BulkImport
class UploadsImporter
TRANSACTION_SIZE = 1000
Expand Down Expand Up @@ -183,17 +196,19 @@ def upload_files
upload =
copy_to_tempfile(path) do |file|
begin
UploadCreator.new(file, row["filename"], type: row["type"]).create_for(
Discourse::SYSTEM_USER_ID,
)
UploadCreator.new(
file,
row["display_filename"] || row["filename"],
type: row["type"],
).create_for(Discourse::SYSTEM_USER_ID)
rescue StandardError => e
error_message = e.message
nil
end
end

if (upload_okay = upload.present? && upload.persisted? && upload.errors.blank?)
upload_path = store.get_path_for_upload(upload)
upload_path = add_multisite_prefix(store.get_path_for_upload(upload))

file_exists =
if store.external?
Expand Down Expand Up @@ -313,7 +328,7 @@ def fix_missing
begin
upload = JSON.parse(row["upload"])
fake_upload.url = upload["url"]
path = store.get_path_for_upload(fake_upload)
path = add_multisite_prefix(store.get_path_for_upload(fake_upload))

file_exists =
if store.external?
Expand Down Expand Up @@ -504,7 +519,8 @@ def create_optimized_images
if optimized_images.present?
optimized_images.map! do |optimized_image|
next unless optimized_image.present?
optimized_image_path = store.get_path_for_optimized_image(optimized_image)
optimized_image_path =
add_multisite_prefix(store.get_path_for_optimized_image(optimized_image))

file_exists =
if store.external?
Expand Down Expand Up @@ -627,6 +643,22 @@ def configure_site_settings
SiteSetting.max_attachment_size_kb = settings[:max_attachment_size_kb]
SiteSetting.max_image_size_kb = settings[:max_image_size_kb]

if settings[:multisite]
# rubocop:disable Discourse/NoDirectMultisiteManipulation
Rails.configuration.multisite = true
# rubocop:enable Discourse/NoDirectMultisiteManipulation

RailsMultisite::ConnectionManagement.class_eval do
def self.current_db_override=(value)
@current_db_override = value
end
def self.current_db
@current_db_override
end
end
RailsMultisite::ConnectionManagement.current_db_override = settings[:multisite_db_name]
end

if settings[:enable_s3_uploads]
SiteSetting.s3_access_key_id = settings[:s3_access_key_id]
SiteSetting.s3_secret_access_key = settings[:s3_secret_access_key]
Expand Down Expand Up @@ -655,6 +687,14 @@ def configure_site_settings
end
end
end

def add_multisite_prefix(path)
if Rails.configuration.multisite
File.join("uploads", RailsMultisite::ConnectionManagement.current_db, path)
else
path
end
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions script/bulk_import/uploads_importer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ site_settings:
s3_secret_access_key: "your-secret-access-key"
s3_cdn_url: "https://your-cdn-url.com"

# Set this to true if the site is a multisite and configure the `multisite_db_name` accordingly
multisite: false
multisite_db_name: "default"

# Sometimes a file can be found at one of many locations. Here's a list of transformations that can
# be applied to the path to try and find the file. The first transformation that results in a file
# being found will be used.
Expand Down

0 comments on commit 38ff1a3

Please sign in to comment.