Skip to content

Commit a121f86

Browse files
authored
feat(drivers/123open): support sha1 reuse api (OpenListTeam#2089)
* feat(drivers/123open): support sha1 reuse api * fix(drivers/123open): fix typos
1 parent 6861cb4 commit a121f86

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

drivers/123_open/driver.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ func (d *Open123) Put(ctx context.Context, dstDir model.Obj, file model.FileStre
181181
if err != nil {
182182
return nil, fmt.Errorf("parse parentFileID error: %v", err)
183183
}
184+
185+
// 尝试 SHA1 秒传
186+
sha1Hash := file.GetHash().GetHash(utils.SHA1)
187+
if len(sha1Hash) == utils.SHA1.Width {
188+
resp, err := d.sha1Reuse(parentFileId, file.GetName(), sha1Hash, file.GetSize(), 2)
189+
if err == nil && resp.Data.Reuse {
190+
return File{
191+
FileName: file.GetName(),
192+
Size: file.GetSize(),
193+
FileId: resp.Data.FileID,
194+
Type: 2,
195+
SHA1: sha1Hash,
196+
}, nil
197+
}
198+
}
199+
184200
// etag 文件md5
185201
etag := file.GetHash().GetHash(utils.MD5)
186202
if len(etag) < utils.MD5.Width {

drivers/123_open/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ type File struct {
5858
Category int `json:"category"`
5959
Status int `json:"status"`
6060
Trashed int `json:"trashed"`
61+
SHA1 string
6162
}
6263

6364
func (f File) GetHash() utils.HashInfo {
65+
if len(f.SHA1) == utils.SHA1.Width && len(f.Etag) != utils.MD5.Width {
66+
return utils.NewHashInfo(utils.SHA1, f.SHA1)
67+
}
6468
return utils.NewHashInfo(utils.MD5, f.Etag)
6569
}
6670

@@ -190,6 +194,14 @@ type UploadCompleteResp struct {
190194
} `json:"data"`
191195
}
192196

197+
type SHA1ReuseResp struct {
198+
BaseResp
199+
Data struct {
200+
FileID int64 `json:"fileID"`
201+
Reuse bool `json:"reuse"`
202+
} `json:"data"`
203+
}
204+
193205
type OfflineDownloadResp struct {
194206
BaseResp
195207
Data struct {

drivers/123_open/upload.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,21 @@ func (d *Open123) complete(preuploadID string) (*UploadCompleteResp, error) {
183183
}
184184
return &resp, nil
185185
}
186+
187+
// SHA1 秒传
188+
func (d *Open123) sha1Reuse(parentFileID int64, filename string, sha1Hash string, size int64, duplicate int) (*SHA1ReuseResp, error) {
189+
var resp SHA1ReuseResp
190+
_, err := d.Request(UploadSHA1Reuse, http.MethodPost, func(req *resty.Request) {
191+
req.SetBody(base.Json{
192+
"parentFileID": parentFileID,
193+
"filename": filename,
194+
"sha1": strings.ToLower(sha1Hash),
195+
"size": size,
196+
"duplicate": duplicate,
197+
})
198+
}, &resp)
199+
if err != nil {
200+
return nil, err
201+
}
202+
return &resp, nil
203+
}

drivers/123_open/util.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ import (
2121
var ( // 不同情况下获取的AccessTokenQPS限制不同 如下模块化易于拓展
2222
Api = "https://open-api.123pan.com"
2323

24-
UserInfo = InitApiInfo(Api+"/api/v1/user/info", 1)
25-
FileList = InitApiInfo(Api+"/api/v2/file/list", 3)
26-
DownloadInfo = InitApiInfo(Api+"/api/v1/file/download_info", 5)
27-
DirectLink = InitApiInfo(Api+"/api/v1/direct-link/url", 5)
28-
Mkdir = InitApiInfo(Api+"/upload/v1/file/mkdir", 2)
29-
Move = InitApiInfo(Api+"/api/v1/file/move", 1)
30-
Rename = InitApiInfo(Api+"/api/v1/file/name", 1)
31-
Trash = InitApiInfo(Api+"/api/v1/file/trash", 2)
32-
UploadCreate = InitApiInfo(Api+"/upload/v2/file/create", 2)
33-
UploadComplete = InitApiInfo(Api+"/upload/v2/file/upload_complete", 0)
24+
UserInfo = InitApiInfo(Api+"/api/v1/user/info", 1)
25+
FileList = InitApiInfo(Api+"/api/v2/file/list", 3)
26+
DownloadInfo = InitApiInfo(Api+"/api/v1/file/download_info", 5)
27+
DirectLink = InitApiInfo(Api+"/api/v1/direct-link/url", 5)
28+
Mkdir = InitApiInfo(Api+"/upload/v1/file/mkdir", 2)
29+
Move = InitApiInfo(Api+"/api/v1/file/move", 1)
30+
Rename = InitApiInfo(Api+"/api/v1/file/name", 1)
31+
Trash = InitApiInfo(Api+"/api/v1/file/trash", 2)
32+
UploadCreate = InitApiInfo(Api+"/upload/v2/file/create", 2)
33+
UploadComplete = InitApiInfo(Api+"/upload/v2/file/upload_complete", 0)
34+
UploadSHA1Reuse = InitApiInfo(Api+"/upload/v2/file/sha1_reuse", 2)
3435

3536
OfflineDownload = InitApiInfo(Api+"/api/v1/offline/download", 1)
3637
OfflineDownloadProcess = InitApiInfo(Api+"/api/v1/offline/download/process", 5)

0 commit comments

Comments
 (0)