Skip to content

Commit

Permalink
wrap getWebkitAsEntry method to be future proof if API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenn Van De Putte committed Feb 5, 2024
1 parent 9772c0b commit e60a1ad
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ember-file-upload/src/system/data-transfer-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { FileUploadDragEvent } from '../interfaces.ts';

const getDataSupport = {};

interface FutureProofDataTransferItem extends DataTransferItem {
getAsEntry?: () => FileSystemDirectoryEntry
}

export default class DataTransferWrapper {
dataTransfer?: DataTransfer;
itemDetails?: FileUploadDragEvent['itemDetails'];
Expand Down Expand Up @@ -58,9 +62,19 @@ export default class DataTransferWrapper {
});
};

const getEntry = (item: FutureProofDataTransferItem): FileSystemDirectoryEntry => {
// In the future this method name might change, so already implementing it like this if needed
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
if (typeof item.getAsEntry === "function") {
return item.getAsEntry()
}

return item.webkitGetAsEntry() as FileSystemDirectoryEntry;
}

const readAllFilesInDirectory = (item: DataTransferItem): Promise<File[]> =>
new Promise((resolve) => {
(item.webkitGetAsEntry() as FileSystemDirectoryEntry)
getEntry(item)
?.createReader()
?.readEntries(async (entries: FileSystemEntry[]) => {
const readFiles: File[] = await Promise.all(
Expand All @@ -75,7 +89,7 @@ export default class DataTransferWrapper {
const readDataTransferItem = async (
item: DataTransferItem,
): Promise<File[]> => {
if (item.webkitGetAsEntry()?.isDirectory) {
if (getEntry(item)?.isDirectory) {
const directoryFile = item.getAsFile() as File;
const filesInDirectory: File[] = await readAllFilesInDirectory(item);
return [directoryFile, ...filesInDirectory];
Expand Down

0 comments on commit e60a1ad

Please sign in to comment.