| 165 | } |
| 166 | |
| 167 | async list(): Promise<FileInfo[]> { |
| 168 | let folderPath = this.path; |
| 169 | |
| 170 | // Dropbox API 需要空字符串来表示根目录 |
| 171 | if (folderPath === "/" || folderPath === "") { |
| 172 | folderPath = ""; |
| 173 | } |
| 174 | |
| 175 | const myHeaders = new Headers(); |
| 176 | myHeaders.append("Content-Type", "application/json"); |
| 177 | |
| 178 | let response = await this.request("https://api.dropboxapi.com/2/files/list_folder", { |
| 179 | method: "POST", |
| 180 | headers: myHeaders, |
| 181 | body: JSON.stringify({ |
| 182 | path: folderPath, |
| 183 | }), |
| 184 | }).catch((e) => { |
| 185 | if (e.message.includes("path/not_found")) { |
| 186 | return { entries: [], has_more: false }; // 返回空数组以避免后续错误 |
| 187 | } |
| 188 | throw e; |
| 189 | }); |
| 190 | |
| 191 | const list: FileInfo[] = []; |
| 192 | |
| 193 | const MAX_ITERATIONS = 100; |
| 194 | let iterationCount = 0; |
| 195 | |
| 196 | while (true) { |
| 197 | iterationCount++; |
| 198 | if (iterationCount > MAX_ITERATIONS) { |
| 199 | throw new Error("Dropbox list pagination exceeded maximum iterations"); |
| 200 | } |
| 201 | if (response.entries) { |
| 202 | for (const item of response.entries) { |
| 203 | // 只包含文件,跳过文件夹 |
| 204 | if (item[".tag"] === "file") { |
| 205 | list.push({ |
| 206 | name: item.name, |
| 207 | path: this.path, |
| 208 | size: item.size || 0, |
| 209 | digest: item.content_hash || "", |
| 210 | createtime: new Date(item.client_modified).getTime(), |
| 211 | updatetime: new Date(item.server_modified).getTime(), |
| 212 | }); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // 检查是否有更多数据 |
| 218 | if (!response.has_more) { |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | // 获取下一页数据 |
| 223 | response = await this.request("https://api.dropboxapi.com/2/files/list_folder/continue", { |
| 224 | method: "POST", |