Skip to content

Commit 6ad4460

Browse files
authored
feat(drivers/guangyapan): add md5-based instant upload support (#3034)
feat(guangyapan): add md5-based instant upload support Co-authored-by: PIKACHUIM <PIKACHUIM@users.noreply.github.com>
1 parent d90d849 commit 6ad4460

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎drivers/guangyapan/driver.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,13 @@ func (d *GuangYaPan) Put(ctx context.Context, dstDir model.Obj, file model.FileS
394394

395395
parentID := dstDir.GetID()
396396

397-
token, code, err := d.getUploadToken(ctx, parentID, name, file.GetSize())
397+
// 优先秒传:先计算文件 MD5,后端命中相同文件时直接秒传完成,无需真实上传。
398+
md5sum, err := d.fileMD5(file, up)
399+
if err != nil {
400+
return nil, err
401+
}
402+
403+
token, code, err := d.getUploadToken(ctx, parentID, name, file.GetSize(), md5sum)
398404
if err != nil {
399405
return nil, err
400406
}

‎drivers/guangyapan/util.go‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,39 @@ func (d *GuangYaPan) waitTaskDone(ctx context.Context, taskID string) error {
133133

134134
// --- Upload helpers ---
135135

136-
func (d *GuangYaPan) getUploadToken(ctx context.Context, parentID, name string, size int64) (*uploadTokenData, int, error) {
136+
// fileMD5 returns the MD5 of the file content, computing it from the stream when
137+
// the caller did not provide it (e.g. local uploads). The computed hash is used
138+
// to attempt instant upload (秒传) before falling back to the real OSS upload.
139+
// For force-stream uploads the whole file would have to be buffered just to hash
140+
// it, which defeats the purpose of streaming, so we skip 秒传 in that case.
141+
func (d *GuangYaPan) fileMD5(file model.FileStreamer, up driver.UpdateProgress) (string, error) {
142+
if md5sum := file.GetHash().GetHash(utils.MD5); len(md5sum) == utils.MD5.Width {
143+
return md5sum, nil
144+
}
145+
if file.IsForceStreamUpload() {
146+
return "", nil
147+
}
148+
_, md5sum, err := streamPkg.CacheFullAndHash(file, &up, utils.MD5)
149+
if err != nil {
150+
return "", err
151+
}
152+
return md5sum, nil
153+
}
154+
155+
func (d *GuangYaPan) getUploadToken(ctx context.Context, parentID, name string, size int64, md5sum string) (*uploadTokenData, int, error) {
137156
var out uploadTokenResp
157+
res := map[string]any{
158+
"fileSize": size,
159+
}
160+
if md5sum != "" {
161+
// 秒传:后端根据 md5 判断文件是否已存在,命中则返回 code 156 秒传完成。
162+
res["md5"] = md5sum
163+
}
138164
err := d.postAPI(ctx, "/nd.bizuserres.s/v1/get_res_center_token", map[string]any{
139165
"capacity": 2,
140166
"name": name,
141167
"parentId": parentID,
142-
"res": map[string]any{
143-
"fileSize": size,
144-
},
168+
"res": res,
145169
}, &out)
146170
if err != nil {
147171
return nil, 0, err

0 commit comments

Comments
 (0)