From 922afdc1bde5aaa032594d13ecff731dfcbe5869 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 7 Aug 2019 20:16:05 +1000 Subject: [PATCH] Client and server-side fixes --- .../controllers/admin-wizards-transfer.js.es6 | 8 +++- .../templates/admin-wizards-transfer.hbs | 9 ++-- assets/stylesheets/wizard_custom_admin.scss | 4 ++ config/locales/client.en.yml | 2 +- controllers/transfer.rb | 44 +++++++++---------- 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 index a9b1be3a..3afb4cc9 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-transfer.js.es6 @@ -79,7 +79,11 @@ export default Ember.Controller.extend({ } 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 { this.set('filePath', event.target.files[0]); } @@ -99,7 +103,7 @@ export default Ember.Controller.extend({ contentType: false, }).then(result => { if (result.error) { - this.set('error', result.error); + this.set('importMessage', result.error); } else { this.setProperties({ successIds: result.success, diff --git a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs index 12de208f..b326fd4d 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-transfer.hbs @@ -31,16 +31,17 @@
{{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}}
{{importMessage}}
{{/if}} + + {{d-button id="import-button" + class="btn btn-primary side" + label="admin.wizard.transfer.import.label" + action=(action "import")}}
{{#if hasLogs}} diff --git a/assets/stylesheets/wizard_custom_admin.scss b/assets/stylesheets/wizard_custom_admin.scss index e4a4b915..7ae598ac 100644 --- a/assets/stylesheets/wizard_custom_admin.scss +++ b/assets/stylesheets/wizard_custom_admin.scss @@ -500,6 +500,10 @@ flex-direction: column; } +.import-message { + margin: 10px 0; +} + .import-logs { margin-top: 20px; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 3c07c78a..d65c42c0 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -226,7 +226,7 @@ en: success: 'Wizard "{{id}}" saved successfully' failure: 'Wizard "{{id}}" could not be saved' 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: location: diff --git a/controllers/transfer.rb b/controllers/transfer.rb index bcf95299..4da52bb9 100644 --- a/controllers/transfer.rb +++ b/controllers/transfer.rb @@ -11,7 +11,7 @@ class CustomWizard::TransferController < ::ApplicationController wizard_objects = [] if wizards.nil? - render json: {error: I18n.t('wizard.export.error.select_one')} + render json: { error: I18n.t('wizard.export.error.select_one') } return end @@ -19,22 +19,17 @@ class CustomWizard::TransferController < ::ApplicationController wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_'))) end - send_data wizard_objects.to_json, type: "application/json", disposition: 'attachment', filename: 'wizards.json' - end - - def is_json(string) - begin - !!JSON.parse(string) - rescue - false - end + 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? - render json: {error: I18n.t('wizard.import.error.no_file')} + render json: { error: I18n.t('wizard.import.error.no_file') } return end @@ -42,35 +37,38 @@ class CustomWizard::TransferController < ::ApplicationController maxFileSize = 512 * 1024 if maxFileSize < fileSize - render json: {error: I18n.t('wizard.import.error.file_large')} + render json: { error: I18n.t('wizard.import.error.file_large') } return end - - unless is_json file - render json: {error: I18n.t('wizard.import.error.invalid_json')} + + begin + jsonObject = JSON.parse file + rescue JSON::ParserError + render json: { error: I18n.t('wizard.import.error.invalid_json') } return end - - jsonObject = JSON.parse file + countValid = 0 success_ids = [] failed_ids = [] + jsonObject.each do |o| - # validate whether the given json is a valid "wizard" - next unless CustomWizard::Template.new(o) + if !CustomWizard::Template.new(o) + failed_ids.push o['id'] + next + end + countValid += 1 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']) success_ids.push o['id'] if !!saved failed_ids.push o['id'] if !saved end 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 - render json: {success: success_ids, failed: failed_ids} + render json: { success: success_ids, failed: failed_ids } end end end