Skip to content

Commit 3b41aa6

Browse files
committed
解决多线程上传引起的授权失败问题
1 parent 20b8b79 commit 3b41aa6

File tree

5 files changed

+96
-62
lines changed

5 files changed

+96
-62
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dist
66
.idea
77
info
88
*.json
9-
*.exe
9+
*.exe
10+
od

Readme.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
- 支持 国际版, 个人版(家庭版), ~~中国版(世纪互联)~~.
7-
- ~~支持上传文件和文件夹到指定目录,并保持上传前的目录结构.~~
7+
- ~~支持上传文件和文件夹到指定目录,~~ 保持上传前的目录结构.
88
- 支持命令参数使用, 方便外部程序调用.
99
- 支持自定义上传分块大小.
1010
- 支持多线程上传(多文件同时上传).
@@ -36,7 +36,7 @@ OneDriveUploader -a "url" -v 2
3636
```
3737

3838
## 使用
39-
```bash
39+
```
4040
Usage of OneDriveUploader:
4141
-a string
4242
// 初始化授权
@@ -78,10 +78,16 @@ Usage of OneDriveUploader:
7878
// 跳过上传网盘中已存在的同名文件. (默认不跳过)
7979
Skip exist file on remote.
8080
81+
-tgbot string
82+
//使用Telegram机器人实时监控上传,此处需填写机器人的access token,形如123456789:xxxxxxxxx,输入时需使用双引号包裹
83+
-uid string
84+
// 使用Telegram机器人实时监控上传,此处需填写接收人的userID,形如123456789
85+
-v int
86+
// 选择版本,其中0为国际版,1为个人版(家庭版),默认为0
8187
```
8288

8389
## 配置
84-
```json
90+
```jsonc
8591
{
8692
// 授权令牌
8793
"RefreshToken": "1234564567890ABCDEF",
@@ -118,7 +124,7 @@ OneDriveUploader -c xxx.json -s "Download" -r "Test"
118124
OneDriveUploader -c xxx.json -t 10 -s "Download"
119125

120126
# 将同目录下的 Download 文件夹上传到 OneDrive 网盘根目录中, 使用 10 线程,同时使用 Telegram Bot 实时监控上传进度
121-
OneDriveUploader -c xxx.json -t 10 -s "Download" -tgbot "bot123456:xxxxxxxx" -uid 123456789
127+
OneDriveUploader -c xxx.json -t 10 -s "Download" -tgbot "123456:xxxxxxxx" -uid 123456789
122128

123129
# 将同目录下的 Download 文件夹上传到 OneDrive 网盘根目录中, 使用 15 线程, 并设置分块大小为 20M
124130
OneDriveUploader -c xxx.json -t 15 -b 20 -s "Download"

api/restore/upload/onedriveRecoverableRestore.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7-
"net/http"
87
"main/fileutil"
8+
"net/http"
99
"runtime/debug"
1010
"strconv"
1111
"time"
@@ -16,11 +16,11 @@ const (
1616
uploadURLKey = "uploadUrl"
1717
)
1818

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) {
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{} {
2020
//1. Get recoverable upload session for the current file path 获取当前文件路径的可压缩上载会话
2121
uploadSessionData, err := rs.getUploadSession(userID, bearerToken, conflictOption, filePath)
2222
if err != nil {
23-
return nil, err
23+
log.Panicf("Failed to Restore :%v", err)
2424
}
2525

2626
//2. Get the upload url returned as a response from the recoverable upload session above. 从上面的可压缩上载会话获取作为响应返回的上载url。
@@ -29,7 +29,7 @@ func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, c
2929
//3. Get the startOffset list for the file 获取文件的startOffset列表
3030
startOffsetLst, err := fileutil.GetFileOffsetStash(filePath)
3131
if err != nil {
32-
return nil, err
32+
log.Panicf("Failed to Restore :%v", err)
3333
}
3434

3535
//4. Loop over the file start offset list to read files in chunk and upload in onedrive 在文件开始偏移量列表上循环以读取块中的文件并在onedrive中上载
@@ -52,20 +52,20 @@ func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, c
5252
//4a. Get the bytes for the file based on the offset 根据偏移量获取文件的字节数
5353
err := fileutil.GetFilePartInBytes(filePartInBytes, filePath, sOffset)
5454
if err != nil {
55-
return nil, err
55+
log.Panicf("Failed to Restore :%v", err)
5656
}
5757
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))
58+
sendMsg(fmt.Sprintf("正在向OneDrive账户 `%s` 上传 `%s` *『%d/%d』* \n速度:`%s/s` \n已耗时: `%d s`", username, filePath, i, len(startOffsetLst), byte2Readable(float64(fileutil.GetDefaultChunkSize())/float64(time.Now().UnixNano()-timeUnix)*float64(1000000000)), time.Now().Unix()-startTime))
5959
} else {
60-
sendMsg(fmt.Sprintf("正在向OneDrive账户 `%s` 上传 `%s` *『%d/%d』* 速度:`----` 已耗时: `%d s`", username, filePath, i, len(startOffsetLst), time.Now().Unix()-startTime))
60+
sendMsg(fmt.Sprintf("正在向OneDrive账户 `%s` 上传 `%s` *『%d/%d』* \n速度:`----` \n已耗时: `%d s`", username, filePath, i, len(startOffsetLst), time.Now().Unix()-startTime))
6161
}
6262

6363
timeUnix = time.Now().UnixNano()
6464
//3b. make a call to the upload url with the file part based on the offset. 使用基于偏移量的文件部分调用上载url。
6565
resp, err := rs.uploadFilePart(uploadURL, filePath, bearerToken, *filePartInBytes, sOffset, isLastChunk)
6666

6767
if err != nil {
68-
return nil, err
68+
log.Panicf("Failed to Restore :%v", err)
6969
}
7070
respMap := make(map[string]interface{})
7171
err = json.NewDecoder(resp.Body).Decode(&respMap)
@@ -80,7 +80,7 @@ func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, c
8080
debug.FreeOSMemory()
8181
}
8282
sendMsg("close")
83-
return uploadResp, nil
83+
return uploadResp
8484
}
8585

8686
//Returns the restore session url for part file upload

api/restore/upload/onedriveRestore.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8-
"net/http"
9-
"net/url"
108
"main/fileutil"
119
httpLocal "main/graph/net/http"
10+
"net/http"
11+
"net/url"
1212
)
1313

1414
const (
@@ -35,21 +35,20 @@ type RestoreService struct {
3535
//@bearerToken will be extracted as sent from the restore input xml
3636
//@filePath will be extracted from the file hierarchy the needs to be restored
3737
//@fileInfo it is the file info struct that contains the actual file reference and the size_type
38-
func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken string, conflictOption string, filePath string, fileInfo fileutil.FileInfo, sendMsg func(text string), locText func(text string) string, username string) (interface{}, error) {
38+
func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken string, conflictOption string, filePath string, fileInfo fileutil.FileInfo, sendMsg func(text string), locText func(text string) string, username string) interface{} {
3939
if fileInfo.SizeType == fileutil.SizeTypeLarge {
4040
//For Large file type use resemble onedrive upload API
4141
//log.Printf("Processing Large File: %s", filePath)
4242
sendMsg(fmt.Sprintf("文件: `%s` 开始上传至OneDrive\n账户:`%s`\n文件超过4MB,进入大文件通道", filePath, username))
4343
return rs.recoverableUpload(userId, bearerToken, conflictOption, filePath, fileInfo, sendMsg, locText, username)
4444
} else {
4545
//log.Printf("Processing Small File: %s", filePath)
46-
sendMsg(fmt.Sprintf("文件: `%s` 开始上传至OneDrive\n账户:`%s`\n文件小于4MB,进入小文件通道,上传中......", filePath, username))
46+
sendMsg(fmt.Sprintf("文件: `%s` 开始上传至OneDrive\n账户:`%s`\n文件小于4MB,进入小文件通道,上传中", filePath, username))
4747
uploadPath := fmt.Sprintf(simpleUploadPath, userId, filePath)
4848
req, err := rs.NewRequest("PUT", uploadPath, getSimpleUploadHeader(bearerToken), fileInfo.FileData)
4949
if err != nil {
50-
return nil, err
50+
log.Panicf("Failed to Restore :%v", err)
5151
}
52-
5352
//Handle query parameter for conflict resolution
5453
//The different values for @microsoft.graph.conflictBehavior= rename|replace|fail
5554
q := url.Values{}
@@ -59,8 +58,7 @@ func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken s
5958
//Execute the request
6059
resp, err := rs.Do(req)
6160
if err != nil {
62-
//Need to return a generic object from onedrive upload instead of response directly
63-
return nil, err
61+
log.Panicf("Failed to Restore :%v", err)
6462
}
6563
if resp.Body != nil {
6664
defer resp.Body.Close()
@@ -69,10 +67,10 @@ func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken s
6967
respMap := make(map[string]interface{})
7068
err = json.NewDecoder(resp.Body).Decode(&respMap)
7169
if err != nil {
72-
return nil, err
70+
log.Panicf("Failed to Restore :%v", err)
7371
}
7472
sendMsg("close")
75-
return respMap, nil
73+
return respMap
7674
}
7775

7876
}
@@ -83,7 +81,7 @@ func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken s
8381
//@userId will be extracted as sent from the restore input xml
8482
//@filePath will be extracted from the file hierarchy the needs to be restored
8583
//@fileInfo it is the file info struct that contains the actual file reference and the size_type
86-
func (rs *RestoreService) SimpleUploadToAlternateLoc(altUserId string, bearerToken string, conflictOption string, filePath string, fileInfo fileutil.FileInfo, sendMsg func(text string), locText func(text string) string, username string) (interface{}, error) {
84+
func (rs *RestoreService) SimpleUploadToAlternateLoc(altUserId string, bearerToken string, conflictOption string, filePath string, fileInfo fileutil.FileInfo, sendMsg func(text string), locText func(text string) string, username string) interface{} {
8785
if fileInfo.SizeType == fileutil.SizeTypeLarge {
8886
//For Large file type use resemble onedrive upload API
8987
return rs.recoverableUpload(altUserId, bearerToken, conflictOption, filePath, fileInfo, sendMsg, locText, username)
@@ -92,9 +90,8 @@ func (rs *RestoreService) SimpleUploadToAlternateLoc(altUserId string, bearerTok
9290
uploadPath := fmt.Sprintf(simpleUploadPath, altUserId, filePath)
9391
req, err := rs.NewRequest("PUT", uploadPath, getSimpleUploadHeader(bearerToken), fileInfo.FileData)
9492
if err != nil {
95-
return nil, err
93+
log.Panicf("Failed to Restore :%v", err)
9694
}
97-
9895
//Handle query parameter for conflict resolution
9996
//The different values for @microsoft.graph.conflictBehavior= rename|replace|fail
10097
q := url.Values{}
@@ -104,8 +101,7 @@ func (rs *RestoreService) SimpleUploadToAlternateLoc(altUserId string, bearerTok
104101
//Execute the request
105102
resp, err := rs.Do(req)
106103
if err != nil {
107-
//Need to return a generic object from onedrive upload instead of response directly
108-
return nil, err
104+
log.Panicf("Failed to Restore :%v", err)
109105
}
110106
if resp.Body != nil {
111107
defer resp.Body.Close()
@@ -114,9 +110,9 @@ func (rs *RestoreService) SimpleUploadToAlternateLoc(altUserId string, bearerTok
114110
respMap := make(map[string]interface{})
115111
err = json.NewDecoder(resp.Body).Decode(&respMap)
116112
if err != nil {
117-
return nil, err
113+
log.Panicf("Failed to Restore :%v", err)
118114
}
119-
return respMap, nil
115+
return respMap
120116
}
121117
}
122118

0 commit comments

Comments
 (0)