Skip to content

Commit af18cb1

Browse files
xrgzsCopilot
andauthored
feat(139): add option ReportRealSize (#8244 close #8141)
* feat(139): handle family upload errors * feat(139): add option `ReportRealSize` * Update drivers/139/driver.go Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 31c55a2 commit af18cb1

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

drivers/139/driver.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package _139
33
import (
44
"context"
55
"encoding/base64"
6+
"encoding/xml"
67
"fmt"
78
"io"
89
"net/http"
@@ -740,14 +741,20 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
740741
break
741742
}
742743
}
744+
var reportSize int64
745+
if d.ReportRealSize {
746+
reportSize = stream.GetSize()
747+
} else {
748+
reportSize = 0
749+
}
743750
data := base.Json{
744751
"manualRename": 2,
745752
"operation": 0,
746753
"fileCount": 1,
747-
"totalSize": 0, // 去除上传大小限制
754+
"totalSize": reportSize,
748755
"uploadContentList": []base.Json{{
749756
"contentName": stream.GetName(),
750-
"contentSize": 0, // 去除上传大小限制
757+
"contentSize": reportSize,
751758
// "digest": "5a3231986ce7a6b46e408612d385bafa"
752759
}},
753760
"parentCatalogID": dstDir.GetID(),
@@ -765,10 +772,10 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
765772
"operation": 0,
766773
"path": path.Join(dstDir.GetPath(), dstDir.GetID()),
767774
"seqNo": random.String(32), //序列号不能为空
768-
"totalSize": 0,
775+
"totalSize": reportSize,
769776
"uploadContentList": []base.Json{{
770777
"contentName": stream.GetName(),
771-
"contentSize": 0,
778+
"contentSize": reportSize,
772779
// "digest": "5a3231986ce7a6b46e408612d385bafa"
773780
}},
774781
})
@@ -779,6 +786,9 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
779786
if err != nil {
780787
return err
781788
}
789+
if resp.Data.Result.ResultCode != "0" {
790+
return fmt.Errorf("get file upload url failed with result code: %s, message: %s", resp.Data.Result.ResultCode, resp.Data.Result.ResultDesc)
791+
}
782792

783793
// Progress
784794
p := driver.NewProgress(stream.GetSize(), up)
@@ -820,13 +830,23 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
820830
if err != nil {
821831
return err
822832
}
823-
_ = res.Body.Close()
824-
log.Debugf("%+v", res)
825833
if res.StatusCode != http.StatusOK {
834+
res.Body.Close()
826835
return fmt.Errorf("unexpected status code: %d", res.StatusCode)
827836
}
837+
bodyBytes, err := io.ReadAll(res.Body)
838+
if err != nil {
839+
return fmt.Errorf("error reading response body: %v", err)
840+
}
841+
var result InterLayerUploadResult
842+
err = xml.Unmarshal(bodyBytes, &result)
843+
if err != nil {
844+
return fmt.Errorf("error parsing XML: %v", err)
845+
}
846+
if result.ResultCode != 0 {
847+
return fmt.Errorf("upload failed with result code: %d, message: %s", result.ResultCode, result.Msg)
848+
}
828849
}
829-
830850
return nil
831851
default:
832852
return errs.NotImplement

drivers/139/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Addition struct {
1212
Type string `json:"type" type:"select" options:"personal_new,family,group,personal" default:"personal_new"`
1313
CloudID string `json:"cloud_id"`
1414
CustomUploadPartSize int64 `json:"custom_upload_part_size" type:"number" default:"0" help:"0 for auto"`
15+
ReportRealSize bool `json:"report_real_size" type:"bool" default:"true" help:"Enable to report the real file size during upload"`
1516
}
1617

1718
var config = driver.Config{

drivers/139/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ type UploadResp struct {
143143
} `json:"data"`
144144
}
145145

146+
type InterLayerUploadResult struct {
147+
XMLName xml.Name `xml:"result"`
148+
Text string `xml:",chardata"`
149+
ResultCode int `xml:"resultCode"`
150+
Msg string `xml:"msg"`
151+
}
152+
146153
type CloudContent struct {
147154
ContentID string `json:"contentID"`
148155
//Modifier string `json:"modifier"`

0 commit comments

Comments
 (0)