(item, path, files)
| 10 | * @param {Array} files - Collected files list |
| 11 | */ |
| 12 | export async function traverseFileTree(item, path, files) { |
| 13 | if (item.isFile) { |
| 14 | return new Promise((resolve, reject) => { |
| 15 | item.file((file) => { |
| 16 | console.log(`Found file: ${file.name} (${file.type}, ${file.size} bytes)`); |
| 17 | files.push({ file, path: path + file.name }); |
| 18 | resolve(); |
| 19 | }, (error) => { |
| 20 | console.warn(`Failed to read file: ${item.name}`, error); |
| 21 | reject(error); |
| 22 | }); |
| 23 | }); |
| 24 | } else if (item.isDirectory) { |
| 25 | const dirReader = item.createReader(); |
| 26 | const newPath = path + item.name + '/'; |
| 27 | |
| 28 | console.log(`Entering directory: ${item.name}`); |
| 29 | |
| 30 | const readAllEntries = () => { |
| 31 | return new Promise((resolve, reject) => { |
| 32 | const allEntries = []; |
| 33 | |
| 34 | const readBatch = () => { |
| 35 | dirReader.readEntries((entries) => { |
| 36 | if (entries.length > 0) { |
| 37 | allEntries.push(...entries); |
| 38 | readBatch(); |
| 39 | } else { |
| 40 | resolve(allEntries); |
| 41 | } |
| 42 | }, (error) => { |
| 43 | console.warn(`Failed to read directory: ${item.name}`, error); |
| 44 | reject(error); |
| 45 | }); |
| 46 | }; |
| 47 | |
| 48 | readBatch(); |
| 49 | }); |
| 50 | }; |
| 51 | |
| 52 | try { |
| 53 | const entries = await readAllEntries(); |
| 54 | console.log(`Directory ${item.name} contains ${entries.length} entries`); |
| 55 | |
| 56 | // Use Promise.all to process entries concurrently |
| 57 | const promises = entries.map(entry => traverseFileTree(entry, newPath, files)); |
| 58 | await Promise.all(promises); |
| 59 | } catch (error) { |
| 60 | console.warn(`Error processing directory ${item.name}:`, error); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Enable global drag-and-drop support |
no test coverage detected