| 102 | } |
| 103 | |
| 104 | async list(): Promise<FileInfo[]> { |
| 105 | const list: FileInfo[] = []; |
| 106 | let start = 0; |
| 107 | const limit = 200; |
| 108 | // 防御性:限制最大分页轮询次数,避免在 API 异常返回时出现无限循环 |
| 109 | const MAX_ITERATIONS = 100; |
| 110 | let iterations = 0; |
| 111 | |
| 112 | while (true) { |
| 113 | if (iterations >= MAX_ITERATIONS) { |
| 114 | throw new Error( |
| 115 | "BaiduFileSystem.list: exceeded max pagination iterations, possible infinite loop from Baidu API response" |
| 116 | ); |
| 117 | } |
| 118 | iterations += 1; |
| 119 | const data = await this.request( |
| 120 | `https://pan.baidu.com/rest/2.0/xpan/file?method=list&dir=${encodeURIComponent( |
| 121 | this.path |
| 122 | )}&order=time&start=${start}&limit=${limit}&access_token=${this.accessToken}` |
| 123 | ); |
| 124 | |
| 125 | if (data.errno) { |
| 126 | if (data.errno === -9) { |
| 127 | break; |
| 128 | } |
| 129 | throw new Error(JSON.stringify(data)); |
| 130 | } |
| 131 | |
| 132 | if (!data.list || data.list.length === 0) { |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | data.list.forEach((val: any) => { |
| 137 | list.push({ |
| 138 | fsid: val.fs_id, |
| 139 | name: val.server_filename, |
| 140 | path: this.path, |
| 141 | size: val.size, |
| 142 | digest: val.md5, |
| 143 | createtime: val.server_ctime * 1000, |
| 144 | updatetime: val.server_mtime * 1000, |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | // 如果返回的数据少于limit,说明已经是最后一页 |
| 149 | if (data.list.length < limit) { |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | start += limit; |
| 154 | } |
| 155 | |
| 156 | return list; |
| 157 | } |
| 158 | |
| 159 | async getDirUrl(): Promise<string> { |
| 160 | return `https://pan.baidu.com/disk/main#/index?category=all&path=${encodeURIComponent(this.path)}`; |