Add basic support for csv user import
This commit is contained in:
@@ -351,8 +351,8 @@
|
||||
<div class="modal-body">
|
||||
<div ng-show="!userImport.done">
|
||||
<div ng-show="!userImport.busy">
|
||||
<input type="file" style="display: none;" id="userImportFileInput" accept="application/json"/>
|
||||
<button class="btn btn-primary" ng-click="userImport.openFileInput()">Select JSON file</button>
|
||||
<input type="file" style="display: none;" id="userImportFileInput" accept="application/json,text/csv"/>
|
||||
<button class="btn btn-primary" ng-click="userImport.openFileInput()">Select JSON or CSV file</button>
|
||||
<br/>
|
||||
<br/>
|
||||
<p class="text-danger" ng-show="userImport.error.file">{{ userImport.error.file }}</p>
|
||||
|
||||
+24
-6
@@ -109,16 +109,34 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
if (!fileInput.files || !fileInput.files[0]) return;
|
||||
|
||||
var file = fileInput.files[0];
|
||||
if (file.type !== 'application/json') return console.log('Unsupported file type.');
|
||||
if (file.type !== 'application/json' && file.type !== 'text/csv') return console.log('Unsupported file type.');
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.addEventListener('load', function () {
|
||||
$scope.$apply(function () {
|
||||
try {
|
||||
$scope.userImport.users = JSON.parse(reader.result);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse users.', e);
|
||||
$scope.userImport.error = { file: 'Imported file is not valid JSON' };
|
||||
if (file.type === 'text/csv') {
|
||||
$scope.userImport.users = [];
|
||||
|
||||
var lines = reader.result.split('\n');
|
||||
if (lines.length === 0) return $scope.userImport.error = { file: 'Imported file has no lines' };
|
||||
|
||||
lines.forEach(function (line) {
|
||||
var items = line.split(',');
|
||||
if (items.length !== 4) return console.log('Wrong column count for:', line);
|
||||
$scope.userImport.users.push({
|
||||
id: items[0].trim(),
|
||||
username: items[1].trim(),
|
||||
email: items[2].trim(),
|
||||
displayName: items[3].trim()
|
||||
});
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
$scope.userImport.users = JSON.parse(reader.result);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse users.', e);
|
||||
$scope.userImport.error = { file: 'Imported file is not valid JSON' };
|
||||
}
|
||||
}
|
||||
});
|
||||
}, false);
|
||||
|
||||
Reference in New Issue
Block a user