Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Uses accept field to validate provided filetypes #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ It supports:
* `accept` default `*`
* `multiple` default `false`
* `selectOnClick` default `true`
* `validateExtensions` default `true`
* `dropzone` default `true`
* `preview` default `true`
* `progress` default `true`
Expand Down
29 changes: 28 additions & 1 deletion addon/components/file-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default Component.extend({
dropzone: true,
progress: true,
showProgress: false,
validateExtension: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateExtensions (+s)

hideFileInput: true,
readAs: 'readAsFile',
selectOnClick: true,
Expand Down Expand Up @@ -73,13 +74,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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the block into a function _validate and call your function in _validate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍

if (!this.filesAreValid(files)) {
return;
}
}

if (!this.get('validateExtension')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateExtensions (+s)

return;
}

return !this._invalidExtension(files);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work. If you set validateExtensions to false it will return above and return in the handleFiles method as well and it would be impossible to upload a file without validation.

I think what we need is:

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

  if (!this.get('validateExtensions')) {
    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);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests that check if handleFiles works properly with the option turned on and off?
Something like that