| 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(); |
| 93 | myHeaders.append("Content-Range", `bytes 0-${size - 1}/${size}`); |
| 94 | return this.fs.request(uploadUrl, { |
| 95 | method: "PUT", |
| 96 | body: content, |
| 97 | headers: myHeaders, |
| 98 | }); |
| 99 | } |
| 100 | } |