1+ angular . module ( 'ngS3upload.directives' , [ ] ) .
2+ directive ( 's3Upload' , [ '$parse' , 'S3Uploader' , function ( $parse , S3Uploader ) {
3+ return {
4+ restrict : 'AC' ,
5+ require : '?ngModel' ,
6+ replace : true ,
7+ transclude : false ,
8+ scope : true ,
9+ controller : [ '$scope' , '$element' , '$attrs' , '$transclude' , function ( $scope , $element , $attrs , $transclude ) {
10+ $scope . attempt = false ;
11+ $scope . success = false ;
12+ $scope . uploading = false ;
13+
14+ $scope . barClass = function ( ) {
15+ return {
16+ "bar-success" : $scope . attempt && ! $scope . uploading && $scope . success
17+ } ;
18+ } ;
19+ } ] ,
20+ compile : function ( element , attr , linker ) {
21+ return {
22+ pre : function ( $scope , $element , $attr ) {
23+ if ( angular . isUndefined ( $attr . bucket ) ) {
24+ throw Error ( 'bucket is a mandatory attribute' ) ;
25+ }
26+ } ,
27+ post : function ( scope , element , attrs , ngModel ) {
28+ // Build the opts array
29+ var opts = angular . extend ( { } , scope . $eval ( attrs . s3UploadOptions || attrs . options ) ) ;
30+ opts = angular . extend ( {
31+ submitOnChange : true ,
32+ getOptionsUri : '/getS3Options' ,
33+ acl : 'public-read' ,
34+ uploadingKey : 'uploading' ,
35+ folder : ''
36+ } , opts ) ;
37+ var bucket = scope . $eval ( attrs . bucket ) ;
38+
39+ // Bind the button click event
40+ var button = angular . element ( element . children ( ) [ 0 ] ) ,
41+ file = angular . element ( element . find ( "input" ) [ 0 ] ) ;
42+ button . bind ( 'click' , function ( e ) {
43+ file [ 0 ] . click ( ) ;
44+ } ) ;
45+
46+ // Update the scope with the view value
47+ ngModel . $render = function ( ) {
48+ scope . filename = ngModel . $viewValue ;
49+ } ;
50+
51+ var uploadFile = function ( ) {
52+ var selectedFile = file [ 0 ] . files [ 0 ] ;
53+ var filename = selectedFile . name ;
54+ var ext = filename . split ( '.' ) . pop ( ) ;
55+
56+ scope . $apply ( function ( ) {
57+ S3Uploader . getUploadOptions ( opts [ 'getOptionsUri' ] ) . then ( function ( s3Options ) {
58+ ngModel . $setValidity ( 'uploading' , false ) ;
59+ var s3Uri = 'https://' + bucket + '.s3.amazonaws.com/' + opts [ 'folder' ] ;
60+ var key = ( new Date ) . getTime ( ) + '-' + S3Uploader . randomString ( 16 ) + "." + ext ;
61+ S3Uploader . upload ( scope ,
62+ s3Uri ,
63+ key ,
64+ opts [ 'acl' ] ,
65+ selectedFile . type ,
66+ s3Options . key ,
67+ s3Options . policy ,
68+ s3Options . signature ,
69+ selectedFile
70+ ) . then ( function ( ) {
71+ ngModel . $setViewValue ( s3Uri + key ) ;
72+ scope . filename = ngModel . $viewValue ;
73+ ngModel . $setValidity ( 'uploading' , true ) ;
74+ ngModel . $setValidity ( 'succeeded' , true ) ;
75+ } , function ( ) {
76+ scope . filename = ngModel . $viewValue ;
77+ ngModel . $setValidity ( 'uploading' , true ) ;
78+ ngModel . $setValidity ( 'succeeded' , false ) ;
79+ } ) ;
80+
81+ } , function ( error ) {
82+ throw Error ( "Can't receive the needed options for S3 " + error ) ;
83+ } ) ;
84+ } ) ;
85+ } ;
86+
87+ element . bind ( 'change' , function ( nVal ) {
88+ if ( opts [ 'submitOnChange' ] ) {
89+ uploadFile ( ) ;
90+ }
91+ } ) ;
92+
93+ }
94+ }
95+ } ,
96+ template : '<div class="upload-wrap">' +
97+ '<button class="btn btn-primary" type="button"><span ng-if="!filename">Choose file</span><span ng-if="filename">Replace file</span></button>' +
98+ '<a ng-href="{{ filename }}" target="_blank" class="" ng-if="filename" > Stored file </a>' +
99+ '<div class="progress progress-striped" ng-class="{active: uploading}" ng-show="attempt" style="margin-top: 10px">' +
100+ '<div class="bar" style="width: {{ progress }}%;" ng-class="barClass()"></div>' +
101+ '</div>' +
102+ '<input type="file" style="display: none"/>' +
103+ '</div>'
104+ }
105+ } ] ) ;
0 commit comments