(entry: FileSystemEntry, path = "")
| 7 | } |
| 8 | |
| 9 | export async function scanDirectory(entry: FileSystemEntry, path = ""): Promise<ScanItem[]> { |
| 10 | const items: ScanItem[] = []; |
| 11 | |
| 12 | if (entry.isDirectory) { |
| 13 | const dirEntry = entry as FileSystemDirectoryEntry; |
| 14 | const currentDirPath = path + entry.name + "/"; |
| 15 | items.push({ path: currentDirPath, isDir: true }); |
| 16 | |
| 17 | const dirReader = dirEntry.createReader(); |
| 18 | |
| 19 | const readAll = async (): Promise<FileSystemEntry[]> => { |
| 20 | return new Promise((resolve) => { |
| 21 | const allResults: FileSystemEntry[] = []; |
| 22 | const read = () => { |
| 23 | dirReader.readEntries( |
| 24 | (results) => { |
| 25 | if (!results || results.length === 0) { |
| 26 | resolve(allResults); |
| 27 | } else { |
| 28 | allResults.push(...results); |
| 29 | read(); |
| 30 | } |
| 31 | }, |
| 32 | (err) => { |
| 33 | console.error("Directory read error:", err); |
| 34 | resolve(allResults); |
| 35 | } |
| 36 | ); |
| 37 | }; |
| 38 | read(); |
| 39 | }); |
| 40 | }; |
| 41 | |
| 42 | const entries = await readAll(); |
| 43 | for (const child of entries) { |
| 44 | const childItems = await scanDirectory(child, currentDirPath); |
| 45 | items.push(...childItems); |
| 46 | } |
| 47 | } else { |
| 48 | const file = await new Promise<File | null>((resolve) => { |
| 49 | (entry as FileSystemFileEntry).file( |
| 50 | (f) => resolve(f), |
| 51 | (err) => { |
| 52 | console.error("File read error:", err); |
| 53 | resolve(null); |
| 54 | } |
| 55 | ); |
| 56 | }); |
| 57 | if (file) { |
| 58 | items.push({ path: path + entry.name, file, isDir: false }); |
| 59 | } |
| 60 | } |
| 61 | return items; |
| 62 | } |
| 63 | |
| 64 | export async function compressFolder(items: ScanItem[]): Promise<Uint8Array> { |
| 65 | const zipData: AsyncZippable = {}; |
no test coverage detected