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.set('selected', new Set());
this.set('filePath', []);
// this.setProperties({selected:[]})
},
@ -14,10 +14,8 @@ export default Ember.Controller.extend({
actions: {
checkChanged(event) {
// return true;
// console.log(event.target.checked)
let selected = this.get('selected')
let selected = this.get('selected');
if (event.target.checked) {
@ -28,71 +26,68 @@ export default Ember.Controller.extend({
} else if (!event.target.checked) {
selected.delete(event.target.id)
}
console.log(selected)
console.log(selected);
this.set('selected', selected)
// console.log(this.get('selected'))
},
export() {
let wizards = this.get('selected')
let url = Discourse.BaseUrl
let route = '/admin/wizards/transfer/export'
url += route + '?'
let wizards = this.get('selected');
let url = Discourse.BaseUrl;
let route = '/admin/wizards/transfer/export';
url += route + '?';
wizards.forEach((wizard) => {
let step = 'wizards[]=' + wizard;
step += '&'
step += '&';
url += step
})
});
location.href = url;
console.log(url)
// return ajax('/admin/wizards/transfer/export', {
// type: "POST",
// data: {
// wizards: wizards
// }
//
// })
},
setFilePath(event) {
console.log(event.target.files[0])
console.log(event.target.files[0]);
this.set('filePath', 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])
}
}
,
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',{
type: 'POST' ,
data:upload ,
let $formData = new FormData();
$formData.append('file', this.get('filePath'));
console.log($formData);
}).then(result=>{
if(result.error){
console.log(result.error)
ajax('/admin/wizards/transfer/import', {
type: 'POST',
data: $formData,
processData: false,
contentType: false,
}else{
alert('wizards imported successfully')
}
})
}
fileReader.readAsText(this.get('filePath'))
}).then(result => {
if (result.error) {
alert(result.error)
} else {
alert(result.success)
}
})
}

Datei anzeigen

@ -17,25 +17,55 @@ class CustomWizard::TransferController < ::ApplicationController
|w|
# p w
wizard_objects.push(PluginStore.get('custom_wizard',w.tr('-','_')))
wizard_objects.push(PluginStore.get('custom_wizard', w.tr('-', '_')))
end
puts '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
def is_json(string)
begin
!!JSON.parse(string)
rescue
false
end
end
def import
json = params['fileJson']
jsonObject = JSON.parse json
puts 'json file'
# p jsonObject
file = File.read(params['file'].tempfile)
fileSize = file.size
maxFileSize = 512 * 1024
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|
# validate whether the given json is a valid "wizard"
next unless CustomWizard::Template.new(o)
countValid += 1
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
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
# admin/wizards/transfer/import