0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-19 23:31:11 +02:00

Client and server-side fixes

Dieser Commit ist enthalten in:
Angus McLeod 2019-08-07 20:16:05 +10:00
Ursprung ee71719793
Commit 922afdc1bd
5 geänderte Dateien mit 37 neuen und 30 gelöschten Zeilen

Datei anzeigen

@ -79,7 +79,11 @@ export default Ember.Controller.extend({
} }
if (maxFileSize < event.target.files[0].size) { if (maxFileSize < event.target.files[0].size) {
this.set('importMessage', I18n.t('admin.wizard.transfer.import.file_size_error')); this.setProperties({
importMessage: I18n.t('admin.wizard.transfer.import.file_size_error'),
filePath: null
});
$('#file-url').val('');
} else { } else {
this.set('filePath', event.target.files[0]); this.set('filePath', event.target.files[0]);
} }
@ -99,7 +103,7 @@ export default Ember.Controller.extend({
contentType: false, contentType: false,
}).then(result => { }).then(result => {
if (result.error) { if (result.error) {
this.set('error', result.error); this.set('importMessage', result.error);
} else { } else {
this.setProperties({ this.setProperties({
successIds: result.success, successIds: result.success,

Datei anzeigen

@ -31,16 +31,17 @@
<div class="controls"> <div class="controls">
{{input id='file-url' type="file" change=(action "setFilePath")}} {{input id='file-url' type="file" change=(action "setFilePath")}}
{{d-button id="import-button"
class="btn btn-primary side"
label="admin.wizard.transfer.import.label"
action=(action "import")}}
{{#if importMessage}} {{#if importMessage}}
<div class="import-message"> <div class="import-message">
{{importMessage}} {{importMessage}}
</div> </div>
{{/if}} {{/if}}
{{d-button id="import-button"
class="btn btn-primary side"
label="admin.wizard.transfer.import.label"
action=(action "import")}}
</div> </div>
{{#if hasLogs}} {{#if hasLogs}}

Datei anzeigen

@ -500,6 +500,10 @@
flex-direction: column; flex-direction: column;
} }
.import-message {
margin: 10px 0;
}
.import-logs { .import-logs {
margin-top: 20px; margin-top: 20px;

Datei anzeigen

@ -226,7 +226,7 @@ en:
success: 'Wizard "{{id}}" saved successfully' success: 'Wizard "{{id}}" saved successfully'
failure: 'Wizard "{{id}}" could not be saved' failure: 'Wizard "{{id}}" could not be saved'
no_file: "Please choose a file to import" no_file: "Please choose a file to import"
file_size_error: "The file size is too big" file_size_error: "The file must be JSON and 512kb or less"
wizard_js: wizard_js:
location: location:

Datei anzeigen

@ -11,7 +11,7 @@ class CustomWizard::TransferController < ::ApplicationController
wizard_objects = [] wizard_objects = []
if wizards.nil? if wizards.nil?
render json: {error: I18n.t('wizard.export.error.select_one')} render json: { error: I18n.t('wizard.export.error.select_one') }
return return
end end
@ -19,22 +19,17 @@ class CustomWizard::TransferController < ::ApplicationController
wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_'))) wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_')))
end end
send_data wizard_objects.to_json, type: "application/json", disposition: 'attachment', filename: 'wizards.json' send_data wizard_objects.to_json,
end type: "application/json",
disposition: 'attachment',
def is_json(string) filename: 'wizards.json'
begin
!!JSON.parse(string)
rescue
false
end
end end
def import def import
file = File.read(params['file'].tempfile) file = File.read(params['file'].tempfile)
if file.nil? if file.nil?
render json: {error: I18n.t('wizard.import.error.no_file')} render json: { error: I18n.t('wizard.import.error.no_file') }
return return
end end
@ -42,35 +37,38 @@ class CustomWizard::TransferController < ::ApplicationController
maxFileSize = 512 * 1024 maxFileSize = 512 * 1024
if maxFileSize < fileSize if maxFileSize < fileSize
render json: {error: I18n.t('wizard.import.error.file_large')} render json: { error: I18n.t('wizard.import.error.file_large') }
return return
end end
unless is_json file begin
render json: {error: I18n.t('wizard.import.error.invalid_json')} jsonObject = JSON.parse file
rescue JSON::ParserError
render json: { error: I18n.t('wizard.import.error.invalid_json') }
return return
end end
jsonObject = JSON.parse file
countValid = 0 countValid = 0
success_ids = [] success_ids = []
failed_ids = [] failed_ids = []
jsonObject.each do |o| jsonObject.each do |o|
# validate whether the given json is a valid "wizard" if !CustomWizard::Template.new(o)
next unless CustomWizard::Template.new(o) failed_ids.push o['id']
next
end
countValid += 1 countValid += 1
pluginStoreEntry = PluginStore.new 'custom_wizard' pluginStoreEntry = PluginStore.new 'custom_wizard'
#plugin store detects the json object type and sets proper `type_name` for the entry
# this condition helps us avoid updating an existing wizard instead of adding a new one
saved = pluginStoreEntry.set(o['id'], o) unless pluginStoreEntry.get(o['id']) saved = pluginStoreEntry.set(o['id'], o) unless pluginStoreEntry.get(o['id'])
success_ids.push o['id'] if !!saved success_ids.push o['id'] if !!saved
failed_ids.push o['id'] if !saved failed_ids.push o['id'] if !saved
end end
if countValid == 0 if countValid == 0
render json: {error: I18n.t('wizard.import.error.no_valid_wizards')} render json: { error: I18n.t('wizard.import.error.no_valid_wizards') }
else else
render json: {success: success_ids, failed: failed_ids} render json: { success: success_ids, failed: failed_ids }
end end
end end
end end