added some validations and changed the import logic to use FormData instead of FileReader
Dieser Commit ist enthalten in:
Ursprung
63fb0658e9
Commit
98d8eeffca
2 geänderte Dateien mit 74 neuen und 49 gelöschten Zeilen
|
@ -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]);
|
||||
|
||||
// 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
|
||||
|
||||
let $formData = new FormData();
|
||||
$formData.append('file', this.get('filePath'));
|
||||
console.log($formData);
|
||||
|
||||
ajax('/admin/wizards/transfer/import', {
|
||||
type: 'POST',
|
||||
data:upload ,
|
||||
data: $formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
|
||||
}).then(result => {
|
||||
if (result.error) {
|
||||
console.log(result.error)
|
||||
alert(result.error)
|
||||
|
||||
} else {
|
||||
alert('wizards imported successfully')
|
||||
alert(result.success)
|
||||
}
|
||||
})
|
||||
}
|
||||
fileReader.readAsText(this.get('filePath'))
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -26,16 +26,46 @@ class CustomWizard::TransferController < ::ApplicationController
|
|||
|
||||
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'
|
||||
#plugin store detects the json object type and sets proper `type_name` for the entry
|
||||
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
|
||||
|
|
Laden …
In neuem Issue referenzieren