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 14, 2016
1 parent ae2c3c1 commit c14b86d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
28 changes: 27 additions & 1 deletion addon/components/file-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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 c14b86d

Please sign in to comment.