Skip to content

Commit 3766389

Browse files
authored
fix(drivers/wps): implement driver.GetRooter interface (#2414)
* fix(drivers/wps): implement driver.GetRooter interface * fix(drivers/wps): add User-Agent and Referer headers in Link method * fix(drivers/wps): removing NoOverwriteUpload field --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent 29ec90e commit 3766389

3 files changed

Lines changed: 90 additions & 34 deletions

File tree

drivers/wps/driver.go

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"strconv"
7+
"strings"
88
"time"
99

1010
"github.com/OpenListTeam/OpenList/v4/drivers/base"
1111
"github.com/OpenListTeam/OpenList/v4/internal/driver"
1212
"github.com/OpenListTeam/OpenList/v4/internal/errs"
1313
"github.com/OpenListTeam/OpenList/v4/internal/model"
14+
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1415
"github.com/go-resty/resty/v2"
1516
)
1617

@@ -59,41 +60,79 @@ func (d *Wps) Drop(ctx context.Context) error {
5960
return nil
6061
}
6162

62-
func (d *Wps) List(ctx context.Context, dir model.Obj, _ model.ListArgs) ([]model.Obj, error) {
63-
basePath := "/"
64-
if dir != nil {
65-
if p := dir.GetPath(); p != "" {
66-
basePath = p
67-
}
63+
func (d *Wps) GetRoot(ctx context.Context) (model.Obj, error) {
64+
root := &Obj{
65+
Obj: &model.Object{
66+
Path: "/",
67+
Name: "root",
68+
Modified: d.Modified,
69+
Ctime: d.Modified,
70+
IsFolder: true,
71+
},
72+
Kind: "root",
6873
}
69-
if basePath == "/" {
74+
rootPath := d.RootFolderPath
75+
if rootPath != "" && rootPath != "/" {
76+
parts := strings.Split(strings.Trim(rootPath, "/"), "/")
7077
groups, err := d.getGroups(ctx)
7178
if err != nil {
7279
return nil, err
7380
}
74-
res := make([]model.Obj, 0, len(groups))
81+
var current *Obj
7582
for _, g := range groups {
76-
path := joinPath(basePath, g.Name)
77-
obj := &Obj{
78-
Obj: &model.Object{
79-
ID: strconv.FormatInt(g.GroupID, 10),
80-
Path: path,
81-
Name: g.Name,
82-
Modified: parseTime(0),
83-
Ctime: parseTime(0),
84-
IsFolder: true,
85-
},
86-
Kind: "group",
87-
GroupID: g.GroupID,
83+
if g.Name == parts[0] {
84+
current = g.groupToObj("/")
85+
break
8886
}
89-
res = append(res, obj)
9087
}
91-
return res, nil
88+
if current == nil {
89+
return nil, fmt.Errorf("root path %q not found", rootPath)
90+
}
91+
parentID := int64(0)
92+
for _, name := range parts[1:] {
93+
files, err := d.getFiles(ctx, current.GroupID, parentID)
94+
if err != nil {
95+
return nil, err
96+
}
97+
var next *Obj
98+
for _, f := range files {
99+
if f.Type == "folder" && f.Name == name {
100+
next = f.fileToObj(current.GetPath(), d.isPersonal())
101+
break
102+
}
103+
}
104+
if next == nil {
105+
return nil, fmt.Errorf("root path %q not found", rootPath)
106+
}
107+
current = next
108+
parentID = current.FileID
109+
}
110+
current.Obj = &model.Object{ID: current.GetID(), Path: "/", Name: current.GetName(), IsFolder: true}
111+
root = current
112+
}
113+
return root, nil
114+
}
115+
116+
func (d *Wps) List(ctx context.Context, dir model.Obj, _ model.ListArgs) ([]model.Obj, error) {
117+
basePath := "/"
118+
if dir != nil {
119+
if p := dir.GetPath(); p != "" {
120+
basePath = p
121+
}
92122
}
93123
node, err := unwrapWpsObj(dir)
94124
if err != nil {
95125
return nil, err
96126
}
127+
if node.Kind == "root" {
128+
groups, err := d.getGroups(ctx)
129+
if err != nil {
130+
return nil, err
131+
}
132+
return utils.SliceConvert(groups, func(g Group) (model.Obj, error) {
133+
return g.groupToObj(basePath), nil
134+
})
135+
}
97136
if node.Kind != "group" && node.Kind != "folder" {
98137
return nil, nil
99138
}
@@ -105,11 +144,9 @@ func (d *Wps) List(ctx context.Context, dir model.Obj, _ model.ListArgs) ([]mode
105144
if err != nil {
106145
return nil, err
107146
}
108-
res := make([]model.Obj, 0, len(files))
109-
for _, f := range files {
110-
res = append(res, f.fileToObj(basePath, d.isPersonal()))
111-
}
112-
return res, nil
147+
return utils.SliceConvert(files, func(f FileInfo) (model.Obj, error) {
148+
return f.fileToObj(basePath, d.isPersonal()), nil
149+
})
113150
}
114151

115152
func (d *Wps) Link(ctx context.Context, file model.Obj, _ model.LinkArgs) (*model.Link, error) {
@@ -138,7 +175,13 @@ func (d *Wps) Link(ctx context.Context, file model.Obj, _ model.LinkArgs) (*mode
138175
if resp.URL == "" {
139176
return nil, fmt.Errorf("empty download url")
140177
}
141-
return &model.Link{URL: resp.URL, Header: http.Header{}}, nil
178+
return &model.Link{
179+
URL: resp.URL,
180+
Header: http.Header{
181+
"User-Agent": []string{d.getUA()},
182+
"Referer": []string{d.driveHost()},
183+
},
184+
}, nil
142185
}
143186

144187
func (d *Wps) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
@@ -357,3 +400,4 @@ func (d *Wps) GetDetails(ctx context.Context) (*model.StorageDetails, error) {
357400
}
358401

359402
var _ driver.Driver = (*Wps)(nil)
403+
var _ driver.GetRooter = (*Wps)(nil)

drivers/wps/meta.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ type Addition struct {
1313
}
1414

1515
var config = driver.Config{
16-
Name: "WPS",
17-
LocalSort: true,
18-
DefaultRoot: "/",
19-
Alert: "",
20-
NoOverwriteUpload: true,
16+
Name: "WPS",
17+
LocalSort: true,
18+
DefaultRoot: "/",
19+
Alert: "",
2120
}
2221

2322
func init() {

drivers/wps/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ func (f FileInfo) fileToObj(basePath string, isPersonal bool) *Obj {
9393
return obj
9494
}
9595

96+
func (g Group) groupToObj(basePath string) *Obj {
97+
return &Obj{
98+
Obj: &model.Object{
99+
ID: strconv.FormatInt(g.GroupID, 10),
100+
Path: joinPath(basePath, g.Name),
101+
Name: g.Name,
102+
IsFolder: true,
103+
},
104+
Kind: "group",
105+
GroupID: g.GroupID,
106+
}
107+
}
108+
96109
type filesResp struct {
97110
Files []FileInfo `json:"files"`
98111
NextOffset int `json:"next_offset"`

0 commit comments

Comments
 (0)