0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-20 07:41:11 +02:00
discourse-custom-wizard/controllers/custom_wizard/admin/transfer.rb

69 Zeilen
1,6 KiB
Ruby

2020-11-03 01:24:20 +01:00
class CustomWizard::AdminTransferController < CustomWizard::AdminController
skip_before_action :check_xhr, :only => [:export]
def export
2020-11-03 01:24:20 +01:00
wizard_ids = params['wizards']
templates = []
2020-11-03 01:24:20 +01:00
if wizard_ids.nil?
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.export.error.select_one') }
return
end
2020-11-03 01:24:20 +01:00
wizard_ids.each do |wizard_id|
if template = CustomWizard::Template.find(wizard_id)
templates.push(template)
end
end
2020-11-03 01:24:20 +01:00
send_data templates.to_json,
2019-08-07 12:16:05 +02:00
type: "application/json",
disposition: 'attachment',
filename: 'wizards.json'
end
def import
file = File.read(params['file'].tempfile)
2020-11-03 01:24:20 +01:00
if file.nil?
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.import.error.no_file') }
return
end
2020-11-03 01:24:20 +01:00
file_size = file.size
max_file_size = 512 * 1024
if max_file_size < file_size
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.import.error.file_large') }
return
end
2020-11-03 01:24:20 +01:00
2019-08-07 12:16:05 +02:00
begin
2020-11-03 01:24:20 +01:00
template_json = JSON.parse file
2019-08-07 12:16:05 +02:00
rescue JSON::ParserError
render json: { error: I18n.t('wizard.import.error.invalid_json') }
return
end
2019-08-07 12:16:05 +02:00
success_ids = []
failed_ids = []
2019-08-07 12:16:05 +02:00
2020-11-03 01:24:20 +01:00
template_json.each do |t_json|
template = CustomWizard::Template.new(t_json)
template.save(skip_jobs: true)
if template.errors.any?
failed_ids.push t_json['id']
else
success_ids.push t_json['id']
2019-08-07 12:16:05 +02:00
end
end
2020-11-03 01:24:20 +01:00
if success_ids.length == 0
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.import.error.no_valid_wizards') }
else
2019-08-07 12:16:05 +02:00
render json: { success: success_ids, failed: failed_ids }
end
end
end