Skip to content

Commit 47a0db7

Browse files
authored
FileUploader: validation should not consider accept option (T1287250)
1 parent f5a228b commit 47a0db7

File tree

5 files changed

+336
-425
lines changed

5 files changed

+336
-425
lines changed

packages/devextreme/js/__internal/ui/html_editor/utils/m_image_uploader_helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export function getFileUploaderBaseOptions() {
594594
return {
595595
value: [],
596596
name: FILE_UPLOADER_NAME,
597-
accept: 'image/*',
597+
allowedFileExtensions: ['image/*'],
598598
uploadMode: 'useButtons',
599599
};
600600
}

packages/devextreme/js/__internal/ui/m_file_uploader.ts

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -489,25 +489,13 @@ class FileUploader extends Editor<FileUploaderProperties> {
489489
}
490490

491491
_validateFileExtension(file) {
492-
const allowedExtensions = this.option('allowedFileExtensions');
493-
const accept = this.option('accept');
494-
const allowedTypes = this._getAllowedFileTypes(accept);
495-
const fileExtension = file.value.name.substring(file.value.name.lastIndexOf('.')).toLowerCase();
496-
// @ts-expect-error
497-
if (accept?.length !== 0 && !this._isFileTypeAllowed(file.value, allowedTypes)) {
498-
return false;
499-
}
500-
// @ts-expect-error
501-
if (allowedExtensions?.length === 0) {
492+
const { allowedFileExtensions } = this.option();
493+
494+
if (!allowedFileExtensions?.length) {
502495
return true;
503496
}
504-
// @ts-expect-error
505-
for (let i = 0; i < allowedExtensions.length; i++) {
506-
if (fileExtension === allowedExtensions[i].toLowerCase()) {
507-
return true;
508-
}
509-
}
510-
return false;
497+
498+
return this._isFileExtensionAllowed(file.value, allowedFileExtensions);
511499
}
512500

513501
_validateMaxFileSize(file): boolean {
@@ -524,6 +512,27 @@ class FileUploader extends Editor<FileUploaderProperties> {
524512
return minFileSize > 0 ? fileSize >= minFileSize : true;
525513
}
526514

515+
_isFileExtensionAllowed(file, allowedExtensions) {
516+
for (let i = 0, n = allowedExtensions.length; i < n; i += 1) {
517+
let allowedExtension = allowedExtensions[i];
518+
519+
if (allowedExtension[0] === '.') {
520+
allowedExtension = allowedExtension.replace('.', '\\.');
521+
522+
if (file.name.match(new RegExp(`${allowedExtension}$`, 'i'))) {
523+
return true;
524+
}
525+
} else {
526+
allowedExtension = allowedExtension.replace(new RegExp('\\*', 'g'), '');
527+
528+
if (file.type.match(new RegExp(allowedExtension, 'i'))) {
529+
return true;
530+
}
531+
}
532+
}
533+
return false;
534+
}
535+
527536
_createBeforeSendAction() {
528537
this._beforeSendAction = this._createActionByOption('onBeforeSend', { excludeValidators: ['readOnly'] });
529538
}
@@ -931,7 +940,6 @@ class FileUploader extends Editor<FileUploaderProperties> {
931940
}
932941

933942
_isInteractionDisabled() {
934-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
935943
return this.option('readOnly') || this.option('disabled');
936944
}
937945

@@ -1129,35 +1137,6 @@ class FileUploader extends Editor<FileUploaderProperties> {
11291137
}
11301138
}
11311139

1132-
_getAllowedFileTypes(acceptSting) {
1133-
if (!acceptSting.length) {
1134-
return [];
1135-
}
1136-
1137-
return acceptSting.split(',').map((item) => item.trim());
1138-
}
1139-
1140-
_isFileTypeAllowed(file, allowedTypes) {
1141-
for (let i = 0, n = allowedTypes.length; i < n; i++) {
1142-
let allowedType = allowedTypes[i];
1143-
1144-
if (allowedType[0] === '.') {
1145-
allowedType = allowedType.replace('.', '\\.');
1146-
1147-
if (file.name.match(new RegExp(`${allowedType}$`, 'i'))) {
1148-
return true;
1149-
}
1150-
} else {
1151-
allowedType = allowedType.replace(new RegExp('\\*', 'g'), '');
1152-
1153-
if (file.type.match(new RegExp(allowedType, 'i'))) {
1154-
return true;
1155-
}
1156-
}
1157-
}
1158-
return false;
1159-
}
1160-
11611140
_renderWrapper() {
11621141
const $wrapper = $('<div>')
11631142
.addClass(FILEUPLOADER_WRAPPER_CLASS)
@@ -1233,7 +1212,7 @@ class FileUploader extends Editor<FileUploaderProperties> {
12331212
}
12341213

12351214
_updateProgressBar(file, loadedFileData) {
1236-
file.progressBar && file.progressBar.option({
1215+
file.progressBar?.option({
12371216
value: loadedFileData.loaded,
12381217
showStatus: true,
12391218
});
@@ -1659,7 +1638,7 @@ class FileUploadStrategyBase {
16591638
}
16601639

16611640
file.isAborted = true;
1662-
file.request && file.request.abort();
1641+
file.request?.abort();
16631642

16641643
if (this._isCustomCallback('abortUpload')) {
16651644
const abortUpload = this.fileUploader.option('abortUpload');

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/fileUploader.markup.tests.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,26 @@ QUnit.module('multiple option', () => {
212212
});
213213

214214
QUnit.module('option accept', () => {
215-
QUnit.test('field accept should be rendered correctly', function(assert) {
215+
QUnit.test('accept attr should be added on input on init', function(assert) {
216216
const $fileUploader = $('#fileuploader').dxFileUploader({
217217
accept: 'image/*'
218218
});
219-
const fileUploader = $fileUploader.dxFileUploader('instance');
220219

221220
const $fileInput = $fileUploader.find('.' + FILEUPLOADER_INPUT_CLASS);
222221

223-
assert.equal($fileInput.prop('accept'), 'image/*', 'value was set to empty string');
222+
assert.strictEqual($fileInput.prop('accept'), 'image/*', 'accept attr is added');
223+
});
224+
225+
QUnit.test('accept attr should be updated on input at runtime', function(assert) {
226+
const $fileUploader = $('#fileuploader').dxFileUploader({
227+
accept: 'image/*'
228+
});
229+
const fileUploader = $fileUploader.dxFileUploader('instance');
230+
const $fileInput = $fileUploader.find('.' + FILEUPLOADER_INPUT_CLASS);
224231

225232
fileUploader.option('accept', 'video/*');
226-
assert.equal($fileInput.prop('accept'), 'video/*', 'value was set to empty string');
233+
234+
assert.strictEqual($fileInput.prop('accept'), 'video/*', 'accept attr is updated');
227235
});
228236
});
229237

0 commit comments

Comments
 (0)