@@ -534,16 +534,15 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
534
534
if size > partSize {
535
535
part = (size + partSize - 1 ) / partSize
536
536
}
537
+
538
+ // 生成所有 partInfos
537
539
partInfos := make ([]PartInfo , 0 , part )
538
540
for i := int64 (0 ); i < part ; i ++ {
539
541
if utils .IsCanceled (ctx ) {
540
542
return ctx .Err ()
541
543
}
542
544
start := i * partSize
543
- byteSize := size - start
544
- if byteSize > partSize {
545
- byteSize = partSize
546
- }
545
+ byteSize := min (size - start , partSize )
547
546
partNumber := i + 1
548
547
partInfo := PartInfo {
549
548
PartNumber : partNumber ,
@@ -591,17 +590,20 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
591
590
// resp.Data.RapidUpload: true 支持快传,但此处直接检测是否返回分片的上传地址
592
591
// 快传的情况下同样需要手动处理冲突
593
592
if resp .Data .PartInfos != nil {
594
- // 读取前100个分片的上传地址
595
- uploadPartInfos := resp .Data .PartInfos
596
-
597
- // 获取后续分片的上传地址
598
- for i := 101 ; i < len (partInfos ); i += 100 {
599
- end := i + 100
600
- if end > len (partInfos ) {
601
- end = len (partInfos )
602
- }
603
- batchPartInfos := partInfos [i :end ]
593
+ // Progress
594
+ p := driver .NewProgress (size , up )
595
+ rateLimited := driver .NewLimitedUploadStream (ctx , stream )
596
+
597
+ // 先上传前100个分片
598
+ err = d .uploadPersonalParts (ctx , partInfos , resp .Data .PartInfos , rateLimited , p )
599
+ if err != nil {
600
+ return err
601
+ }
604
602
603
+ // 如果还有剩余分片,分批获取上传地址并上传
604
+ for i := 100 ; i < len (partInfos ); i += 100 {
605
+ end := min (i + 100 , len (partInfos ))
606
+ batchPartInfos := partInfos [i :end ]
605
607
moredata := base.Json {
606
608
"fileId" : resp .Data .FileId ,
607
609
"uploadId" : resp .Data .UploadId ,
@@ -617,44 +619,13 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
617
619
if err != nil {
618
620
return err
619
621
}
620
- uploadPartInfos = append (uploadPartInfos , moreresp .Data .PartInfos ... )
621
- }
622
-
623
- // Progress
624
- p := driver .NewProgress (size , up )
625
-
626
- rateLimited := driver .NewLimitedUploadStream (ctx , stream )
627
- // 上传所有分片
628
- for _ , uploadPartInfo := range uploadPartInfos {
629
- index := uploadPartInfo .PartNumber - 1
630
- partSize := partInfos [index ].PartSize
631
- log .Debugf ("[139] uploading part %+v/%+v" , index , len (uploadPartInfos ))
632
- limitReader := io .LimitReader (rateLimited , partSize )
633
-
634
- // Update Progress
635
- r := io .TeeReader (limitReader , p )
636
-
637
- req , err := http .NewRequestWithContext (ctx , http .MethodPut , uploadPartInfo .UploadUrl , r )
638
- if err != nil {
639
- return err
640
- }
641
- req .Header .Set ("Content-Type" , "application/octet-stream" )
642
- req .Header .Set ("Content-Length" , fmt .Sprint (partSize ))
643
- req .Header .Set ("Origin" , "https://yun.139.com" )
644
- req .Header .Set ("Referer" , "https://yun.139.com/" )
645
- req .ContentLength = partSize
646
-
647
- res , err := base .HttpClient .Do (req )
622
+ err = d .uploadPersonalParts (ctx , partInfos , moreresp .Data .PartInfos , rateLimited , p )
648
623
if err != nil {
649
624
return err
650
625
}
651
- _ = res .Body .Close ()
652
- log .Debugf ("[139] uploaded: %+v" , res )
653
- if res .StatusCode != http .StatusOK {
654
- return fmt .Errorf ("unexpected status code: %d" , res .StatusCode )
655
- }
656
626
}
657
627
628
+ // 全部分片上传完毕后,complete
658
629
data = base.Json {
659
630
"contentHash" : fullHash ,
660
631
"contentHashAlgorithm" : "SHA256" ,
0 commit comments