Skip to content

Commit 2d2d9ae

Browse files
xrgzsCopilot
andauthored
fix(drivers/wps): correct account relevant handling (#2415)
* fix(drivers/wps): correct account modes handling Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * feat(drivers/wps): enhance GetDetails for business account Co-authored-by: Copilot <copilot@github.com> Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> Co-authored-by: Copilot <copilot@github.com>
1 parent 3766389 commit 2d2d9ae

3 files changed

Lines changed: 57 additions & 19 deletions

File tree

drivers/wps/driver.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,47 @@ func (d *Wps) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer
382382
}
383383

384384
func (d *Wps) GetDetails(ctx context.Context) (*model.StorageDetails, error) {
385-
url := fmt.Sprintf("%s/api/v3/spaces", d.driveHost()+d.drivePrefix())
386-
var resp spacesResp
385+
if d.isPersonal() {
386+
url := ENDPOINT_PERSONAL + "/api/v3/spaces"
387+
var resp spacesResp
388+
r, err := d.request(ctx).SetResult(&resp).SetError(&resp).Get(url)
389+
if err != nil {
390+
return nil, err
391+
}
392+
if r != nil && r.IsError() {
393+
return nil, fmt.Errorf("http error: %d", r.StatusCode())
394+
}
395+
return &model.StorageDetails{
396+
DiskUsage: model.DiskUsage{
397+
TotalSpace: resp.Total,
398+
UsedSpace: resp.Used,
399+
},
400+
}, nil
401+
}
402+
url := ENDPOINT_BUSINESS + "/3rd/plussvr/compose/v1/u/companies/batch/service-space?comp_ids=" + fmt.Sprint(d.login.CompanyID)
403+
var resp serviceSpaceResp
387404
r, err := d.request(ctx).SetResult(&resp).SetError(&resp).Get(url)
388405
if err != nil {
389406
return nil, err
390407
}
391408
if r != nil && r.IsError() {
392409
return nil, fmt.Errorf("http error: %d", r.StatusCode())
393410
}
394-
return &model.StorageDetails{
395-
DiskUsage: model.DiskUsage{
396-
TotalSpace: resp.Total,
397-
UsedSpace: resp.Used,
398-
},
399-
}, nil
411+
if len(resp.Info) == 0 {
412+
return nil, fmt.Errorf("empty service space info")
413+
}
414+
// info := resp.Info[0]
415+
for _, info := range resp.Info {
416+
if info.ID == d.login.CompanyID {
417+
return &model.StorageDetails{
418+
DiskUsage: model.DiskUsage{
419+
TotalSpace: info.SpaceTotal,
420+
UsedSpace: info.SpaceUsed,
421+
},
422+
}, nil
423+
}
424+
}
425+
return nil, fmt.Errorf("service space info not found for company ID: %d", d.login.CompanyID)
400426
}
401427

402428
var _ driver.Driver = (*Wps)(nil)

drivers/wps/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ type spacesResp struct {
128128
} `json:"used_parts"`
129129
}
130130

131+
type serviceSpaceResp struct {
132+
Info []struct {
133+
ID int64 `json:"id"`
134+
SpaceTotal int64 `json:"space_total"`
135+
SpaceUsed int64 `json:"space_used"`
136+
} `json:"info"`
137+
}
138+
131139
type uploadCreateUpdateResp struct {
132140
apiResult
133141
Method string `json:"method"`

drivers/wps/util.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ func checkAPI(resp *resty.Response, result apiResult) error {
155155
}
156156

157157
func (d *Wps) getGroups(ctx context.Context) ([]Group, error) {
158-
if d.isPersonal() {
158+
// different APIs
159+
switch d.Mode {
160+
case "Personal":
159161
var resp personalGroupsResp
160162
r, err := d.request(ctx).SetResult(&resp).SetError(&resp).Get(d.driveURL("/api/v3/groups"))
161163
if err != nil {
@@ -169,17 +171,19 @@ func (d *Wps) getGroups(ctx context.Context) ([]Group, error) {
169171
res = append(res, Group{GroupID: g.ID, Name: g.Name})
170172
}
171173
return res, nil
174+
case "Business":
175+
var resp groupsResp
176+
url := fmt.Sprintf("%s/3rd/plus/groups/v1/companies/%d/users/self/groups/private", ENDPOINT_BUSINESS, d.login.CompanyID)
177+
r, err := d.request(ctx).SetResult(&resp).SetError(&resp).Get(url)
178+
if err != nil {
179+
return nil, err
180+
}
181+
if r != nil && r.IsError() {
182+
return nil, fmt.Errorf("http error: %d", r.StatusCode())
183+
}
184+
return resp.Groups, nil
172185
}
173-
var resp groupsResp
174-
url := fmt.Sprintf("%s/3rd/plus/groups/v1/companies/%d/users/self/groups/private", ENDPOINT_BUSINESS, d.login.CompanyID)
175-
r, err := d.request(ctx).SetResult(&resp).SetError(&resp).Get(url)
176-
if err != nil {
177-
return nil, err
178-
}
179-
if r != nil && r.IsError() {
180-
return nil, fmt.Errorf("http error: %d", r.StatusCode())
181-
}
182-
return resp.Groups, nil
186+
return nil, fmt.Errorf("unsupported mode: %s", d.Mode)
183187
}
184188

185189
func (d *Wps) getFiles(ctx context.Context, groupID, parentID int64) ([]FileInfo, error) {

0 commit comments

Comments
 (0)