Skip to content

Commit

Permalink
[FEATURE] Uses accept field to validate provided filetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
timmcanty committed Apr 13, 2016
1 parent ae2c3c1 commit 3c1172a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
18 changes: 18 additions & 0 deletions addon/components/file-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,31 @@ export default Component.extend({
}
},

_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);
},

handleFiles: function(files) {
if (typeof(this.filesAreValid) === 'function') {
if (!this.filesAreValid(files)) {
return;
}
}

if (this._invalidExtension(files)) {
return;
}

if (this.get('preview')) {
this.updatePreview(files);
}
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/components/file-picker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,22 @@ test('it shows file input', function(assert) {
this.render();

assert.equal(component.$('input:hidden').length, 0);
});
});

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);
});

0 comments on commit 3c1172a

Please sign in to comment.