-
Notifications
You must be signed in to change notification settings - Fork 37
[FEATURE] Uses accept field to validate provided filetypes #57
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ export default Component.extend({ | |
| dropzone: true, | ||
| progress: true, | ||
| showProgress: false, | ||
| validateExtension: true, | ||
| hideFileInput: true, | ||
| readAs: 'readAsFile', | ||
| selectOnClick: true, | ||
|
|
@@ -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') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move the block into a function
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated 👍 |
||
| if (!this.filesAreValid(files)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (!this.get('validateExtension')) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return; | ||
| } | ||
|
|
||
| return !this._invalidExtension(files); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't work. If you set 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); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validateExtensions(+s)