| 49 | } |
| 50 | |
| 51 | export class DropboxFileWriter implements FileWriter { |
| 52 | path: string; |
| 53 | |
| 54 | fs: DropboxFileSystem; |
| 55 | |
| 56 | constructor(fs: DropboxFileSystem, path: string) { |
| 57 | this.fs = fs; |
| 58 | this.path = path; |
| 59 | } |
| 60 | |
| 61 | async write(content: string | Blob): Promise<void> { |
| 62 | // 检查文件是否存在 |
| 63 | const exists = await this.fs.exists(this.path); |
| 64 | |
| 65 | if (exists) { |
| 66 | // 如果文件存在,则更新 |
| 67 | return this.updateFile(content); |
| 68 | } else { |
| 69 | // 如果文件不存在,则创建 |
| 70 | return this.createNewFile(content); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private async updateFile(content: string | Blob): Promise<void> { |
| 75 | const myHeaders = new Headers(); |
| 76 | myHeaders.append("Content-Type", "application/octet-stream"); |
| 77 | myHeaders.append( |
| 78 | "Dropbox-API-Arg", |
| 79 | JSON.stringify({ |
| 80 | path: this.path, |
| 81 | mode: "overwrite", |
| 82 | autorename: false, |
| 83 | }) |
| 84 | ); |
| 85 | |
| 86 | await this.fs.request("https://content.dropboxapi.com/2/files/upload", { |
| 87 | method: "POST", |
| 88 | headers: myHeaders, |
| 89 | body: content instanceof Blob ? content : new Blob([content]), |
| 90 | }); |
| 91 | |
| 92 | return Promise.resolve(); |
| 93 | } |
| 94 | |
| 95 | private async createNewFile(content: string | Blob): Promise<void> { |
| 96 | const myHeaders = new Headers(); |
| 97 | myHeaders.append("Content-Type", "application/octet-stream"); |
| 98 | myHeaders.append( |
| 99 | "Dropbox-API-Arg", |
| 100 | JSON.stringify({ |
| 101 | path: this.path, |
| 102 | mode: "add", |
| 103 | autorename: false, |
| 104 | }) |
| 105 | ); |
| 106 | |
| 107 | await this.fs.request("https://content.dropboxapi.com/2/files/upload", { |
| 108 | method: "POST", |
nothing calls this directly
no outgoing calls
no test coverage detected