Skip to content

Commit 7858f49

Browse files
ZZ0YYCopilot
andcommitted
fix: use base.NewRestyClient() and use e.g
Co-authored-by: Copilot <copilot@github.com>
1 parent f828fc5 commit 7858f49

2 files changed

Lines changed: 129 additions & 130 deletions

File tree

Lines changed: 110 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
11
package cloudflare_imgbed
22

33
import (
4-
"context"
5-
"fmt"
6-
"strings"
7-
"time"
8-
9-
"github.com/OpenListTeam/OpenList/v4/internal/driver"
10-
"github.com/OpenListTeam/OpenList/v4/internal/errs"
11-
"github.com/OpenListTeam/OpenList/v4/internal/model"
12-
"github.com/go-resty/resty/v2"
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/OpenListTeam/OpenList/v4/drivers/base"
9+
"github.com/OpenListTeam/OpenList/v4/internal/driver"
10+
"github.com/OpenListTeam/OpenList/v4/internal/errs"
11+
"github.com/OpenListTeam/OpenList/v4/internal/model"
12+
"github.com/go-resty/resty/v2"
1313
)
1414

1515
type CFImgBed struct {
16-
model.Storage
17-
Addition
18-
client *resty.Client
16+
model.Storage
17+
Addition
18+
client *resty.Client
1919
}
2020

2121
func (d *CFImgBed) Config() driver.Config {
22-
return config
22+
return config
2323
}
2424

2525
func (d *CFImgBed) GetAddition() driver.Additional {
26-
return &d.Addition
26+
return &d.Addition
2727
}
2828

2929
// Init initializes the HTTP client with the configured Address and Token.
3030
func (d *CFImgBed) Init(ctx context.Context) error {
31-
d.client = resty.New().
32-
SetBaseURL(strings.TrimRight(d.Address, "/")).
33-
SetTimeout(30*time.Second).
34-
SetHeader("Authorization", "Bearer "+d.Token).
35-
SetDebug(false)
36-
return nil
31+
d.client = base.NewRestyClient()
32+
d.client.SetBaseURL(strings.TrimRight(d.Address, "/")).
33+
SetHeader("Authorization", "Bearer "+d.Token).
34+
SetDebug(false)
35+
return nil
3736
}
3837

3938
func (d *CFImgBed) Drop(ctx context.Context) error {
40-
return nil
39+
return nil
4140
}
4241

4342
// apiError represents a generic error response from the CFImgBed API.
4443
type apiError struct {
45-
Error string `json:"error"`
46-
Message string `json:"message"`
44+
Error string `json:"error"`
45+
Message string `json:"message"`
4746
}
4847

4948
// buildReqPath constructs the path to send to the CFImgBed List API.
@@ -56,144 +55,144 @@ type apiError struct {
5655
// dir object whose GetPath() already equals the root path itself. We must
5756
// detect this and avoid double-prepending rootPath.
5857
func buildReqPath(rootPath, dirPath string) string {
59-
rootPath = strings.Trim(rootPath, "/")
60-
dirPath = strings.Trim(dirPath, "/")
58+
rootPath = strings.Trim(rootPath, "/")
59+
dirPath = strings.Trim(dirPath, "/")
6160

62-
if dirPath == "" || dirPath == rootPath {
63-
// Either listing the real root, or OpenList passed the virtual root dir
64-
return rootPath
65-
}
66-
if rootPath == "" {
67-
return dirPath
68-
}
69-
// dirPath is a subfolder returned by a previous List call, prepend rootPath
70-
return rootPath + "/" + dirPath
61+
if dirPath == "" || dirPath == rootPath {
62+
// Either listing the real root, or OpenList passed the virtual root dir
63+
return rootPath
64+
}
65+
if rootPath == "" {
66+
return dirPath
67+
}
68+
// dirPath is a subfolder returned by a previous List call, prepend rootPath
69+
return rootPath + "/" + dirPath
7170
}
7271

7372
// List retrieves the file and directory listing for the given directory.
7473
func (d *CFImgBed) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
75-
rootPath := strings.Trim(d.GetRootPath(), "/")
76-
77-
var dirPath string
78-
if dir != nil {
79-
dirPath = strings.Trim(dir.GetPath(), "/")
80-
}
81-
reqPath := buildReqPath(rootPath, dirPath)
82-
83-
var resp ListResponse
84-
var errResp apiError
85-
res, err := d.client.R().
86-
SetQueryParam("dir", reqPath).
87-
SetQueryParam("count", "-1").
88-
SetResult(&resp).
89-
SetError(&errResp).
90-
Get("/api/manage/list")
91-
92-
if err != nil {
93-
return nil, err
94-
}
95-
if res.IsError() {
96-
if errResp.Message != "" {
97-
return nil, fmt.Errorf("CFImgBed API error: %s", errResp.Message)
98-
}
99-
return nil, fmt.Errorf("CFImgBed API returned status %d", res.StatusCode())
100-
}
101-
102-
objs := make([]model.Obj, 0, len(resp.Directories)+len(resp.Files))
103-
104-
// Strip rootPath prefix from returned paths so that GetPath() is relative
105-
// to the OpenList mount point, not the CFImgBed root.
106-
for _, rawDir := range resp.Directories {
107-
cleanDir := strings.TrimRight(rawDir, "/")
108-
p := stripRootPrefix(cleanDir, rootPath)
109-
objs = append(objs, parseDir(p))
110-
}
111-
112-
for _, item := range resp.Files {
113-
p := stripRootPrefix(item.Name, rootPath)
114-
objs = append(objs, parseFile(FileItem{
115-
Name: p,
116-
Metadata: item.Metadata,
117-
}))
118-
}
119-
120-
return objs, nil
74+
rootPath := strings.Trim(d.GetRootPath(), "/")
75+
76+
var dirPath string
77+
if dir != nil {
78+
dirPath = strings.Trim(dir.GetPath(), "/")
79+
}
80+
reqPath := buildReqPath(rootPath, dirPath)
81+
82+
var resp ListResponse
83+
var errResp apiError
84+
res, err := d.client.R().
85+
SetQueryParam("dir", reqPath).
86+
SetQueryParam("count", "-1").
87+
SetResult(&resp).
88+
SetError(&errResp).
89+
Get("/api/manage/list")
90+
91+
if err != nil {
92+
return nil, err
93+
}
94+
if res.IsError() {
95+
if errResp.Message != "" {
96+
return nil, fmt.Errorf("CFImgBed API error: %s", errResp.Message)
97+
}
98+
return nil, fmt.Errorf("CFImgBed API returned status %d", res.StatusCode())
99+
}
100+
101+
objs := make([]model.Obj, 0, len(resp.Directories)+len(resp.Files))
102+
103+
// Strip rootPath prefix from returned paths so that GetPath() is relative
104+
// to the OpenList mount point, not the CFImgBed root.
105+
for _, rawDir := range resp.Directories {
106+
cleanDir := strings.TrimRight(rawDir, "/")
107+
p := stripRootPrefix(cleanDir, rootPath)
108+
objs = append(objs, parseDir(p))
109+
}
110+
111+
for _, item := range resp.Files {
112+
p := stripRootPrefix(item.Name, rootPath)
113+
objs = append(objs, parseFile(FileItem{
114+
Name: p,
115+
Metadata: item.Metadata,
116+
}))
117+
}
118+
119+
return objs, nil
121120
}
122121

123122
// stripRootPrefix removes the rootPath prefix from a path returned by the API.
124123
// If rootPath is empty or the path doesn't start with rootPath/, return as-is.
125124
func stripRootPrefix(p, rootPath string) string {
126-
if rootPath == "" {
127-
return p
128-
}
129-
prefix := rootPath + "/"
130-
if strings.HasPrefix(p, prefix) {
131-
return strings.TrimPrefix(p, prefix)
132-
}
133-
return p
125+
if rootPath == "" {
126+
return p
127+
}
128+
prefix := rootPath + "/"
129+
if strings.HasPrefix(p, prefix) {
130+
return strings.TrimPrefix(p, prefix)
131+
}
132+
return p
134133
}
135134

136135
// Link constructs a direct download URL for the given file object.
137136
// Format: {Address}/file/{rootPath}/{filePath} with no double slashes.
138137
func (d *CFImgBed) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
139-
rootPath := strings.Trim(d.GetRootPath(), "/")
140-
filePath := strings.Trim(file.GetPath(), "/")
138+
rootPath := strings.Trim(d.GetRootPath(), "/")
139+
filePath := strings.Trim(file.GetPath(), "/")
141140

142-
var fullPath string
143-
if rootPath != "" && filePath != "" {
144-
fullPath = rootPath + "/" + filePath
145-
} else if rootPath != "" {
146-
fullPath = rootPath
147-
} else {
148-
fullPath = filePath
149-
}
141+
var fullPath string
142+
if rootPath != "" && filePath != "" {
143+
fullPath = rootPath + "/" + filePath
144+
} else if rootPath != "" {
145+
fullPath = rootPath
146+
} else {
147+
fullPath = filePath
148+
}
150149

151-
link := strings.TrimRight(d.Address, "/") + "/file/" + fullPath
152-
return &model.Link{URL: link}, nil
150+
link := strings.TrimRight(d.Address, "/") + "/file/" + fullPath
151+
return &model.Link{URL: link}, nil
153152
}
154153

155154
func (d *CFImgBed) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
156-
return nil, errs.NotImplement
155+
return nil, errs.NotImplement
157156
}
158157

159158
func (d *CFImgBed) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
160-
return nil, errs.NotImplement
159+
return nil, errs.NotImplement
161160
}
162161

163162
func (d *CFImgBed) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
164-
return nil, errs.NotImplement
163+
return nil, errs.NotImplement
165164
}
166165

167166
func (d *CFImgBed) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
168-
return nil, errs.NotImplement
167+
return nil, errs.NotImplement
169168
}
170169

171170
func (d *CFImgBed) Remove(ctx context.Context, obj model.Obj) error {
172-
return errs.NotImplement
171+
return errs.NotImplement
173172
}
174173

175174
func (d *CFImgBed) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
176-
return nil, errs.NotImplement
175+
return nil, errs.NotImplement
177176
}
178177

179178
func (d *CFImgBed) GetArchiveMeta(ctx context.Context, obj model.Obj, args model.ArchiveArgs) (model.ArchiveMeta, error) {
180-
return nil, errs.NotImplement
179+
return nil, errs.NotImplement
181180
}
182181

183182
func (d *CFImgBed) ListArchive(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) ([]model.Obj, error) {
184-
return nil, errs.NotImplement
183+
return nil, errs.NotImplement
185184
}
186185

187186
func (d *CFImgBed) Extract(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) (*model.Link, error) {
188-
return nil, errs.NotImplement
187+
return nil, errs.NotImplement
189188
}
190189

191190
func (d *CFImgBed) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj, args model.ArchiveDecompressArgs) ([]model.Obj, error) {
192-
return nil, errs.NotImplement
191+
return nil, errs.NotImplement
193192
}
194193

195194
func (d *CFImgBed) GetDetails(ctx context.Context) (*model.StorageDetails, error) {
196-
return nil, errs.NotImplement
195+
return nil, errs.NotImplement
197196
}
198197

199198
var _ driver.Driver = (*CFImgBed)(nil)

drivers/cloudflare_imgbed/meta.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
package cloudflare_imgbed
22

33
import (
4-
"github.com/OpenListTeam/OpenList/v4/internal/driver"
5-
"github.com/OpenListTeam/OpenList/v4/internal/op"
4+
"github.com/OpenListTeam/OpenList/v4/internal/driver"
5+
"github.com/OpenListTeam/OpenList/v4/internal/op"
66
)
77

88
type Addition struct {
9-
driver.RootPath
10-
Address string `json:"address" type:"text" required:"true" default:"" help:"API 域名,如 https://img.example.com"`
11-
Token string `json:"token" type:"text" required:"true" default:"" help:"API 认证 Token"`
9+
driver.RootPath
10+
Address string `json:"address" type:"text" required:"true" default:"" help:"API domain, https://img.example.com"`
11+
Token string `json:"token" type:"text" required:"true" default:"" help:"API authentication token"`
1212
}
1313

1414
var config = driver.Config{
15-
Name: "cloudflare_imgbed",
16-
LocalSort: false,
17-
OnlyProxy: false,
18-
NoCache: false,
19-
NoUpload: true,
20-
NeedMs: false,
21-
DefaultRoot: "/",
22-
CheckStatus: false,
23-
Alert: "",
24-
NoOverwriteUpload: false,
25-
NoLinkURL: false,
15+
Name: "cloudflare_imgbed",
16+
LocalSort: false,
17+
OnlyProxy: false,
18+
NoCache: false,
19+
NoUpload: true,
20+
NeedMs: false,
21+
DefaultRoot: "/",
22+
CheckStatus: false,
23+
Alert: "",
24+
NoOverwriteUpload: false,
25+
NoLinkURL: false,
2626
}
2727

2828
func init() {
29-
op.RegisterDriver(func() driver.Driver {
30-
return &CFImgBed{}
31-
})
29+
op.RegisterDriver(func() driver.Driver {
30+
return &CFImgBed{}
31+
})
3232
}

0 commit comments

Comments
 (0)