1
0
Fork 0

added some validations and changed the import logic to use FormData instead of FileReader

Dieser Commit ist enthalten in:
Faizaan Gagan 2019-07-29 13:30:56 +05:30
Ursprung 63fb0658e9
Commit 98d8eeffca
2 geänderte Dateien mit 74 neuen und 49 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@ export default Ember.Controller.extend({
this._super(); this._super();
this.set('selected', new Set()); this.set('selected', new Set());
this.set('filePath', []); this.set('filePath', []);
// this.setProperties({selected:[]})
}, },
@ -14,10 +14,8 @@ export default Ember.Controller.extend({
actions: { actions: {
checkChanged(event) { checkChanged(event) {
// return true;
// console.log(event.target.checked)
let selected = this.get('selected') let selected = this.get('selected');
if (event.target.checked) { if (event.target.checked) {
@ -28,71 +26,68 @@ export default Ember.Controller.extend({
} else if (!event.target.checked) { } else if (!event.target.checked) {
selected.delete(event.target.id) selected.delete(event.target.id)
} }
console.log(selected) console.log(selected);
this.set('selected', selected) this.set('selected', selected)
// console.log(this.get('selected'))
}, },
export() { export() {
let wizards = this.get('selected') let wizards = this.get('selected');
let url = Discourse.BaseUrl let url = Discourse.BaseUrl;
let route = '/admin/wizards/transfer/export' let route = '/admin/wizards/transfer/export';
url += route + '?' url += route + '?';
wizards.forEach((wizard) => { wizards.forEach((wizard) => {
let step = 'wizards[]=' + wizard; let step = 'wizards[]=' + wizard;
step += '&' step += '&';
url += step url += step
}) });
location.href = url; location.href = url;
console.log(url) console.log(url)
// return ajax('/admin/wizards/transfer/export', {
// type: "POST",
// data: {
// wizards: wizards
// }
//
// })
}, },
setFilePath(event) { setFilePath(event) {
console.log(event.target.files[0]) console.log(event.target.files[0]);
// 512 kb is the max file size
let maxFileSize = 512 * 1024;
if (maxFileSize < event.target.files[0].size) {
this.set('fileError', 'The file size is too big')
} else {
this.set('filePath', event.target.files[0]) this.set('filePath', event.target.files[0])
} }
}
, ,
import() { import() {
let fileReader = new FileReader();
fileReader.onload = function () {
let upload = {'fileJson': fileReader.result};
// ajax('admin/wizard/transfer/import');
console.log(fileReader.result)
//ajax call
ajax('/admin/wizards/transfer/import',{ let $formData = new FormData();
type: 'POST' , $formData.append('file', this.get('filePath'));
data:upload , console.log($formData);
}).then(result=>{ ajax('/admin/wizards/transfer/import', {
if(result.error){ type: 'POST',
console.log(result.error) data: $formData,
processData: false,
contentType: false,
}else{ }).then(result => {
alert('wizards imported successfully') if (result.error) {
alert(result.error)
} else {
alert(result.success)
} }
}) })
}
fileReader.readAsText(this.get('filePath'))
} }

Datei anzeigen

@ -17,25 +17,55 @@ class CustomWizard::TransferController < ::ApplicationController
|w| |w|
# p w # p w
wizard_objects.push(PluginStore.get('custom_wizard',w.tr('-','_'))) wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_')))
end end
puts 'wizard_objects' puts 'wizard_objects'
p wizard_objects p wizard_objects
send_data wizard_objects.to_json ,type: "application/json", disposition:'attachment' ,filename: 'wizards.json' send_data wizard_objects.to_json, type: "application/json", disposition: 'attachment', filename: 'wizards.json'
end end
def is_json(string)
begin
!!JSON.parse(string)
rescue
false
end
end
def import def import
json = params['fileJson'] file = File.read(params['file'].tempfile)
jsonObject = JSON.parse json fileSize = file.size
puts 'json file' maxFileSize = 512 * 1024
# p jsonObject if maxFileSize < fileSize
render json: {error: "File too large"}
end
unless is_json file
render json: {error: "File is not a valid json file"}
end
jsonObject = JSON.parse file
countValid = 0
jsonObject.each do |o| jsonObject.each do |o|
# validate whether the given json is a valid "wizard"
next unless CustomWizard::Template.new(o)
countValid += 1
puts 'json entity' puts 'json entity'
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 #plugin store detects the json object type and sets proper `type_name` for the entry
pluginStoreEntry.set(o['id'],o) pluginStoreEntry.set(o['id'], o)
end
if countValid == 0
render json: {error: "File doesn't contain any valid wizards"}
else
render json: {success: "Wizards imported successfully"}
end end
end end
# admin/wizards/transfer/import # admin/wizards/transfer/import