| 42 | } |
| 43 | |
| 44 | async createDir(dir: string, _opts?: FileCreateOptions): Promise<void> { |
| 45 | if (!dir) { |
| 46 | return Promise.resolve(); |
| 47 | } |
| 48 | |
| 49 | const fullPath = joinPath(this.path, dir).replace(/^\/ScriptCat/, ""); |
| 50 | if (!fullPath) { |
| 51 | return Promise.resolve(); |
| 52 | } |
| 53 | |
| 54 | // Dropbox 会自动创建父目录,所以我们只需要创建最终目录 |
| 55 | const myHeaders = new Headers(); |
| 56 | myHeaders.append("Content-Type", "application/json"); |
| 57 | |
| 58 | try { |
| 59 | await this.request("https://api.dropboxapi.com/2/files/create_folder_v2", { |
| 60 | method: "POST", |
| 61 | headers: myHeaders, |
| 62 | body: JSON.stringify({ |
| 63 | path: fullPath, |
| 64 | autorename: false, |
| 65 | }), |
| 66 | }); |
| 67 | |
| 68 | // 添加到缓存 |
| 69 | this.pathCache.add(fullPath); |
| 70 | } catch (error: any) { |
| 71 | // 如果目录已存在,Dropbox 会返回错误,但这是正常情况 |
| 72 | if (error.message && error.message.includes("path/conflict")) { |
| 73 | // 目录已存在,不需要报错 |
| 74 | this.pathCache.add(fullPath); |
| 75 | return; |
| 76 | } |
| 77 | throw error; |
| 78 | } |
| 79 | |
| 80 | return Promise.resolve(); |
| 81 | } |
| 82 | |
| 83 | request(url: string, config?: RequestInit, nothen?: boolean) { |
| 84 | config = config || {}; |