Fix mailbox import
This commit is contained in:
@@ -209,13 +209,13 @@
|
||||
<div class="modal-body">
|
||||
<div ng-show="!mailboxImport.done">
|
||||
<div ng-show="!mailboxImport.busy">
|
||||
<p ng-bind-html=" 'email.mailboxImportDialog.description' | tr:{ docsLink: 'https://cloudron.io/documentation/email/#import' } "></p>
|
||||
<input type="file" style="display: none;" id="mailboxImportFileInput" accept="application/json"/>
|
||||
<p ng-bind-html=" 'email.mailboxImportDialog.description' | tr:{ docsLink: 'https://cloudron.io/documentation/email/#import-mailboxes' } "></p>
|
||||
<input type="file" style="display: none;" id="mailboxImportFileInput" accept="application/json,text/csv"/>
|
||||
<button class="btn btn-primary" ng-click="mailboxImport.openFileInput()">{{ 'email.mailboxImportDialog.fileInput' | tr }}</button>
|
||||
<br/>
|
||||
<br/>
|
||||
<p class="text-danger" ng-show="mailboxImport.error.file">{{ mailboxImport.error.file }}</p>
|
||||
<p ng-show="mailboxImport.mailboxes.length">{{ 'email.mailboxImportDialog.mailboxesFound' | tr:{ count: mailboxImport.mailboxes.length } }}</p>
|
||||
<p class="text-info" ng-show="mailboxImport.mailboxes.length">{{ 'email.mailboxImportDialog.mailboxesFound' | tr:{ count: mailboxImport.mailboxes.length } }}</p>
|
||||
</div>
|
||||
<div ng-show="mailboxImport.busy" class="progress progress-striped active">
|
||||
<div class="progress-bar progress-bar-success" role="progressbar" style="width: {{ mailboxImport.percent }}%"></div>
|
||||
|
||||
+48
-13
@@ -377,24 +377,50 @@ angular.module('Application').controller('EmailController', ['$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.mailboxImport.mailboxes = JSON.parse(reader.result);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse mailboxes.', e);
|
||||
$scope.mailboxImport.error = { file: 'Imported file is not valid JSON' };
|
||||
$scope.mailboxImport.mailboxes = [];
|
||||
var mailboxes = [];
|
||||
|
||||
if (file.type === 'text/csv') {
|
||||
var lines = reader.result.split('\n');
|
||||
if (lines.length === 0) return $scope.mailboxImport.error = { file: 'Imported file has no lines' };
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i].trim();
|
||||
if (!line) continue;
|
||||
var items = line.split(',');
|
||||
if (items.length !== 4) {
|
||||
$scope.mailboxImport.error = { file: 'Line ' + (i+1) + ' has wrong column count. Expecting 4' };
|
||||
return;
|
||||
}
|
||||
mailboxes.push({
|
||||
name: items[0].trim(),
|
||||
domain: items[1].trim(),
|
||||
owner: items[2].trim(),
|
||||
ownerType: items[3].trim(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
mailboxes = JSON.parse(reader.result).map(function (mailbox) {
|
||||
return {
|
||||
name: mailbox.name,
|
||||
domain: mailbox.domain,
|
||||
owner: mailbox.owner,
|
||||
ownerType: mailbox.ownerType
|
||||
};
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Failed to parse mailboxes.', e);
|
||||
$scope.mailboxImport.error = { file: 'Imported file is not valid JSON' };
|
||||
}
|
||||
}
|
||||
|
||||
// TODO check for validity and minium requirements for the mailboxes from JSON
|
||||
// currently supported and required fields are:
|
||||
// domain
|
||||
// name
|
||||
// ownerId
|
||||
// ownerType
|
||||
$scope.mailboxImport.mailboxes = mailboxes;
|
||||
});
|
||||
}, false);
|
||||
reader.readAsText(file);
|
||||
@@ -423,7 +449,15 @@ angular.module('Application').controller('EmailController', ['$scope', '$locatio
|
||||
var processed = 0;
|
||||
|
||||
async.eachSeries($scope.mailboxImport.mailboxes, function (mailbox, callback) {
|
||||
Client.addMailbox(mailbox.domain, mailbox.name, mailbox.ownerId, mailbox.ownerType, function (error) {
|
||||
var owner = $scope.owners.find(function (o) { return o.display === mailbox.owner && o.type === mailbox.ownerType; }); // owner may not exist
|
||||
if (!owner) {
|
||||
$scope.mailboxImport.error.import.push({ error: new Error('Could not detect owner'), mailbox: mailbox });
|
||||
++processed;
|
||||
$scope.mailboxImport.percent = 100 * processed / $scope.mailboxImport.mailboxes.length;
|
||||
return callback();
|
||||
}
|
||||
|
||||
Client.addMailbox(mailbox.domain, mailbox.name, owner.id, mailbox.ownerType, function (error) {
|
||||
if (error) $scope.mailboxImport.error.import.push({ error: error, mailbox: mailbox });
|
||||
else ++$scope.mailboxImport.success;
|
||||
|
||||
@@ -437,6 +471,7 @@ angular.module('Application').controller('EmailController', ['$scope', '$locatio
|
||||
|
||||
$scope.mailboxImport.busy = false;
|
||||
$scope.mailboxImport.done = true;
|
||||
if ($scope.mailboxImport.success) $scope.mailboxes.refresh();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -145,7 +145,7 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
var items = line.split(',');
|
||||
if (items.length !== 5) {
|
||||
$scope.userImport.error = { file: 'Line ' + (i+1) + ' has wrong column count. Expecting 5' };
|
||||
break;
|
||||
return;
|
||||
}
|
||||
users.push({
|
||||
username: items[0].trim(),
|
||||
|
||||
Reference in New Issue
Block a user