11package cloudflare_imgbed
22
33import (
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"
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
1515type CFImgBed struct {
16- model.Storage
17- Addition
18- client * resty.Client
16+ model.Storage
17+ Addition
18+ client * resty.Client
1919}
2020
2121func (d * CFImgBed ) Config () driver.Config {
22- return config
22+ return config
2323}
2424
2525func (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.
3030func (d * CFImgBed ) Init (ctx context.Context ) error {
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
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
3636}
3737
3838func (d * CFImgBed ) Drop (ctx context.Context ) error {
39- return nil
39+ return nil
4040}
4141
4242// apiError represents a generic error response from the CFImgBed API.
4343type apiError struct {
44- Error string `json:"error"`
45- Message string `json:"message"`
44+ Error string `json:"error"`
45+ Message string `json:"message"`
4646}
4747
4848// buildReqPath constructs the path to send to the CFImgBed List API.
@@ -55,144 +55,144 @@ type apiError struct {
5555// dir object whose GetPath() already equals the root path itself. We must
5656// detect this and avoid double-prepending rootPath.
5757func buildReqPath (rootPath , dirPath string ) string {
58- rootPath = strings .Trim (rootPath , "/" )
59- dirPath = strings .Trim (dirPath , "/" )
58+ rootPath = strings .Trim (rootPath , "/" )
59+ dirPath = strings .Trim (dirPath , "/" )
6060
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
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
7070}
7171
7272// List retrieves the file and directory listing for the given directory.
7373func (d * CFImgBed ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
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
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
120120}
121121
122122// stripRootPrefix removes the rootPath prefix from a path returned by the API.
123123// If rootPath is empty or the path doesn't start with rootPath/, return as-is.
124124func stripRootPrefix (p , rootPath string ) string {
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
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
133133}
134134
135135// Link constructs a direct download URL for the given file object.
136136// Format: {Address}/file/{rootPath}/{filePath} with no double slashes.
137137func (d * CFImgBed ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
138- rootPath := strings .Trim (d .GetRootPath (), "/" )
139- filePath := strings .Trim (file .GetPath (), "/" )
138+ rootPath := strings .Trim (d .GetRootPath (), "/" )
139+ filePath := strings .Trim (file .GetPath (), "/" )
140140
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- }
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+ }
149149
150- link := strings .TrimRight (d .Address , "/" ) + "/file/" + fullPath
151- return & model.Link {URL : link }, nil
150+ link := strings .TrimRight (d .Address , "/" ) + "/file/" + fullPath
151+ return & model.Link {URL : link }, nil
152152}
153153
154154func (d * CFImgBed ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) (model.Obj , error ) {
155- return nil , errs .NotImplement
155+ return nil , errs .NotImplement
156156}
157157
158158func (d * CFImgBed ) Move (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
159- return nil , errs .NotImplement
159+ return nil , errs .NotImplement
160160}
161161
162162func (d * CFImgBed ) Rename (ctx context.Context , srcObj model.Obj , newName string ) (model.Obj , error ) {
163- return nil , errs .NotImplement
163+ return nil , errs .NotImplement
164164}
165165
166166func (d * CFImgBed ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
167- return nil , errs .NotImplement
167+ return nil , errs .NotImplement
168168}
169169
170170func (d * CFImgBed ) Remove (ctx context.Context , obj model.Obj ) error {
171- return errs .NotImplement
171+ return errs .NotImplement
172172}
173173
174174func (d * CFImgBed ) Put (ctx context.Context , dstDir model.Obj , file model.FileStreamer , up driver.UpdateProgress ) (model.Obj , error ) {
175- return nil , errs .NotImplement
175+ return nil , errs .NotImplement
176176}
177177
178178func (d * CFImgBed ) GetArchiveMeta (ctx context.Context , obj model.Obj , args model.ArchiveArgs ) (model.ArchiveMeta , error ) {
179- return nil , errs .NotImplement
179+ return nil , errs .NotImplement
180180}
181181
182182func (d * CFImgBed ) ListArchive (ctx context.Context , obj model.Obj , args model.ArchiveInnerArgs ) ([]model.Obj , error ) {
183- return nil , errs .NotImplement
183+ return nil , errs .NotImplement
184184}
185185
186186func (d * CFImgBed ) Extract (ctx context.Context , obj model.Obj , args model.ArchiveInnerArgs ) (* model.Link , error ) {
187- return nil , errs .NotImplement
187+ return nil , errs .NotImplement
188188}
189189
190190func (d * CFImgBed ) ArchiveDecompress (ctx context.Context , srcObj , dstDir model.Obj , args model.ArchiveDecompressArgs ) ([]model.Obj , error ) {
191- return nil , errs .NotImplement
191+ return nil , errs .NotImplement
192192}
193193
194194func (d * CFImgBed ) GetDetails (ctx context.Context ) (* model.StorageDetails , error ) {
195- return nil , errs .NotImplement
195+ return nil , errs .NotImplement
196196}
197197
198198var _ driver.Driver = (* CFImgBed )(nil )
0 commit comments