Files
meanTorrent/modules/users/client/controllers/settings/change-profile-picture.client.controller.js
Laurence Tennant 2b6cf53d30 fix(users): Better MIME-type checking, remove image cropping library (#1589)
* Cropping remove, nicer UI

* Fix MIME-type checking, add image upload tests

* Change image config settings to uploads.profile.image to build a more
rational structure for configuring other types of uploads
2016-11-15 15:59:47 -08:00

57 lines
1.6 KiB
JavaScript

(function () {
'use strict';
angular
.module('users')
.controller('ChangeProfilePictureController', ChangeProfilePictureController);
ChangeProfilePictureController.$inject = ['$timeout', 'Authentication', 'Upload', 'Notification'];
function ChangeProfilePictureController($timeout, Authentication, Upload, Notification) {
var vm = this;
vm.user = Authentication.user;
vm.progress = 0;
vm.upload = function (dataUrl) {
Upload.upload({
url: '/api/users/picture',
data: {
newProfilePicture: dataUrl
}
}).then(function (response) {
$timeout(function () {
onSuccessItem(response.data);
});
}, function (response) {
if (response.status > 0) onErrorItem(response.data);
}, function (evt) {
vm.progress = parseInt(100.0 * evt.loaded / evt.total, 10);
});
};
// Called after the user has successfully uploaded a new picture
function onSuccessItem(response) {
// Show success message
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> Successfully changed profile picture' });
// Populate user object
vm.user = Authentication.user = response;
// Reset form
vm.fileSelected = false;
vm.progress = 0;
}
// Called after the user has failed to upload a new picture
function onErrorItem(response) {
vm.fileSelected = false;
vm.progress = 0;
// Show error message
Notification.error({ message: response.message, title: '<i class="glyphicon glyphicon-remove"></i> Failed to change profile picture' });
}
}
}());