(item, path, files)
| 92 | } |
| 93 | |
| 94 | async function traverseFileTree(item, path, files) { |
| 95 | if (item.isFile) { |
| 96 | return new Promise((resolve, reject) => { |
| 97 | item.file((file) => { |
| 98 | console.log(`找到文件: ${file.name} (${file.type}, ${file.size} bytes)`); |
| 99 | files.push({ file, path: path + file.name }); |
| 100 | resolve(); |
| 101 | }, (error) => { |
| 102 | console.warn(`读取文件失败: ${item.name}`, error); |
| 103 | reject(error); |
| 104 | }); |
| 105 | }); |
| 106 | } else if (item.isDirectory) { |
| 107 | if (!folderName) { |
| 108 | folderName = item.name; |
| 109 | } |
| 110 | const dirReader = item.createReader(); |
| 111 | const newPath = path + item.name + '/'; |
| 112 | |
| 113 | console.log(`进入目录: ${item.name}`); |
| 114 | |
| 115 | // readEntries must be called multiple times to fetch all files |
| 116 | const readAllEntries = () => { |
| 117 | return new Promise((resolve, reject) => { |
| 118 | const allEntries = []; |
| 119 | |
| 120 | const readBatch = () => { |
| 121 | dirReader.readEntries((entries) => { |
| 122 | if (entries.length > 0) { |
| 123 | console.log('Read batch of', entries.length, 'entries from', newPath); |
| 124 | allEntries.push(...entries); |
| 125 | readBatch(); |
| 126 | } else { |
| 127 | console.log('Finished reading directory:', newPath, 'Total entries:', allEntries.length); |
| 128 | resolve(allEntries); |
| 129 | } |
| 130 | }, (error) => { |
| 131 | console.warn(`读取目录失败: ${item.name}`, error); |
| 132 | reject(error); |
| 133 | }); |
| 134 | }; |
| 135 | |
| 136 | readBatch(); |
| 137 | }); |
| 138 | }; |
| 139 | |
| 140 | try { |
| 141 | const entries = await readAllEntries(); |
| 142 | console.log(`目录 ${item.name} 包含 ${entries.length} 个项目`); |
| 143 | |
| 144 | // Use Promise.all to handle entries concurrently |
| 145 | const promises = entries.map(entry => traverseFileTree(entry, newPath, files)); |
| 146 | await Promise.all(promises); |
| 147 | } catch (error) { |
| 148 | console.warn(`处理目录 ${item.name} 时出错:`, error); |
| 149 | } |
| 150 | } |
| 151 | } |
no test coverage detected