Skip to content

Commit

Permalink
fix: handle getFile returning null and close #106, close #120
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandjitsu committed Nov 27, 2024
1 parent 90fa75c commit 7c0596f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
33 changes: 31 additions & 2 deletions src/file-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,35 @@ it('should not use getAsFileSystemHandle when not in a secure context', async ()
window.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'): DragEvent {
return {
type,
Expand Down Expand Up @@ -403,7 +432,7 @@ function dataTransferItemFromEntry(entry: FileEntry | DirEntry, file?: File): Da
} as any;
}

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


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

type FileOrDirEntry = FileEntry | DirEntry
Expand Down
13 changes: 10 additions & 3 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,16 @@ async function fromDataTransferItem(item: DataTransferItem, entry?: FileSystemEn
// - https://github.com/react-dropzone/react-dropzone/issues/1397
if (globalThis.isSecureContext && typeof (item as any).getAsFileSystemHandle === 'function') {
const h = await (item as any).getAsFileSystemHandle();
const file = await h.getFile();
file.handle = h;
return toFileWithPath(file);
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) {
Expand Down

0 comments on commit 7c0596f

Please sign in to comment.