0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 01:40:29 +01:00
discourse-custom-wizard/controllers/custom_wizard/transfer.rb

75 Zeilen
1,8 KiB
Ruby

class CustomWizard::TransferController < ::ApplicationController
before_action :ensure_logged_in
before_action :ensure_admin
skip_before_action :check_xhr, :only => [:export]
def index
end
def export
wizards = params['wizards']
wizard_objects = []
if wizards.nil?
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.export.error.select_one') }
return
end
wizards.each do |w|
wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_')))
end
2019-08-07 12:16:05 +02:00
send_data wizard_objects.to_json,
type: "application/json",
disposition: 'attachment',
filename: 'wizards.json'
end
def import
file = File.read(params['file'].tempfile)
if file.nil?
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.import.error.no_file') }
return
end
fileSize = file.size
maxFileSize = 512 * 1024
if maxFileSize < fileSize
2019-08-07 12:16:05 +02:00
render json: { error: I18n.t('wizard.import.error.file_large') }
return
end
2019-08-07 12:16:05 +02:00
begin
jsonObject = JSON.parse file
rescue JSON::ParserError
render json: { error: I18n.t('wizard.import.error.invalid_json') }
return
end
2019-08-07 12:16:05 +02:00
countValid = 0
success_ids = []
failed_ids = []
2019-08-07 12:16:05 +02:00
jsonObject.each do |o|
2019-08-07 12:16:05 +02:00
if !CustomWizard::Template.new(o)
failed_ids.push o['id']
next
end
countValid += 1
pluginStoreEntry = PluginStore.new 'custom_wizard'
saved = pluginStoreEntry.set(o['id'], o) unless pluginStoreEntry.get(o['id'])
success_ids.push o['id'] if !!saved
failed_ids.push o['id'] if !saved
end
if countValid == 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