Skip to content
Merged
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
37 changes: 34 additions & 3 deletions src/file-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,37 @@ it("should not use getAsFileSystemHandle when not in a secure context", async ()
globalThis.isSecureContext = true;
});

it("should reject when getAsFileSystemHandle resolves to null", async () => {
const evt = dragEvtFromItems([dataTransferItemWithFsHandle(null, null)]);
expect(fromEvent(evt)).rejects.toThrow("[object Object] is not a File");
});

it("should fallback to getAsFile when getAsFileSystemHandle resolves to undefined", async () => {
const name = "test.nosec.json";
const mockFile = createFile(
name,
{ ping: false },
{
type: "application/json",
},
);
const evt = dragEvtFromItems([
dataTransferItemWithFsHandle(mockFile, undefined),
]);

const files = await fromEvent(evt);
expect(files).toHaveLength(1);
expect(files.every((file) => file instanceof File)).toBe(true);

const [file] = files as FileWithPath[];

expect(file.name).toBe(mockFile.name);
expect(file.type).toBe(mockFile.type);
expect(file.size).toBe(mockFile.size);
expect(file.lastModified).toBe(mockFile.lastModified);
expect(file.path).toBe(`./${name}`);
});

function dragEvtFromItems(
items: DataTransferItem | DataTransferItem[],
type: string = "drop",
Expand Down Expand Up @@ -456,8 +487,8 @@ function dataTransferItemFromEntry(
}

function dataTransferItemWithFsHandle(
file?: File,
h?: FileSystemFileHandle,
file?: File | null,
h?: FileSystemFileHandle | null,
): DataTransferItem {
return {
kind: "file",
Expand Down Expand Up @@ -570,7 +601,7 @@ function sortFiles<T extends File>(files: T[]) {
}

interface FileSystemFileHandle {
getFile(): Promise<File>;
getFile(): Promise<File | null>;
}

type FileOrDirEntry = FileEntry | DirEntry;
Expand Down
16 changes: 11 additions & 5 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function flatten<T>(items: any[]): T[] {
);
}

function fromDataTransferItem(
async function fromDataTransferItem(
item: DataTransferItem,
entry?: FileSystemEntry | null,
) {
Expand All @@ -118,18 +118,24 @@ function fromDataTransferItem(
globalThis.isSecureContext &&
typeof (item as any).getAsFileSystemHandle === "function"
) {
return (item as any).getAsFileSystemHandle().then(async (h: any) => {
const h = await (item as any).getAsFileSystemHandle();
if (h === null) {
throw new Error(`${item} is not a File`);
}
// It seems that the handle can be `undefined` (see https://github.com/react-dropzone/file-selector/issues/120),
// so we check if it isn't; if it is, the code path continues to the next API (`getAsFile`).
if (h !== undefined) {
const file = await h.getFile();
file.handle = h;
return toFileWithPath(file);
});
}
}
const file = item.getAsFile();
if (!file) {
return Promise.reject(`${item} is not a File`);
throw new Error(`${item} is not a File`);
}
const fwp = toFileWithPath(file, entry?.fullPath ?? undefined);
return Promise.resolve(fwp);
return fwp;
}

async function fromEntry(
Expand Down