Skip to content

Commit e209c16

Browse files
committed
fix: avoid duplicating the code to accept files
1 parent 4a8a377 commit e209c16

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

packages/uui-file-dropzone/lib/uui-file-dropzone.element.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
114114
queue.push(dataTransferItemList[i].webkitGetAsEntry());
115115
}
116116

117+
const acceptList: string[] = [];
118+
const wildcards: string[] = [];
119+
117120
// if the accept filer is set
118121
if (this.accept) {
119-
const acceptList: string[] = [];
120-
const wildcards: string[] = [];
121-
122122
// Create the arrays defined above
123123
this.accept.split(',').forEach(item => {
124124
if (item.includes('*')) {
@@ -127,6 +127,7 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
127127
acceptList.push(item.trim().toLowerCase());
128128
}
129129
});
130+
}
130131

131132
while (queue.length > 0) {
132133
const entry: FileSystemFileEntry = queue.shift()!;
@@ -189,27 +190,22 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
189190
}
190191
}
191192

192-
private async _getFile(fileEntry: FileSystemFileEntry): Promise<File> {
193-
return await new Promise<File>((resolve, reject) =>
194-
fileEntry.file(resolve, reject)
195-
);
196-
}
193+
private _isAccepted(acceptList: string[], wildcards: string[], file: File) {
194+
if (acceptList.length === 0 && wildcards.length === 0) {
195+
return true;
196+
}
197197

198-
private async _isAccepted(
199-
acceptList: string[],
200-
wildcards: string[],
201-
entry: FileSystemFileEntry
202-
) {
203-
const file = await this._getFile(entry);
204198
const fileType = file.type.toLowerCase();
205199
const fileExtension = '.' + file.name.split('.')[1].toLowerCase();
206200

207201
if (acceptList.includes(fileExtension)) {
208202
return true;
209203
}
204+
210205
if (acceptList.includes(fileType)) {
211206
return true;
212207
}
208+
213209
if (wildcards.some(wildcard => fileType.startsWith(wildcard))) {
214210
return true;
215211
}

0 commit comments

Comments
 (0)