discourse/migrations/lib/uploader/site_settings.rb
Selase Krakani 6c91148db8
DEV: Refactor uploads_importer script (#29292)
* DEV: Implement uploads command entrypoint

- Setup Thor UploadsCommand for CLI
- First pass at modularizing various parts of the exising `uploads_import` script

* DEV: First attempt at modularizing missing uploads fixer task

Move missing upload fix to a dedicated uploads task implementation unit

* DEV: First attempt at modularizing missing uploads uploader task

Move uploader to a dedicated uploads task implementation unit

* DEV: First attempt at modularizing missing uploads optimizer task

Move optimizer to a dedicated uploads task implementation unit

* DEV: Various follow up fixes to get optimization working

- Start threads early
- Improve "log" message formatting
- Add missing `copy_to_tempfile` method on "uploader" task

* DEV: Refactor a bit more

Deduplicate and move most of threading premitives to base task as-is

* DEV: Remove redundant condition in uploads db migration

* DEV: More deduplication

Move task retry logic to base class and tidy up other implementation
details carried over from the existing script
2024-10-31 13:31:12 +00:00

81 lines
2.3 KiB
Ruby

# frozen_string_literal: true
module Migrations::Uploader
class SiteSettings
class S3UploadsConfigurationError < StandardError
end
def initialize(options)
@options = options
end
def configure!
configure_basic_uploads
configure_multisite if @options[:multisite]
configure_s3 if @options[:enable_s3_uploads]
end
def self.configure!(options)
new(options).configure!
end
private
def configure_basic_uploads
SiteSetting.clean_up_uploads = false
SiteSetting.authorized_extensions = @options[:authorized_extensions]
SiteSetting.max_attachment_size_kb = @options[:max_attachment_size_kb]
SiteSetting.max_image_size_kb = @options[:max_image_size_kb]
end
def configure_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 = @options[:multisite_db_name]
end
def configure_s3
SiteSetting.s3_access_key_id = @options[:s3_access_key_id]
SiteSetting.s3_secret_access_key = @options[:s3_secret_access_key]
SiteSetting.s3_upload_bucket = @options[:s3_upload_bucket]
SiteSetting.s3_region = @options[:s3_region]
SiteSetting.s3_cdn_url = @options[:s3_cdn_url]
SiteSetting.enable_s3_uploads = true
if SiteSetting.enable_s3_uploads != true
raise S3UploadsConfigurationError, "Failed to enable S3 uploads"
end
verify_s3_uploads_configuration!
end
def verify_s3_uploads_configuration!
Tempfile.open("discourse-s3-test") do |tmpfile|
tmpfile.write("test")
tmpfile.rewind
upload =
UploadCreator.new(tmpfile, "discourse-s3-test.txt").create_for(Discourse::SYSTEM_USER_ID)
unless upload.present? && upload.persisted? && upload.errors.blank? &&
upload.url.start_with?("//")
raise S3UploadsConfigurationError, "Failed to upload to S3"
end
upload.destroy
end
end
end
end