diff --git a/addon/components/file-picker.js b/addon/components/file-picker.js index d0fa5d6..f8e220c 100644 --- a/addon/components/file-picker.js +++ b/addon/components/file-picker.js @@ -73,13 +73,39 @@ export default Component.extend({ } }, - handleFiles: function(files) { + _invalidExtension: function(files) { + let accept = this.get('accept'); + + if (accept === '*') { + return; + } + + let validExtensions = accept.split(','); + + let fileExtensions = files.map(file => `.${file.filename.split('.').slice(-1)[0]}`); + + return fileExtensions.some(extension => validExtensions.indexOf(extension) === -1); + }, + + _validate: function(files) { if (typeof(this.filesAreValid) === 'function') { if (!this.filesAreValid(files)) { return; } } + if (this._invalidExtension(files)) { + return; + } + + return true; + }, + + handleFiles: function(files) { + if (!this._validate(files)) { + return; + } + if (this.get('preview')) { this.updatePreview(files); } diff --git a/tests/unit/components/file-picker-test.js b/tests/unit/components/file-picker-test.js index 6224459..0c93a8d 100644 --- a/tests/unit/components/file-picker-test.js +++ b/tests/unit/components/file-picker-test.js @@ -90,4 +90,22 @@ test('it shows file input', function(assert) { this.render(); assert.equal(component.$('input:hidden').length, 0); -}); \ No newline at end of file +}); + +test('it rejects improper filetypes', function(assert) { + assert.expect(2); + + const component = this.subject({ + accept: '.jpg,.jpeg', + multiple: true + }); + + const files = [ + { filename: 'goodfile.jpg' }, + { filename: 'good_file.jpeg' }, + { filename: 'badfile.html' } + ]; + + assert.strictEqual(component._invalidExtension(files), true); + assert.strictEqual(component._invalidExtension(files.slice(0, 2)), false); +});