Skip to content

Commit

Permalink
Better output mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanliang committed Jan 4, 2022
1 parent 1f0addf commit 66933b1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
10 changes: 7 additions & 3 deletions api/restore/upload/onedriveRecoverableRestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, c
//3b. make a call to the upload url with the file part based on the offset. 使用基于偏移量的文件部分调用上载url。
var resp *http.Response
for errCount := 1; errCount < 10; errCount++ {
bearerToken = http2.GetBearer() //解决长时下载时,Bearer超时的问题
bearerToken = http2.GetBearer() //解决长时下载时,Bearer超时的问题,这里采用超时一次就重新获取一次token的方案
resp, err = rs.uploadFilePart(uploadURL, filePath, bearerToken, *filePartInBytes, sOffset, isLastChunk)
if err != nil {
sendMsg(fmt.Sprintf(locText("failToLink"), username, filePath, errCount))
sendMsg("close|" + fmt.Sprintf(locText("failToLink"), username, filePath, errCount))
// close 用作输出时定位,带有 close 在输出时不会被刷新走
// close= 表示文件传输结束,此时会同步删除tg发出的消息
// close| 则不会删除消息
} else {
break
}
Expand All @@ -99,7 +102,8 @@ func (rs *RestoreService) recoverableUpload(userID string, bearerToken string, c
uploadResp = append(uploadResp, respMap)
debug.FreeOSMemory()
}
sendMsg("close")

sendMsg("close=" + fmt.Sprintf(fmt.Sprintf(locText("completeUpload"), filePath, time.Now().Unix()-startTime, byte2Readable(float64(_size)/float64(time.Now().UnixNano()-timeUnix)*float64(1000000000)))))
return uploadResp
}

Expand Down
7 changes: 5 additions & 2 deletions api/restore/upload/onedriveRestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"path/filepath"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -74,7 +75,8 @@ func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken s
//log.Printf("Processing Small File: %s", filePath)
sendMsg(fmt.Sprintf(locText("oneDriveSmallFile"), filePath, username))
targetPath := strings.ReplaceAll(filepath.Join(targetFolder, filePath), "\\", "/")

startTime := time.Now().Unix()
_size, err := fileutil.GetFileSize(filePath)
uploadPath := fmt.Sprintf(simpleUploadPath, userId, targetPath)
req, err := rs.NewRequest("PUT", uploadPath, getSimpleUploadHeader(bearerToken), fileInfo.FileData)
if err != nil {
Expand Down Expand Up @@ -111,7 +113,8 @@ func (rs *RestoreService) SimpleUploadToOriginalLoc(userId string, bearerToken s
if err != nil {
log.Panicf(locText("failToStore"), err)
}
sendMsg("close")
timeUnix := time.Now().UnixNano()
sendMsg("close=" + fmt.Sprintf(fmt.Sprintf(locText("completeUpload"), filePath, time.Now().Unix()-startTime, byte2Readable(float64(_size)/float64(time.Now().UnixNano()-timeUnix)*float64(1000000000)))))
return respMap
}

Expand Down
1 change: 1 addition & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ googleDriveGetAccess = "Please open the following website, log in to Google acco
googleDriveOAuthFileCreateSuccess = "Google Drive OAuth2 file created successfully, save to:"
googleDriveUploadTip = "Google Drive account `%s` \n Uploading `%s` Time: %d s"
googleDriveUploadTip1 = "Google Drive account `%s` \nUploading `%s`\nSize:`%s` Complete: `%s` Progress: *『%d/%d』* \nSpeed:`%s` \nTime: `%d s`"
noGoogleDriveInfo = "No Google Drive upload configuration, do you need to create a new configuration?"
oneDriveBigFile = "File: `%s` Start upload to OneDrive\nAccounts:`%s`\nFile over 4MB, go to large file channel"
oneDriveSmallFile = "File: `%s` Start upload to OneDrive\nAccounts:`%s`\nFile less than 4MB, go to small file channel, uploading in progress"
oneDriveUploadTip1 = "OneDrive account `%s` \nUploading `%s`\nSize:`%s` Complete: `%s` Progress: *『%d/%d』* \nSpeed:`%s/s` \nTime: `%d s`"
Expand Down
2 changes: 1 addition & 1 deletion i18n/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ googleDriveGetAccess = "请打开下面的网址,登录Google账户后,根
googleDriveOAuthFileCreateSuccess = "Google Drive OAuth2文件创建成功,保存至:"
googleDriveUploadTip = "正在向Google Drive账户 `%s` 上传 `%s` 已耗时: %d s"
googleDriveUploadTip1 = "正在向Google Drive账户 `%s` 上传 `%s`\n大小:`%s` 已上传大小: `%s` 进度: *『%d/%d』* \n速度:`%s` \n已耗时: `%d s`"
noGoogleDriveInfo = "没有Google Drive上传配置,是否需要新建配置"
noGoogleDriveInfo = "没有Google Drive上传配置,是否需要新建配置"
oneDriveBigFile = "文件: `%s` 开始上传至OneDrive\n账户:`%s`\n文件超过4MB,进入大文件通道"
oneDriveSmallFile = "文件: `%s` 开始上传至OneDrive\n账户:`%s`\n文件小于4MB,进入小文件通道,上传中"
oneDriveUploadTip1 = "正在向OneDrive账户 `%s` 上传 `%s`\n大小:`%s` 已上传大小: `%s` 进度: *『%d/%d』* \n速度:`%s/s` \n已耗时: `%d s`"
Expand Down
65 changes: 36 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -43,6 +44,11 @@ func Upload(infoPath string, filePath string, targetFolder string, threads int,
infoPath = filepath.Join(programPath, infoPath)

// restoreOption := "orig"
pathLastChar := filePath[len(filePath)-1]
if pathLastChar == '/' || pathLastChar == '\\' { // 当最后一位是/时,可能会出现找不到文件的情况,这里进行首先处理
filePath = filePath[:len(filePath)-1]
}

oldDir, err := os.Getwd()
if err != nil {
log.Panic(err)
Expand Down Expand Up @@ -85,10 +91,15 @@ func restore(restoreSrvc *upload.RestoreService, filesToRestore map[string]fileu
checkPath := make(map[string]bool, 0)
pathFiles := make(map[string]map[string]bool, 0)

for filePath, fileInfo := range filesToRestore {
filePaths := make([]string, 0, len(filesToRestore))
for k := range filesToRestore {
filePaths = append(filePaths, k)
}
sort.Sort(sort.StringSlice(filePaths)) //将文件按照文件名顺序排序
for _, filePath := range filePaths {
wg.Add(1)
pool <- struct{}{}

fileInfo := filesToRestore[filePath]
paths, fileName := filepath.Split(filepath.Join(targetFolder, filePath))
if mode == 1 {
if paths == "" {
Expand Down Expand Up @@ -188,7 +199,7 @@ func oldFunc(a string) string {
}

func botSend(botKey string, iuserID string, initText string) func(string) {
var message_id = int64(0)
var messageId = int64(0)
resp, err := http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&parse_mode=MarkdownV2&text=%s", botKey, iuserID, url.QueryEscape(initText)))
if err != nil {
log.Panic(err)
Expand All @@ -201,21 +212,24 @@ func botSend(botKey string, iuserID string, initText string) func(string) {
//fmt.Println(string(body))
ok, _ := jsonparser.GetBoolean(body, "ok")
if ok {
message_id, _ = jsonparser.GetInt(body, "result", "message_id")
messageId, _ = jsonparser.GetInt(body, "result", "message_id")
} else {
description, _ := jsonparser.GetString(body, "description")
log.Panicf(loc.print("telegramSendError"), description)
}
return func(text string) {
if text == "close" {
resp, err = http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%s&message_id=%d", botKey, iuserID, message_id))
if text[:5] == "close" && text[5] != '|' {
// msg 头部的 close 用作输出时定位,带有 close 在输出时不会被刷新走
// close= 表示文件传输结束,此时会同步删除tg发出的消息
// close| 则不会删除消息
resp, err = http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%s&message_id=%d", botKey, iuserID, messageId))
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
return
}
resp, err = http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText?chat_id=%s&parse_mode=MarkdownV2&message_id=%d&text=%s", botKey, iuserID, message_id, url.QueryEscape(text)))
resp, err = http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText?chat_id=%s&parse_mode=MarkdownV2&message_id=%d&text=%s", botKey, iuserID, messageId, url.QueryEscape(text)))
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -334,6 +348,14 @@ func main() {

_, _ = fmt.Fprintf(writer, loc.print("startToUpload"), folder, fileutil.Byte2Readable(float64(size)))

updateOutput := func(text string) {
if text[:5] != "close" {
_, _ = fmt.Fprintf(writer.Newline(), "%s\n", text)
} else {
_, _ = fmt.Fprintf(writer.Bypass(), "%s\n", text[6:])
}
}

var sendMsg func(string)
if botKey != "" && _UserID != "" {
sendMsg = botSend(botKey, _UserID, fmt.Sprintf(loc.print("startToUpload"), folder, fileutil.Byte2Readable(float64(size))))
Expand All @@ -342,35 +364,19 @@ func main() {
case "OneDrive":
Upload(strings.ReplaceAll(configFile, "\\", "/"), strings.ReplaceAll(folder, "\\", "/"), targetFolder, thread, func() (func(text string), string, string) {
if botKey != "" && _UserID != "" {
return func(text string) {
if text != "close" {
_, _ = fmt.Fprintf(writer, "%s\n", text)
}
}, botKey, _UserID
return updateOutput, botKey, _UserID
} else {
return func(text string) {
if text != "close" {
_, _ = fmt.Fprintf(writer, "%s\n", text)
}
}, "", ""
return updateOutput, "", ""
}
}, func(text string) string {
return loc.print(text)
})
case "GoogleDrive":
googledrive.Upload(strings.ReplaceAll(configFile, "\\", "/"), strings.ReplaceAll(folder, "\\", "/"), func() (func(text string), string, string, func(string, string, string) func(string)) {
if botKey != "" && _UserID != "" {
return func(text string) {
if text != "close" {
_, _ = fmt.Fprintf(writer, "%s\n", text)
}
}, botKey, _UserID, botSend
return updateOutput, botKey, _UserID, botSend
} else {
return func(text string) {
if text != "close" {
_, _ = fmt.Fprintf(writer, "%s\n", text)
}
}, "", "", nil
return updateOutput, "", "", nil
}
}, func(text string) string {
return loc.print(text)
Expand All @@ -380,8 +386,9 @@ func main() {

cost := time.Now().Unix() - startTime
speed := fileutil.Byte2Readable(float64(size) / float64(cost))
_, _ = fmt.Fprintf(writer, loc.print("completeUpload"), folder, cost, speed)
_, _ = fmt.Fprintf(writer.Bypass(), loc.print("completeUpload"), folder, cost, speed)
if botKey != "" && _UserID != "" {
log.Printf(fmt.Sprintf(loc.print("completeUpload"), folder, cost, speed))
sendMsg(fmt.Sprintf(loc.print("completeUpload"), folder, cost, speed))
}
} else {
Expand Down Expand Up @@ -412,7 +419,7 @@ func main() {

/*
SET CGO_ENABLED=0
SET GOOS=linux
$env:GOOS="linux"
SET GOARCH=amd64
go build -o LightUploader .
/usr/local/bin
Expand Down

0 comments on commit 66933b1

Please sign in to comment.