Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +01:00
Client and server-side fixes
Dieser Commit ist enthalten in:
Ursprung
ee71719793
Commit
922afdc1bd
5 geänderte Dateien mit 37 neuen und 30 gelöschten Zeilen
|
@ -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,
|
||||
|
|
|
@ -31,16 +31,17 @@
|
|||
|
||||
<div class="controls">
|
||||
{{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}}
|
||||
<div class="import-message">
|
||||
{{importMessage}}
|
||||
</div>
|
||||
{{/if}}
|
||||
|
||||
{{d-button id="import-button"
|
||||
class="btn btn-primary side"
|
||||
label="admin.wizard.transfer.import.label"
|
||||
action=(action "import")}}
|
||||
</div>
|
||||
|
||||
{{#if hasLogs}}
|
||||
|
|
|
@ -500,6 +500,10 @@
|
|||
flex-direction: column;
|
||||
}
|
||||
|
||||
.import-message {
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.import-logs {
|
||||
margin-top: 20px;
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Laden …
In neuem Issue referenzieren