Mostly get drag and drop working

This commit is contained in:
ZXY101
2023-09-13 16:57:52 +02:00
parent 3c8ab90b06
commit 3a37b62a38
9 changed files with 284 additions and 146 deletions

View File

@@ -3,4 +3,42 @@ export async function requestPersistentStorage() {
const isPersisted = await navigator.storage.persist();
console.log(`Persisted storage granted: ${isPersisted}`);
}
}
export function formatBytes(bytes: number, decimals = 2) {
if (bytes === 0) return '0 B';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
}
async function getFile(fileEntry: FileSystemFileEntry) {
try {
return new Promise<File>((resolve, reject) => fileEntry.file(resolve, reject));
} catch (err) {
console.log(err);
}
}
export async function scanFiles(item: FileSystemEntry, files: Promise<File | undefined>[]) {
if (item.isDirectory) {
const directoryReader = (item as FileSystemDirectoryEntry).createReader();
await new Promise<void>((resolve) => {
directoryReader.readEntries((entries) => {
entries.forEach((entry) => {
if (entry.isFile) {
files.push(getFile(entry as FileSystemFileEntry))
resolve()
}
scanFiles(entry, files);
});
});
});
}
}