|
| 1 | +package upload |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "main/fileutil" |
| 9 | + "runtime/debug" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + uploadSessionPath = "/users/%s/drive/root:/%s:/createUploadSession" |
| 16 | + uploadURLKey = "uploadUrl" |
| 17 | +) |
| 18 | + |
| 19 | +func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, conflictOption string, filePath string, fileInfo fileutil.FileInfo, sendMsg func(text string), locText func(text string) string, username string) ([]map[string]interface{}, error) { |
| 20 | + //1. Get recoverable upload session for the current file path 获取当前文件路径的可压缩上载会话 |
| 21 | + uploadSessionData, err := rs.getUploadSession(userID, bearerToken, conflictOption, filePath) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + |
| 26 | + //2. Get the upload url returned as a response from the recoverable upload session above. 从上面的可压缩上载会话获取作为响应返回的上载url。 |
| 27 | + uploadURL := uploadSessionData[uploadURLKey].(string) |
| 28 | + |
| 29 | + //3. Get the startOffset list for the file 获取文件的startOffset列表 |
| 30 | + startOffsetLst, err := fileutil.GetFileOffsetStash(filePath) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + //4. Loop over the file start offset list to read files in chunk and upload in onedrive 在文件开始偏移量列表上循环以读取块中的文件并在onedrive中上载 |
| 36 | + var uploadResp []map[string]interface{} |
| 37 | + lastChunkIndex := len(startOffsetLst) - 1 |
| 38 | + var isLastChunk bool |
| 39 | + timeUnix := time.Now().UnixNano() |
| 40 | + var buffer = make([]byte, fileutil.GetDefaultChunkSize()) |
| 41 | + startTime := time.Now().Unix() |
| 42 | + for i, sOffset := range startOffsetLst { |
| 43 | + if i == lastChunkIndex { |
| 44 | + lastChunkSize, err := fileutil.GetLatsChunkSizeInBytes(filePath) |
| 45 | + if err != nil { |
| 46 | + log.Panic(err) |
| 47 | + } |
| 48 | + buffer = make([]byte, lastChunkSize) |
| 49 | + isLastChunk = true |
| 50 | + } |
| 51 | + filePartInBytes := &buffer |
| 52 | + //4a. Get the bytes for the file based on the offset 根据偏移量获取文件的字节数 |
| 53 | + err := fileutil.GetFilePartInBytes(filePartInBytes, filePath, sOffset) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + if i != 0 { |
| 58 | + sendMsg(fmt.Sprintf("正在向OneDrive账户 `%s` 上传 `%s` *『%d/%d』* 速度:`%s/s` 已耗时: `%d s`", username, filePath, i, len(startOffsetLst), byte2Readable(float64(fileutil.GetDefaultChunkSize())/float64(time.Now().UnixNano()-timeUnix)*float64(1000000000)), time.Now().Unix()-startTime)) |
| 59 | + } else { |
| 60 | + sendMsg(fmt.Sprintf("正在向OneDrive账户 `%s` 上传 `%s` *『%d/%d』* 速度:`----` 已耗时: `%d s`", username, filePath, i, len(startOffsetLst), time.Now().Unix()-startTime)) |
| 61 | + } |
| 62 | + |
| 63 | + timeUnix = time.Now().UnixNano() |
| 64 | + //3b. make a call to the upload url with the file part based on the offset. 使用基于偏移量的文件部分调用上载url。 |
| 65 | + resp, err := rs.uploadFilePart(uploadURL, filePath, bearerToken, *filePartInBytes, sOffset, isLastChunk) |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + respMap := make(map[string]interface{}) |
| 71 | + err = json.NewDecoder(resp.Body).Decode(&respMap) |
| 72 | + if err != nil { |
| 73 | + fmt.Println(err) |
| 74 | + } |
| 75 | + if resp.Body != nil { |
| 76 | + defer resp.Body.Close() |
| 77 | + } |
| 78 | + //fmt.Printf("%+v, status code: %s", respMap, resp.Status) |
| 79 | + uploadResp = append(uploadResp, respMap) |
| 80 | + debug.FreeOSMemory() |
| 81 | + } |
| 82 | + sendMsg("close") |
| 83 | + return uploadResp, nil |
| 84 | +} |
| 85 | + |
| 86 | +//Returns the restore session url for part file upload |
| 87 | +func (rs *RestoreService) getUploadSession(userID string, bearerToken string, conflictOption string, filePath string) (map[string]interface{}, error) { |
| 88 | + uploadSessionPath := fmt.Sprintf(uploadSessionPath, userID, filePath) |
| 89 | + uploadSessionData := make(map[string]interface{}) |
| 90 | + //Get the body for resemble upload session call. |
| 91 | + body, err := getRessumableSessionBody(filePath, conflictOption) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + //Create request instance |
| 97 | + req, err := rs.NewRequest("POST", uploadSessionPath, getRessumableUploadSessionHeader(bearerToken), body) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + //Execute the request |
| 102 | + resp, err := rs.Do(req) |
| 103 | + if err != nil { |
| 104 | + //Need to return a generic object from onedrive upload instead of response directly |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + //convert http.Response to map |
| 109 | + err = json.NewDecoder(resp.Body).Decode(&uploadSessionData) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + return uploadSessionData, nil |
| 114 | +} |
| 115 | + |
| 116 | +//Uploads the file part to Onedrive |
| 117 | +func (rs *RestoreService) uploadFilePart(uploadURL string, filePath string, bearerToken string, filePart []byte, startOffset int64, isLastPart bool) (*http.Response, error) { |
| 118 | + //This is required for Content-Range header key |
| 119 | + fileSizeInBytes, err := fileutil.GetFileSize(filePath) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + //Fetch Last chunklength -- will be needed in Content_length header |
| 125 | + lastChunkLength, err := fileutil.GetLatsChunkSizeInBytes(filePath) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + //Create upload part file request |
| 131 | + req, err := rs.NewRequest("PUT", uploadURL, getRessumableUploadHeader(fileSizeInBytes, bearerToken, startOffset, isLastPart, lastChunkLength), filePart) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + //Execute the request |
| 137 | + resp, err := rs.Do(req) |
| 138 | + if err != nil { |
| 139 | + //Need to return a generic object from onedrive upload instead of response directly |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + return resp, nil |
| 143 | +} |
| 144 | + |
| 145 | +//Returns header for upload session API |
| 146 | +func getRessumableUploadSessionHeader(accessToken string) map[string]string { |
| 147 | + //As a work around for now, ultimately this will be recived as a part of restore xml |
| 148 | + bearerToken := fmt.Sprintf("bearer %s", accessToken) |
| 149 | + return map[string]string{ |
| 150 | + "Content-Type": "application/json", |
| 151 | + "Authorization": bearerToken, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +//Returns headers for recoverable actual upload as file parts |
| 156 | +func getRessumableUploadHeader(fileSizeInBytes int64, accessToken string, startOffset int64, isLastChunk bool, lastChunkSize int64) map[string]string { |
| 157 | + var cRange string |
| 158 | + var cLength string |
| 159 | + |
| 160 | + if isLastChunk { |
| 161 | + cRange = fmt.Sprintf("bytes %d-%d/%d", startOffset, fileSizeInBytes-2, fileSizeInBytes-1) |
| 162 | + cLength = fmt.Sprintf("%d", lastChunkSize) |
| 163 | + } else { |
| 164 | + cRange = fmt.Sprintf("bytes %d-%d/%d", startOffset, startOffset+fileutil.GetDefaultChunkSize()-1, fileSizeInBytes-1) |
| 165 | + cLength = fmt.Sprintf("%d", fileutil.GetDefaultChunkSize()) |
| 166 | + } |
| 167 | + |
| 168 | + // fmt.Printf("\nCLength: %s , cRange: %s\n", cLength, cRange) |
| 169 | + bearerToken := fmt.Sprintf("bearer %s", accessToken) |
| 170 | + return map[string]string{ |
| 171 | + "Content-Length": cLength, |
| 172 | + "Content-Range": cRange, |
| 173 | + "Authorization": bearerToken, |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +//Returns the expected body for creating file upload session to onedrive |
| 178 | +func getRessumableSessionBody(filePath string, conflictOption string) (string, error) { |
| 179 | + bodyMap := map[string]string{"@microsoft.graph.conflictBehavior": conflictOption, "description": "", "name": filePath} |
| 180 | + jsonBody, err := json.Marshal(bodyMap) |
| 181 | + return string(jsonBody), err |
| 182 | +} |
| 183 | + |
| 184 | +func byte2Readable(bytes float64) string { |
| 185 | + const kb float64 = 1024 |
| 186 | + const mb float64 = kb * 1024 |
| 187 | + const gb float64 = mb * 1024 |
| 188 | + var readable float64 |
| 189 | + var unit string |
| 190 | + _bytes := bytes |
| 191 | + |
| 192 | + if _bytes >= gb { |
| 193 | + // xx GB |
| 194 | + readable = _bytes / gb |
| 195 | + unit = "GB" |
| 196 | + } else if _bytes < gb && _bytes >= mb { |
| 197 | + // xx MB |
| 198 | + readable = _bytes / mb |
| 199 | + unit = "MB" |
| 200 | + } else { |
| 201 | + // xx KB |
| 202 | + readable = _bytes / kb |
| 203 | + unit = "KB" |
| 204 | + } |
| 205 | + return strconv.FormatFloat(readable, 'f', 2, 64) + " " + unit |
| 206 | +} |
0 commit comments