11'use strict' ;
22
33angular . module ( 'anchrClientApp' )
4- . controller ( 'ImageCtrl' , [ '$rootScope' , '$scope' , 'Upload' , '$timeout' , function ( $rootScope , $scope , Upload , $timeout ) {
4+ . controller ( 'ImageCtrl' , [ '$rootScope' , '$scope' , 'Snackbar' , ' Upload', 'Encryption' , ' $timeout', function ( $rootScope , $scope , Snackbar , Upload , Encryption , $timeout ) {
55 var allowedTypes = [ 'image/' ] ;
66
77 $scope . encryptAndUpload = function ( files , errFiles ) {
@@ -16,19 +16,27 @@ angular.module('anchrClientApp')
1616 return false ;
1717 }
1818
19- reader . onload = function ( e ) {
20- var encrypted = CryptoJS . AES . encrypt ( e . target . result , password ) ;
21- var blob = new Blob ( [ encrypted ] , { type : file . type } ) ;
22- blob . name = file . name ;
23- blob . encrypted = true ;
24- $scope . files . loading = false ;
25- $scope . uploadFiles ( [ blob ] , [ ] ) ;
19+ reader . onload = function ( e ) {
20+ Encryption . encrypt ( e . target . result , password )
21+ . then ( function ( encrypted ) {
22+ var blob = new Blob ( [ encrypted ] , { type : file . type } ) ;
23+ blob . name = file . name ;
24+ blob . encrypted = true ;
25+ $scope . files . loading = false ;
26+ $scope . uploadFiles ( [ blob ] , [ ] ) ;
27+ } )
28+ . catch ( function ( error ) {
29+ console . error ( error ) ;
30+
31+ $scope . files . loading = false ;
32+ Snackbar . show ( "Failed to encrypt image." ) ;
33+ } ) ;
2634 } ;
2735
28- reader . readAsDataURL ( file ) ;
36+ reader . readAsArrayBuffer ( file ) ;
2937 } ;
3038
31- $scope . uploadFiles = function ( files , errFiles ) {
39+ $scope . uploadFiles = function ( files , errFiles ) {
3240 if ( ( files && files . length ) || ( errFiles && errFiles . length ) ) {
3341 $scope . files . files = $scope . files . files . concat ( files ) ;
3442 $scope . files . errFiles = $scope . files . errFiles . concat ( errFiles ) ;
@@ -41,7 +49,7 @@ angular.module('anchrClientApp')
4149 else {
4250 file . upload = Upload . upload ( {
4351 url : $rootScope . getApiUrl ( ) + 'image' ,
44- data : { uploadFile : file , encrypted : file . encrypted }
52+ data : { uploadFile : file , encrypted : file . encrypted }
4553 } ) ;
4654
4755 file . upload . then ( function ( response ) {
@@ -63,7 +71,7 @@ angular.module('anchrClientApp')
6371
6472 $scope . clear = function ( ) {
6573 $scope . files = {
66- files : [ ] ,
74+ files : [ ] ,
6775 errFiles : [ ] ,
6876 password : null ,
6977 encrypt : false ,
@@ -79,8 +87,8 @@ angular.module('anchrClientApp')
7987 $rootScope . init ( ) ;
8088 }
8189
82- function arrayMatch ( regexArray , val ) {
83- for ( var i = 0 ; i < regexArray . length ; i ++ ) {
90+ function arrayMatch ( regexArray , val ) {
91+ for ( var i = 0 ; i < regexArray . length ; i ++ ) {
8492 if ( val . match ( regexArray [ i ] ) ) return true ;
8593 }
8694 return false ;
0 commit comments