| 58 | } |
| 59 | |
| 60 | async write(content: string | Blob): Promise<void> { |
| 61 | // 预上传获取id |
| 62 | const size = this.size(content).toString(); |
| 63 | const md5 = await this.md5(content); |
| 64 | const blockList: string[] = [md5]; |
| 65 | let urlencoded = new URLSearchParams(); |
| 66 | urlencoded.append("path", this.path); |
| 67 | urlencoded.append("size", size); |
| 68 | urlencoded.append("isdir", "0"); |
| 69 | urlencoded.append("autoinit", "1"); |
| 70 | urlencoded.append("rtype", "3"); |
| 71 | urlencoded.append("block_list", JSON.stringify(blockList)); |
| 72 | const myHeaders = new Headers(); |
| 73 | myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); |
| 74 | let data = await this.fs.request( |
| 75 | `http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${this.fs.accessToken}`, |
| 76 | { |
| 77 | method: "POST", |
| 78 | headers: myHeaders, |
| 79 | body: urlencoded, |
| 80 | } |
| 81 | ); |
| 82 | if (data.errno) { |
| 83 | throw new Error(JSON.stringify(data)); |
| 84 | } |
| 85 | const uploadid = data.uploadid; |
| 86 | const body = new FormData(); |
| 87 | if (content instanceof Blob) { |
| 88 | // 分片上传 |
| 89 | body.append("file", content); |
| 90 | } else { |
| 91 | body.append("file", new Blob([content])); |
| 92 | } |
| 93 | |
| 94 | data = await this.fs.request( |
| 95 | `${ |
| 96 | `https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&access_token=${this.fs.accessToken}` + |
| 97 | `&type=tmpfile&path=` |
| 98 | }${encodeURIComponent(this.path)}&uploadid=${uploadid}&partseq=0`, |
| 99 | { |
| 100 | method: "POST", |
| 101 | body, |
| 102 | } |
| 103 | ); |
| 104 | if (data.errno) { |
| 105 | throw new Error(JSON.stringify(data)); |
| 106 | } |
| 107 | // 创建文件 |
| 108 | urlencoded = new URLSearchParams(); |
| 109 | urlencoded.append("path", this.path); |
| 110 | urlencoded.append("size", size); |
| 111 | urlencoded.append("isdir", "0"); |
| 112 | urlencoded.append("block_list", JSON.stringify(blockList)); |
| 113 | urlencoded.append("uploadid", uploadid); |
| 114 | urlencoded.append("rtype", "3"); |
| 115 | data = await this.fs.request( |
| 116 | `https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.fs.accessToken}`, |
| 117 | { |