Skip to content

Commit 57e1523

Browse files
committed
feat(drivers/139): add UseOldStreamUpload option
- Add UseOldStreamUpload option - Implement newRequest, newPost - Support rapid upload for PersonalNew and Group/Family Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent b9b9f54 commit 57e1523

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

drivers/139/driver.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,22 @@ func (d *Yun139) getPartSize(size int64) int64 {
627627
}
628628

629629
func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
630-
switch d.Addition.Type {
631-
case MetaPersonalNew:
630+
// PersonalNew 以及 Group/Family 在非旧流模式时走新上传路径
631+
if d.Addition.Type == MetaPersonalNew ||
632+
((d.isGroup() || d.isFamily()) && !d.UseOldStreamUpload) {
633+
var createPath, getUploadUrlPath, completePath string
634+
if d.isGroup() || d.isFamily() {
635+
// 家庭盘和小组盘共用同一套新上传 API
636+
createPath = "/dynamic/file/create"
637+
getUploadUrlPath = "/dynamic/file/getUploadUrl"
638+
completePath = "/dynamic/file/complete"
639+
} else {
640+
// MetaPersonalNew
641+
createPath = "/file/create"
642+
getUploadUrlPath = "/file/getUploadUrl"
643+
completePath = "/file/complete"
644+
}
645+
632646
var err error
633647
fullHash := stream.GetHash().GetHash(utils.SHA256)
634648
if len(fullHash) != utils.SHA256.Width {
@@ -683,9 +697,8 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
683697
"type": "file",
684698
"fileRenameMode": "auto_rename",
685699
}
686-
pathname := "/file/create"
687700
var resp PersonalUploadResp
688-
_, err = d.personalPost(pathname, data, &resp)
701+
_, err = d.newPost(createPath, data, &resp)
689702
if err != nil {
690703
return err
691704
}
@@ -732,9 +745,8 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
732745
"accountType": 1,
733746
},
734747
}
735-
pathname := "/file/getUploadUrl"
736748
var moreresp PersonalUploadUrlResp
737-
_, err = d.personalPost(pathname, moredata, &moreresp)
749+
_, err = d.newPost(getUploadUrlPath, moredata, &moreresp)
738750
if err != nil {
739751
return err
740752
}
@@ -751,7 +763,7 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
751763
"fileId": resp.Data.FileId,
752764
"uploadId": resp.Data.UploadId,
753765
}
754-
_, err = d.personalPost("/file/complete", data, nil)
766+
_, err = d.newPost(completePath, data, nil)
755767
if err != nil {
756768
return err
757769
}
@@ -796,11 +808,11 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
796808
}
797809
}
798810
return nil
799-
case MetaPersonal:
800-
fallthrough
801-
case MetaGroup:
802-
fallthrough
803-
case MetaFamily:
811+
}
812+
813+
// 旧上传路径
814+
switch d.Addition.Type {
815+
case MetaPersonal, MetaGroup, MetaFamily:
804816
// 处理冲突
805817
// 获取文件列表
806818
files, err := d.List(ctx, dstDir, model.ListArgs{})

drivers/139/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Addition struct {
1818
CustomUploadPartSize int64 `json:"custom_upload_part_size" type:"number" default:"0" help:"0 for auto"`
1919
ReportRealSize bool `json:"report_real_size" type:"bool" default:"true" help:"Enable to report the real file size during upload"`
2020
UseLargeThumbnail bool `json:"use_large_thumbnail" type:"bool" default:"false" help:"Enable to use large thumbnail for images"`
21+
UseOldStreamUpload bool `json:"use_old_stream_upload" type:"bool" default:"false" help:"Enable to use old stream upload method (not support rapid upload)"`
2122
}
2223

2324
var config = driver.Config{

drivers/139/util.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ func unicode(str string) string {
497497
return textUnquoted
498498
}
499499

500-
func (d *Yun139) personalRequest(pathname string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
501-
url := d.getPersonalCloudHost() + pathname
500+
func (d *Yun139) newRequest(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
502501
req := base.RestyClient.R()
503502
randStr := random.String(16)
504503
ts := time.Now().Format("2006-01-02 15:04:05")
@@ -560,7 +559,21 @@ func (d *Yun139) personalRequest(pathname string, method string, callback base.R
560559
}
561560

562561
func (d *Yun139) personalPost(pathname string, data interface{}, resp interface{}) ([]byte, error) {
563-
return d.personalRequest(pathname, http.MethodPost, func(req *resty.Request) {
562+
return d.newRequest(d.getFamilyCloudHost()+pathname, http.MethodPost, func(req *resty.Request) {
563+
req.SetBody(data)
564+
}, resp)
565+
}
566+
567+
func (d *Yun139) newPost(pathname string, data interface{}, resp interface{}) ([]byte, error) {
568+
var url string
569+
switch d.Type {
570+
case MetaFamily, MetaGroup:
571+
// this is on purpose
572+
url = d.getGroupCloudHost() + pathname
573+
default:
574+
url = d.getPersonalCloudHost() + pathname
575+
}
576+
return d.newRequest(url, http.MethodPost, func(req *resty.Request) {
564577
req.SetBody(data)
565578
}, resp)
566579
}

0 commit comments

Comments
 (0)