@@ -25,7 +25,7 @@ func ApplyForNewPass(url string, ms int) string {
2525 return httpLocal .NewPassCheck (url , ms )
2626}
2727
28- func Upload (infoPath string , filePath string , threads int , sendMsg func () (func (text string ), string , string ), locText func (text string ) string ) {
28+ func Upload (infoPath string , filePath string , targetFolder string , threads int , sendMsg func () (func (text string ), string , string ), locText func (text string ) string ) {
2929
3030 programPath , err := filepath .Abs (filepath .Dir (infoPath ))
3131 if err != nil {
@@ -59,7 +59,7 @@ func Upload(infoPath string, filePath string, threads int, sendMsg func() (func(
5959 } else {
6060 restore(restoreSrvc, fileInfoToUpload, threads)
6161 }*/
62- restore (restoreSrvc , fileInfoToUpload , threads , sendMsg , locText , infoPath )
62+ restore (restoreSrvc , fileInfoToUpload , targetFolder , threads , sendMsg , locText , infoPath )
6363 err = os .Chdir (oldDir )
6464 if err != nil {
6565 log .Panic (err )
@@ -70,7 +70,7 @@ func changeBlockSize(MB int) {
7070}
7171
7272//Restore to original location
73- func restore (restoreSrvc * upload.RestoreService , filesToRestore map [string ]fileutil.FileInfo , threads int , sendMsg func () (func (text string ), string , string ), locText func (text string ) string , infoPath string ) {
73+ func restore (restoreSrvc * upload.RestoreService , filesToRestore map [string ]fileutil.FileInfo , targetFolder string , threads int , sendMsg func () (func (text string ), string , string ), locText func (text string ) string , infoPath string ) {
7474 var wg sync.WaitGroup
7575 pool := make (chan struct {}, threads )
7676 for filePath , fileInfo := range filesToRestore {
@@ -96,7 +96,7 @@ func restore(restoreSrvc *upload.RestoreService, filesToRestore map[string]fileu
9696 temp (tip )
9797 userID , bearerToken := httpLocal .GetMyIDAndBearer (infoPath )
9898 username := strings .ReplaceAll (filepath .Base (infoPath ), ".json" , "" )
99- restoreSrvc .SimpleUploadToOriginalLoc (userID , bearerToken , "rename" , filePath , fileInfo , temp , locText , username )
99+ restoreSrvc .SimpleUploadToOriginalLoc (userID , bearerToken , "rename" , targetFolder , filePath , fileInfo , temp , locText , username )
100100
101101 //printResp(resp)
102102 defer temp ("close" )
@@ -119,7 +119,7 @@ func printResp(resp interface{}) {
119119}
120120
121121//Restore to Alternate location 还原到备用位置
122- func restoreToAltLoc (restoreSrvc * upload.RestoreService , filesToRestore map [string ]fileutil.FileInfo , sendMsg func () func (text string ), locText func (text string ) string , infoPath string ) {
122+ func restoreToAltLoc (restoreSrvc * upload.RestoreService , filesToRestore map [string ]fileutil.FileInfo , targetFolder string , sendMsg func () func (text string ), locText func (text string ) string , infoPath string ) {
123123 rootFolder := fileutil .GetAlternateRootFolder ()
124124 var wg sync.WaitGroup
125125 pool := make (chan struct {}, 10 )
@@ -137,7 +137,7 @@ func restoreToAltLoc(restoreSrvc *upload.RestoreService, filesToRestore map[stri
137137 us := ""
138138 userID , bearerToken := httpLocal .GetMyIDAndBearer (infoPath )
139139 //username := strings.ReplaceAll(filepath.Base(infoPath), ".json", "")
140- restoreSrvc .SimpleUploadToAlternateLoc (userID , bearerToken , "rename" , rootFilePath , fileItem , temp , locText , us )
140+ restoreSrvc .SimpleUploadToAlternateLoc (userID , bearerToken , "rename" , targetFolder , rootFilePath , fileItem , temp , locText , us )
141141
142142 }()
143143 wg .Wait ()
@@ -197,6 +197,18 @@ func botSend(botKey string, iuserID string, initText string) func(string) {
197197
198198 }
199199}
200+
201+ func DirSize (path string ) (int64 , error ) {
202+ var size int64
203+ err := filepath .Walk (path , func (_ string , info os.FileInfo , err error ) error {
204+ if ! info .IsDir () {
205+ size += info .Size ()
206+ }
207+ return err
208+ })
209+ return size , err
210+ }
211+
200212func main () {
201213 var codeURL string
202214 var configFile string
@@ -206,6 +218,7 @@ func main() {
206218 var iuserID string
207219 var ms int
208220 var block int
221+ var targetFolder string
209222 // StringVar用指定的名称、控制台参数项目、默认值、使用信息注册一个string类型flag,并将flag的值保存到p指向的变量
210223 flag .StringVar (& codeURL , "a" , "" , "通过登录 https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=ad5e65fd-856d-4356-aefc-537a9700c137&response_type=code&redirect_uri=http://localhost/onedrive-login&response_mode=query&scope=offline_access%20User.Read%20Files.ReadWrite.All 后跳转的网址(输入网址时需要使用双引号包裹网址)" )
211224 flag .IntVar (& ms , "v" , 0 , "选择版本,其中0为国际版,1为个人版(家庭版),默认为0" )
@@ -215,6 +228,7 @@ func main() {
215228 flag .IntVar (& block , "b" , 10 , "自定义上传分块大小, 可以提高网络吞吐量, 受限于磁盘性能和网络速度,默认为 10 (单位MB)" )
216229 flag .StringVar (& botKey , "tgbot" , "" , "使用Telegram机器人实时监控上传,此处需填写机器人的access token,形如123456789:xxxxxxxxx,输入时需使用双引号包裹" )
217230 flag .StringVar (& iuserID , "uid" , "" , "使用Telegram机器人实时监控上传,此处需填写接收人的userID,形如123456789" )
231+ flag .StringVar (& targetFolder , "r" , "" , "设置上传的目录" )
218232
219233 // 从arguments中解析注册的flag。必须在所有flag都注册好而未访问其值时执行。未注册却使用flag -help时,会返回ErrHelp。
220234
@@ -224,12 +238,18 @@ func main() {
224238 startTime := time .Now ().Unix ()
225239 writer := uilive .New ()
226240 writer .Start ()
241+ size , err := DirSize (folder )
242+ if err != nil {
243+ log .Panic (err )
244+ }
245+ _ , _ = fmt .Fprintf (writer , "`%s` 开始上传,大小: `%s`" , folder , fileutil .Byte2Readable (float64 (size )))
246+
227247 var sendMsg func (string )
228248 if botKey != "" && iuserID != "" {
229- sendMsg = botSend (botKey , iuserID , fmt .Sprintf ("`%s` 开始上传" , folder ))
249+ sendMsg = botSend (botKey , iuserID , fmt .Sprintf ("`%s` 开始上传,大小: `%s` " , folder , fileutil . Byte2Readable ( float64 ( size )) ))
230250 }
231251
232- Upload (strings .ReplaceAll (configFile , "\\ " , "/" ), strings .ReplaceAll (folder , "\\ " , "/" ), thread , func () (func (text string ), string , string ) {
252+ Upload (strings .ReplaceAll (configFile , "\\ " , "/" ), strings .ReplaceAll (folder , "\\ " , "/" ), targetFolder , thread , func () (func (text string ), string , string ) {
233253 if botKey != "" && iuserID != "" {
234254 return func (text string ) {
235255 _ , _ = fmt .Fprintf (writer , "%s\n " , text )
@@ -243,9 +263,11 @@ func main() {
243263 }, func (text string ) string {
244264 return oldFunc (text )
245265 })
246- _ , _ = fmt .Fprintf (writer , "%s上传完成,耗时 %d 秒\n " , folder , time .Now ().Unix ()- startTime )
266+ cost := time .Now ().Unix () - startTime
267+ speed := fileutil .Byte2Readable (float64 (size ) / float64 (cost ))
268+ _ , _ = fmt .Fprintf (writer , "%s上传完成,耗时 %d 秒,平均上传速度 %s/s\n " , folder , cost , speed )
247269 if botKey != "" && iuserID != "" {
248- sendMsg (fmt .Sprintf ("`%s`上传完成,耗时 %d 秒\n " , folder , time . Now (). Unix () - startTime ))
270+ sendMsg (fmt .Sprintf ("`%s`上传完成,耗时 %d 秒,平均上传速度 `%s/s` " , folder , cost , speed ))
249271 }
250272 } else {
251273 if codeURL == "" {
@@ -257,6 +279,7 @@ func main() {
257279
258280 // 打印
259281 //fmt.Printf("username=%v password=%v host=%v port=%v", username, password, host, port)
282+
260283}
261284
262285/*
0 commit comments