| 3 | import type DropboxFileSystem from "./dropbox"; |
| 4 | |
| 5 | export class DropboxFileReader implements FileReader { |
| 6 | file: FileInfo; |
| 7 | |
| 8 | fs: DropboxFileSystem; |
| 9 | |
| 10 | constructor(fs: DropboxFileSystem, file: FileInfo) { |
| 11 | this.fs = fs; |
| 12 | this.file = file; |
| 13 | } |
| 14 | |
| 15 | async read(type?: "string" | "blob"): Promise<string | Blob> { |
| 16 | const filePath = joinPath(this.file.path, this.file.name); |
| 17 | |
| 18 | const myHeaders = new Headers(); |
| 19 | myHeaders.append("Content-Type", "application/octet-stream"); |
| 20 | myHeaders.append( |
| 21 | "Dropbox-API-Arg", |
| 22 | JSON.stringify({ |
| 23 | path: filePath, |
| 24 | }) |
| 25 | ); |
| 26 | |
| 27 | // 获取文件内容 |
| 28 | const data = await this.fs.request( |
| 29 | "https://content.dropboxapi.com/2/files/download", |
| 30 | { |
| 31 | method: "POST", |
| 32 | headers: myHeaders, |
| 33 | }, |
| 34 | true |
| 35 | ); |
| 36 | |
| 37 | if (data.status !== 200) { |
| 38 | return Promise.reject(await data.text()); |
| 39 | } |
| 40 | |
| 41 | switch (type) { |
| 42 | case "string": |
| 43 | return data.text(); |
| 44 | default: { |
| 45 | return data.blob(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export class DropboxFileWriter implements FileWriter { |
| 52 | path: string; |
nothing calls this directly
no outgoing calls
no test coverage detected