Skip to content

Commit fcbc79c

Browse files
authored
feat: Support 123pan safebox (#9311)
* feat(meta): Added a SafePassword field - Added the SafePassword field to meta.go - Revised the field format to align with the code style - The SafePassword field is used to supplement the extended functionality * feat(driver): Added support for safe unlocking logic - Added safe file unlocking logic in `driver.go`, returning an error if unlocking fails. - Introduced the `safeBoxUnlocked` variable of type `sync.Map` to record the IDs of unlocked files. - Enhanced error handling logic to automatically attempt to unlock safe files and re-retrieve the file list. - Added the `IsLock` field to file types in `types.go` to identify whether they are safe files. - Added a constant definition for the `SafeBoxUnlock` interface address in `util.go`. - Added the `unlockSafeBox` method to unlock a safe with a specified file ID via the API. - Optimized the file retrieval logic to automatically call the unlock method when the safe is locked. * Refactor (driver): Optimize lock field type - Changed the `IsLock` field type from `int` to `bool` for better semantics. - Updated the check logic to use direct Boolean comparisons to improve code readability and accuracy.
1 parent 930f9f6 commit fcbc79c

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

drivers/123/driver.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"net/http"
88
"net/url"
9+
"strconv"
10+
"strings"
911
"sync"
1012
"time"
1113

@@ -28,7 +30,8 @@ import (
2830
type Pan123 struct {
2931
model.Storage
3032
Addition
31-
apiRateLimit sync.Map
33+
apiRateLimit sync.Map
34+
safeBoxUnlocked sync.Map
3235
}
3336

3437
func (d *Pan123) Config() driver.Config {
@@ -52,9 +55,26 @@ func (d *Pan123) Drop(ctx context.Context) error {
5255
}
5356

5457
func (d *Pan123) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
58+
if f, ok := dir.(File); ok && f.IsLock {
59+
if err := d.unlockSafeBox(f.FileId); err != nil {
60+
return nil, err
61+
}
62+
}
5563
files, err := d.getFiles(ctx, dir.GetID(), dir.GetName())
5664
if err != nil {
57-
return nil, err
65+
msg := strings.ToLower(err.Error())
66+
if strings.Contains(msg, "safe box") || strings.Contains(err.Error(), "保险箱") {
67+
if id, e := strconv.ParseInt(dir.GetID(), 10, 64); e == nil {
68+
if e = d.unlockSafeBox(id); e == nil {
69+
files, err = d.getFiles(ctx, dir.GetID(), dir.GetName())
70+
} else {
71+
return nil, e
72+
}
73+
}
74+
}
75+
if err != nil {
76+
return nil, err
77+
}
5878
}
5979
return utils.SliceConvert(files, func(src File) (model.Obj, error) {
6080
return src, nil

drivers/123/meta.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
)
77

88
type Addition struct {
9-
Username string `json:"username" required:"true"`
10-
Password string `json:"password" required:"true"`
9+
Username string `json:"username" required:"true"`
10+
Password string `json:"password" required:"true"`
11+
SafePassword string `json:"safe_password"`
1112
driver.RootID
1213
//OrderBy string `json:"order_by" type:"select" options:"file_id,file_name,size,update_at" default:"file_name"`
1314
//OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" default:"asc"`

drivers/123/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type File struct {
2020
Etag string `json:"Etag"`
2121
S3KeyFlag string `json:"S3KeyFlag"`
2222
DownloadUrl string `json:"DownloadUrl"`
23+
IsLock bool `json:"IsLock"`
2324
}
2425

2526
func (f File) CreateTime() time.Time {

drivers/123/util.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
S3Auth = MainApi + "/file/s3_upload_object/auth"
4444
UploadCompleteV2 = MainApi + "/file/upload_complete/v2"
4545
S3Complete = MainApi + "/file/s3_complete_multipart_upload"
46+
SafeBoxUnlock = MainApi + "/restful/goapi/v1/file/safe_box/auth/unlockbox"
4647
//AuthKeySalt = "8-8D$sL8gPjom7bk#cY"
4748
)
4849

@@ -238,6 +239,22 @@ do:
238239
return body, nil
239240
}
240241

242+
func (d *Pan123) unlockSafeBox(fileId int64) error {
243+
if _, ok := d.safeBoxUnlocked.Load(fileId); ok {
244+
return nil
245+
}
246+
data := base.Json{"password": d.SafePassword}
247+
url := fmt.Sprintf("%s?fileId=%d", SafeBoxUnlock, fileId)
248+
_, err := d.Request(url, http.MethodPost, func(req *resty.Request) {
249+
req.SetBody(data)
250+
}, nil)
251+
if err != nil {
252+
return err
253+
}
254+
d.safeBoxUnlocked.Store(fileId, true)
255+
return nil
256+
}
257+
241258
func (d *Pan123) getFiles(ctx context.Context, parentId string, name string) ([]File, error) {
242259
page := 1
243260
total := 0
@@ -267,6 +284,15 @@ func (d *Pan123) getFiles(ctx context.Context, parentId string, name string) ([]
267284
req.SetQueryParams(query)
268285
}, &resp)
269286
if err != nil {
287+
msg := strings.ToLower(err.Error())
288+
if strings.Contains(msg, "safe box") || strings.Contains(err.Error(), "保险箱") {
289+
if fid, e := strconv.ParseInt(parentId, 10, 64); e == nil {
290+
if e = d.unlockSafeBox(fid); e == nil {
291+
return d.getFiles(ctx, parentId, name)
292+
}
293+
return nil, e
294+
}
295+
}
270296
return nil, err
271297
}
272298
log.Debug(string(_res))

0 commit comments

Comments
 (0)