1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00
Files
browser/src/app/vault/vaultAttachmentsController.js
2017-09-20 16:45:13 -04:00

127 lines
4.7 KiB
JavaScript

angular
.module('bit.vault')
.controller('vaultAttachmentsController', function ($scope, apiService, $uibModalInstance, cryptoService, cipherService,
loginId, $analytics, validationService, toastr, $timeout, authService, $uibModal) {
$analytics.eventTrack('vaultAttachmentsController', { category: 'Modal' });
$scope.login = {};
$scope.readOnly = true;
$scope.loading = true;
$scope.isPremium = true;
$scope.canUseAttachments = true;
var closing = false;
authService.getUserProfile().then(function (profile) {
$scope.isPremium = profile.premium;
return apiService.ciphers.get({ id: loginId }).$promise;
}).then(function (cipher) {
$scope.login = cipherService.decryptLogin(cipher);
$scope.readOnly = !$scope.login.edit;
$scope.canUseAttachments = $scope.isPremium || $scope.login.organizationId;
$scope.loading = false;
}, function () {
$scope.loading = false;
});
$scope.save = function (form) {
var fileEl = document.getElementById('file');
var files = fileEl.files;
if (!files || !files.length) {
validationService.addError(form, 'file', 'Select a file.', true);
return;
}
$scope.savePromise = cipherService.encryptAttachmentFile(getKeyForLogin(), files[0]).then(function (encValue) {
var fd = new FormData();
var blob = new Blob([encValue.data], { type: 'application/octet-stream' });
fd.append('data', blob, encValue.fileName);
return apiService.ciphers.postAttachment({ id: loginId }, fd).$promise;
}).then(function (response) {
$analytics.eventTrack('Added Attachment');
$scope.login = cipherService.decryptLogin(response);
// reset file input
// ref: https://stackoverflow.com/a/20552042
fileEl.type = '';
fileEl.type = 'file';
fileEl.value = '';
}, function (err) {
if (err) {
validationService.addError(form, 'file', err, true);
}
else {
validationService.addError(form, 'file', 'Something went wrong.', true);
}
});
};
$scope.download = function (attachment) {
attachment.loading = true;
if (!$scope.canUseAttachments) {
attachment.loading = false;
alert('Premium membership is required to use this feature.');
return;
}
cipherService.downloadAndDecryptAttachment(getKeyForLogin(), attachment, true).then(function (res) {
$timeout(function () {
attachment.loading = false;
});
}, function () {
$timeout(function () {
attachment.loading = false;
});
});
};
function getKeyForLogin() {
if ($scope.login.organizationId) {
return cryptoService.getOrgKey($scope.login.organizationId);
}
return null;
}
$scope.remove = function (attachment) {
if (!confirm('Are you sure you want to delete this attachment (' + attachment.fileName + ')?')) {
return;
}
attachment.loading = true;
apiService.ciphers.delAttachment({ id: loginId, attachmentId: attachment.id }).$promise.then(function () {
attachment.loading = false;
$analytics.eventTrack('Deleted Attachment');
var index = $scope.login.attachments.indexOf(attachment);
if (index > -1) {
$scope.login.attachments.splice(index, 1);
}
}, function () {
toastr.error('Cannot delete attachment.');
attachment.loading = false;
});
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
$scope.$on('modal.closing', function (e, reason, closed) {
if (closing) {
return;
}
e.preventDefault();
closing = true;
$uibModalInstance.close(!!$scope.login.attachments && $scope.login.attachments.length > 0);
});
$scope.showUpgrade = function () {
$uibModal.open({
animation: true,
templateUrl: 'app/views/premiumRequired.html',
controller: 'premiumRequiredController'
});
};
});