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 $formData = new FormData();
let upload = {'fileJson': fileReader.result}; $formData.append('file', this.get('filePath'));
// ajax('admin/wizard/transfer/import'); console.log($formData);
console.log(fileReader.result)
//ajax call
ajax('/admin/wizards/transfer/import', { ajax('/admin/wizards/transfer/import', {
type: 'POST', type: 'POST',
data:upload , data: $formData,
processData: false,
contentType: false,
}).then(result => { }).then(result => {
if (result.error) { if (result.error) {
console.log(result.error) alert(result.error)
} else { } else {
alert('wizards imported successfully') alert(result.success)
} }
}) })
}
fileReader.readAsText(this.get('filePath'))
} }

Datei anzeigen

@ -26,16 +26,46 @@ class CustomWizard::TransferController < ::ApplicationController
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