File tree Expand file tree Collapse file tree 2 files changed +46
-2
lines changed
Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -73,13 +73,39 @@ export default Component.extend({
7373 }
7474 } ,
7575
76- handleFiles : function ( files ) {
76+ _invalidExtension : function ( files ) {
77+ let accept = this . get ( 'accept' ) ;
78+
79+ if ( accept === '*' ) {
80+ return ;
81+ }
82+
83+ let validExtensions = accept . split ( ',' ) ;
84+
85+ let fileExtensions = files . map ( file => `.${ file . filename . split ( '.' ) . slice ( - 1 ) [ 0 ] } ` ) ;
86+
87+ return fileExtensions . some ( extension => validExtensions . indexOf ( extension ) === - 1 ) ;
88+ } ,
89+
90+ _validate : function ( files ) {
7791 if ( typeof ( this . filesAreValid ) === 'function' ) {
7892 if ( ! this . filesAreValid ( files ) ) {
7993 return ;
8094 }
8195 }
8296
97+ if ( this . _invalidExtension ( files ) ) {
98+ return ;
99+ }
100+
101+ return true ;
102+ } ,
103+
104+ handleFiles : function ( files ) {
105+ if ( ! this . _validate ( files ) ) {
106+ return ;
107+ }
108+
83109 if ( this . get ( 'preview' ) ) {
84110 this . updatePreview ( files ) ;
85111 }
Original file line number Diff line number Diff line change @@ -90,4 +90,22 @@ test('it shows file input', function(assert) {
9090 this . render ( ) ;
9191
9292 assert . equal ( component . $ ( 'input:hidden' ) . length , 0 ) ;
93- } ) ;
93+ } ) ;
94+
95+ test ( 'it rejects improper filetypes' , function ( assert ) {
96+ assert . expect ( 2 ) ;
97+
98+ const component = this . subject ( {
99+ accept : '.jpg,.jpeg' ,
100+ multiple : true
101+ } ) ;
102+
103+ const files = [
104+ { filename : 'goodfile.jpg' } ,
105+ { filename : 'good_file.jpeg' } ,
106+ { filename : 'badfile.html' }
107+ ] ;
108+
109+ assert . strictEqual ( component . _invalidExtension ( files ) , true ) ;
110+ assert . strictEqual ( component . _invalidExtension ( files . slice ( 0 , 2 ) ) , false ) ;
111+ } ) ;
You can’t perform that action at this time.
0 commit comments