@@ -3,12 +3,10 @@ package aliyundrive_open
33import (
44 "context"
55 "errors"
6- "fmt"
76 "net/http"
87 "path/filepath"
98 "time"
109
11- "github.com/Xhofe/rateg"
1210 "github.com/alist-org/alist/v3/drivers/base"
1311 "github.com/alist-org/alist/v3/internal/driver"
1412 "github.com/alist-org/alist/v3/internal/errs"
@@ -24,9 +22,8 @@ type AliyundriveOpen struct {
2422
2523 DriveId string
2624
27- limitList func (ctx context.Context , data base.Json ) (* Files , error )
28- limitLink func (ctx context.Context , file model.Obj ) (* model.Link , error )
29- ref * AliyundriveOpen
25+ limiter * limiter
26+ ref * AliyundriveOpen
3027}
3128
3229func (d * AliyundriveOpen ) Config () driver.Config {
@@ -38,25 +35,23 @@ func (d *AliyundriveOpen) GetAddition() driver.Additional {
3835}
3936
4037func (d * AliyundriveOpen ) Init (ctx context.Context ) error {
38+ d .limiter = getLimiterForUser (globalLimiterUserID )
4139 if d .LIVPDownloadFormat == "" {
4240 d .LIVPDownloadFormat = "jpeg"
4341 }
4442 if d .DriveType == "" {
4543 d .DriveType = "default"
4644 }
47- res , err := d .request ("/adrive/v1.0/user/getDriveInfo" , http .MethodPost , nil )
45+ res , err := d .request (ctx , limiterOther , "/adrive/v1.0/user/getDriveInfo" , http .MethodPost , nil )
4846 if err != nil {
47+ d .limiter .free ()
48+ d .limiter = nil
4949 return err
5050 }
5151 d .DriveId = utils .Json .Get (res , d .DriveType + "_drive_id" ).ToString ()
52- d .limitList = rateg .LimitFnCtx (d .list , rateg.LimitFnOption {
53- Limit : 4 ,
54- Bucket : 1 ,
55- })
56- d .limitLink = rateg .LimitFnCtx (d .link , rateg.LimitFnOption {
57- Limit : 1 ,
58- Bucket : 1 ,
59- })
52+ userID := utils .Json .Get (res , "user_id" ).ToString ()
53+ d .limiter .free ()
54+ d .limiter = getLimiterForUser (userID )
6055 return nil
6156}
6257
@@ -70,6 +65,8 @@ func (d *AliyundriveOpen) InitReference(storage driver.Driver) error {
7065}
7166
7267func (d * AliyundriveOpen ) Drop (ctx context.Context ) error {
68+ d .limiter .free ()
69+ d .limiter = nil
7370 d .ref = nil
7471 return nil
7572}
@@ -87,9 +84,6 @@ func (d *AliyundriveOpen) GetRoot(ctx context.Context) (model.Obj, error) {
8784}
8885
8986func (d * AliyundriveOpen ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
90- if d .limitList == nil {
91- return nil , fmt .Errorf ("driver not init" )
92- }
9387 files , err := d .getFiles (ctx , dir .GetID ())
9488 if err != nil {
9589 return nil , err
@@ -108,7 +102,7 @@ func (d *AliyundriveOpen) List(ctx context.Context, dir model.Obj, args model.Li
108102}
109103
110104func (d * AliyundriveOpen ) link (ctx context.Context , file model.Obj ) (* model.Link , error ) {
111- res , err := d .request ("/adrive/v1.0/openFile/getDownloadUrl" , http .MethodPost , func (req * resty.Request ) {
105+ res , err := d .request (ctx , limiterLink , "/adrive/v1.0/openFile/getDownloadUrl" , http .MethodPost , func (req * resty.Request ) {
112106 req .SetBody (base.Json {
113107 "drive_id" : d .DriveId ,
114108 "file_id" : file .GetID (),
@@ -133,16 +127,13 @@ func (d *AliyundriveOpen) link(ctx context.Context, file model.Obj) (*model.Link
133127}
134128
135129func (d * AliyundriveOpen ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
136- if d .limitLink == nil {
137- return nil , fmt .Errorf ("driver not init" )
138- }
139- return d .limitLink (ctx , file )
130+ return d .link (ctx , file )
140131}
141132
142133func (d * AliyundriveOpen ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) (model.Obj , error ) {
143134 nowTime , _ := getNowTime ()
144135 newDir := File {CreatedAt : nowTime , UpdatedAt : nowTime }
145- _ , err := d .request ("/adrive/v1.0/openFile/create" , http .MethodPost , func (req * resty.Request ) {
136+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/create" , http .MethodPost , func (req * resty.Request ) {
146137 req .SetBody (base.Json {
147138 "drive_id" : d .DriveId ,
148139 "parent_file_id" : parentDir .GetID (),
@@ -168,7 +159,7 @@ func (d *AliyundriveOpen) MakeDir(ctx context.Context, parentDir model.Obj, dirN
168159
169160func (d * AliyundriveOpen ) Move (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
170161 var resp MoveOrCopyResp
171- _ , err := d .request ("/adrive/v1.0/openFile/move" , http .MethodPost , func (req * resty.Request ) {
162+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/move" , http .MethodPost , func (req * resty.Request ) {
172163 req .SetBody (base.Json {
173164 "drive_id" : d .DriveId ,
174165 "file_id" : srcObj .GetID (),
@@ -198,7 +189,7 @@ func (d *AliyundriveOpen) Move(ctx context.Context, srcObj, dstDir model.Obj) (m
198189
199190func (d * AliyundriveOpen ) Rename (ctx context.Context , srcObj model.Obj , newName string ) (model.Obj , error ) {
200191 var newFile File
201- _ , err := d .request ("/adrive/v1.0/openFile/update" , http .MethodPost , func (req * resty.Request ) {
192+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/update" , http .MethodPost , func (req * resty.Request ) {
202193 req .SetBody (base.Json {
203194 "drive_id" : d .DriveId ,
204195 "file_id" : srcObj .GetID (),
@@ -230,7 +221,7 @@ func (d *AliyundriveOpen) Rename(ctx context.Context, srcObj model.Obj, newName
230221
231222func (d * AliyundriveOpen ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) error {
232223 var resp MoveOrCopyResp
233- _ , err := d .request ("/adrive/v1.0/openFile/copy" , http .MethodPost , func (req * resty.Request ) {
224+ _ , err := d .request (ctx , limiterOther , "/adrive/v1.0/openFile/copy" , http .MethodPost , func (req * resty.Request ) {
234225 req .SetBody (base.Json {
235226 "drive_id" : d .DriveId ,
236227 "file_id" : srcObj .GetID (),
@@ -256,7 +247,7 @@ func (d *AliyundriveOpen) Remove(ctx context.Context, obj model.Obj) error {
256247 if d .RemoveWay == "delete" {
257248 uri = "/adrive/v1.0/openFile/delete"
258249 }
259- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
250+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
260251 req .SetBody (base.Json {
261252 "drive_id" : d .DriveId ,
262253 "file_id" : obj .GetID (),
@@ -295,7 +286,7 @@ func (d *AliyundriveOpen) Other(ctx context.Context, args model.OtherArgs) (inte
295286 default :
296287 return nil , errs .NotSupport
297288 }
298- _ , err := d .request (uri , http .MethodPost , func (req * resty.Request ) {
289+ _ , err := d .request (ctx , limiterOther , uri , http .MethodPost , func (req * resty.Request ) {
299290 req .SetBody (data ).SetResult (& resp )
300291 })
301292 if err != nil {
0 commit comments