0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 11:00:28 +01: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

@ -19,15 +19,10 @@ 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
@ -46,22 +41,25 @@ class CustomWizard::TransferController < ::ApplicationController
return return
end end
unless is_json file begin
jsonObject = JSON.parse file
rescue JSON::ParserError
render json: { error: I18n.t('wizard.import.error.invalid_json') } 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