Add basic support for csv user import
This commit is contained in:
@@ -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