Skip to content

Commit b3597c5

Browse files
author
Roland Groza
committed
fix: filter non-file items out
1 parent d318b88 commit b3597c5

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/file-selector.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ it('uses the DataTransfer {items} instead of {files} if it exists', async () =>
7070
expect(file.path).toBe(name);
7171
});
7272

73+
it('skips DataTransfer {items} that are of kind "string"', async () => {
74+
const item = dataTransferItemFromStr('test');
75+
const evt = dragEvtFromItems(item);
76+
77+
const files = await fromEvent(evt);
78+
expect(files).toHaveLength(0);
79+
});
80+
7381
it('can read a tree of directories recursively and return a flat list of FileWithPath objects', async () => {
7482
const mockFiles = sortFiles([
7583
createFile('ping.json', {ping: true}),
@@ -124,8 +132,20 @@ function dataTransferItemFromFile(file: File): DataTransferItem {
124132
getAsFile() {
125133
return file;
126134
},
127-
getAsString() {
135+
// tslint:disable-next-line: no-empty
136+
getAsString() {}
137+
} as any;
138+
}
139+
140+
function dataTransferItemFromStr(str: string): DataTransferItem {
141+
return {
142+
kind: 'string',
143+
type: 'text/plain',
144+
getAsFile() {
128145
return null;
146+
},
147+
getAsString(cb: (data: string) => void) {
148+
return cb(str);
129149
}
130150
} as any;
131151
}

src/file-selector.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ async function getDataTransferFiles(dt: DataTransfer) {
4242
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
4343
function toFilePromises(item: DataTransferItem) {
4444
if (typeof item.webkitGetAsEntry !== 'function') {
45-
return fromDataTransferItem(item);
45+
if (item.kind === 'file') {
46+
return fromDataTransferItem(item);
47+
}
48+
return [];
4649
}
4750

4851
const entry = item.webkitGetAsEntry();

src/file.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ describe('toFile()', () => {
4545
type: 'application/json',
4646
lastModified: 1234567
4747
};
48-
const file = new File([], 'test.json', opts);
48+
const data = JSON.stringify({ping: true});
49+
const file = new File([data], 'test.json', opts);
4950
const fwp = toFileWithPath(file);
5051

5152
expect(fwp === file).toBe(false);
52-
expect(fwp).toEqual(file);
53+
expect(fwp.name).toEqual(file.name);
54+
expect(fwp.type).toEqual(file.type);
55+
expect(fwp.size).toEqual(file.size);
56+
expect(fwp.lastModified).toEqual(file.lastModified);
5357
});
5458

5559
it('should behave like a File', done => {

src/file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ function withMimeType(file: File) {
4545
}
4646

4747
function clone(file: File, type?: string) {
48-
return new File([file], file.name, {
48+
const data = file.slice();
49+
return new File([data], file.name, {
4950
lastModified: file.lastModified,
5051
type: type || file.type
5152
});

0 commit comments

Comments
 (0)