| 33 | } |
| 34 | |
| 35 | export class OneDriveFileWriter implements FileWriter { |
| 36 | path: string; |
| 37 | |
| 38 | fs: OneDriveFileSystem; |
| 39 | |
| 40 | constructor(fs: OneDriveFileSystem, path: string) { |
| 41 | this.fs = fs; |
| 42 | this.path = path; |
| 43 | } |
| 44 | |
| 45 | size(content: string | Blob) { |
| 46 | if (content instanceof Blob) { |
| 47 | return content.size; |
| 48 | } |
| 49 | return new Blob([content]).size; |
| 50 | } |
| 51 | |
| 52 | async md5(content: string | Blob) { |
| 53 | if (content instanceof Blob) { |
| 54 | return calculateMd5(content); |
| 55 | } |
| 56 | return md5OfText(content); |
| 57 | } |
| 58 | |
| 59 | async write(content: string | Blob): Promise<void> { |
| 60 | // 预上传获取id |
| 61 | const size = this.size(content); |
| 62 | if (size === 0) { |
| 63 | return this.fs.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.path}:/content`, { |
| 64 | method: "PUT", |
| 65 | body: content, |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | let myHeaders = new Headers(); |
| 70 | myHeaders.append("Content-Type", "application/json"); |
| 71 | const uploadUrl = await this.fs |
| 72 | .request(`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.path}:/createUploadSession`, { |
| 73 | method: "POST", |
| 74 | headers: myHeaders, |
| 75 | body: JSON.stringify({ |
| 76 | item: { |
| 77 | "@microsoft.graph.conflictBehavior": "replace", |
| 78 | // description: "description", |
| 79 | // fileSystemInfo: { |
| 80 | // "@odata.type": "microsoft.graph.fileSystemInfo", |
| 81 | // }, |
| 82 | // name: this.path.substring(this.path.lastIndexOf("/") + 1), |
| 83 | }, |
| 84 | }), |
| 85 | }) |
| 86 | .then((data) => { |
| 87 | if (data.error) { |
| 88 | throw new Error(JSON.stringify(data)); |
| 89 | } |
| 90 | return data.uploadUrl; |
| 91 | }); |
| 92 | myHeaders = new Headers(); |
nothing calls this directly
no outgoing calls
no test coverage detected