| 3 | import type BaiduFileSystem from "./baidu"; |
| 4 | |
| 5 | export class BaiduFileReader implements FileReader { |
| 6 | file: FileInfo; |
| 7 | |
| 8 | fs: BaiduFileSystem; |
| 9 | |
| 10 | constructor(fs: BaiduFileSystem, file: FileInfo) { |
| 11 | this.fs = fs; |
| 12 | this.file = file; |
| 13 | } |
| 14 | |
| 15 | async read(type?: "string" | "blob"): Promise<string | Blob> { |
| 16 | // 查询文件信息获取dlink |
| 17 | const data = await this.fs.request( |
| 18 | `https://pan.baidu.com/rest/2.0/xpan/multimedia?method=filemetas&access_token=${ |
| 19 | this.fs.accessToken |
| 20 | }&fsids=[${this.file.fsid!}]&dlink=1` |
| 21 | ); |
| 22 | if (!data.list.length) { |
| 23 | throw new Error("file not found"); |
| 24 | } |
| 25 | const resp = await fetch(`${data.list[0].dlink}&access_token=${this.fs.accessToken}`); |
| 26 | switch (type) { |
| 27 | case "string": |
| 28 | return await resp.text(); |
| 29 | default: { |
| 30 | return await resp.blob(); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export class BaiduFileWriter implements FileWriter { |
| 37 | path: string; |
nothing calls this directly
no outgoing calls
no test coverage detected