Skip to content

Commit d17889b

Browse files
authored
feat(gofile): add configurable link expiration handling (#9329)
* feat(driver): add Gofile storage driver Add support for Gofile.io cloud storage service with full CRUD operations. Features: - File and folder listing - Upload and download functionality - Create, move, rename, copy, and delete operations - Direct link generation for file access - API token authentication The driver implements all required driver interfaces and follows the existing driver patterns in the codebase. * feat(gofile): add configurable link expiration handling - Adjusts driver addition metadata to accept LinkExpiry and DirectLinkExpiry options for caching and API expiry control (drivers/gofile/meta.go:10). - Applies the new options when building file links, setting optional local cache expiration (drivers/gofile/driver.go:101) and sending an expireTime to the direct-link API (drivers/gofile/util.go:202). - Logs Gofile API error payloads and validates the structured error response before returning it (drivers/gofile/util.go:141). - Adds the required imports and returns the configured model.Link instance (drivers/gofile/driver.go:6).
1 parent 4f8bc47 commit d17889b

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

drivers/gofile/driver.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gofile
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/alist-org/alist/v3/internal/driver"
89
"github.com/alist-org/alist/v3/internal/errs"
@@ -72,7 +73,7 @@ func (d *Gofile) List(ctx context.Context, dir model.Obj, args model.ListArgs) (
7273
}
7374

7475
var objects []model.Obj
75-
76+
7677
// Process children or contents
7778
contents := response.Data.Children
7879
if contents == nil {
@@ -97,9 +98,18 @@ func (d *Gofile) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
9798
return nil, fmt.Errorf("failed to create direct link: %w", err)
9899
}
99100

100-
return &model.Link{
101+
// Configure cache expiration based on user setting
102+
link := &model.Link{
101103
URL: directLink,
102-
}, nil
104+
}
105+
106+
// Only set expiration if LinkExpiry > 0 (0 means no caching)
107+
if d.LinkExpiry > 0 {
108+
expiration := time.Duration(d.LinkExpiry) * 24 * time.Hour
109+
link.Expiration = &expiration
110+
}
111+
112+
return link, nil
103113
}
104114

105115
func (d *Gofile) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
@@ -258,4 +268,4 @@ func (d *Gofile) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj
258268
return nil, errs.NotImplement
259269
}
260270

261-
var _ driver.Driver = (*Gofile)(nil)
271+
var _ driver.Driver = (*Gofile)(nil)

drivers/gofile/meta.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
package gofile
22

33
import (
4-
"github.com/alist-org/alist/v3/internal/driver"
5-
"github.com/alist-org/alist/v3/internal/op"
4+
"github.com/alist-org/alist/v3/internal/driver"
5+
"github.com/alist-org/alist/v3/internal/op"
66
)
77

88
type Addition struct {
9-
driver.RootID
10-
APIToken string `json:"api_token" required:"true" help:"Get your API token from your Gofile profile page"`
9+
driver.RootID
10+
APIToken string `json:"api_token" required:"true" help:"Get your API token from your Gofile profile page"`
11+
LinkExpiry int `json:"link_expiry" type:"number" default:"30" help:"Direct link cache duration in days. Set to 0 to disable caching"`
12+
DirectLinkExpiry int `json:"direct_link_expiry" type:"number" default:"0" help:"Direct link expiration time in hours on Gofile server. Set to 0 for no expiration"`
1113
}
1214

1315
var config = driver.Config{
14-
Name: "Gofile",
15-
DefaultRoot: "",
16-
LocalSort: false,
17-
OnlyProxy: false,
18-
NoCache: false,
19-
NoUpload: false,
16+
Name: "Gofile",
17+
DefaultRoot: "",
18+
LocalSort: false,
19+
OnlyProxy: false,
20+
NoCache: false,
21+
NoUpload: false,
2022
}
2123

2224
func init() {
2325
op.RegisterDriver(func() driver.Driver {
2426
return &Gofile{}
2527
})
26-
}
28+
}

drivers/gofile/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@ func (c *Content) ModifiedTime() time.Time {
121121

122122
func (c *Content) IsDir() bool {
123123
return c.Type == "folder"
124-
}
124+
}

drivers/gofile/util.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"net/http"
1111
"path/filepath"
1212
"strings"
13+
"time"
1314

1415
"github.com/alist-org/alist/v3/drivers/base"
1516
"github.com/alist-org/alist/v3/internal/driver"
1617
"github.com/alist-org/alist/v3/internal/model"
18+
log "github.com/sirupsen/logrus"
1719
)
1820

1921
const (
@@ -137,9 +139,10 @@ func (d *Gofile) deleteJSON(ctx context.Context, endpoint string, data interface
137139

138140
func (d *Gofile) handleError(resp *http.Response) error {
139141
body, _ := io.ReadAll(resp.Body)
142+
log.Debugf("Gofile API error (HTTP %d): %s", resp.StatusCode, string(body))
140143

141144
var errorResp ErrorResponse
142-
if err := json.Unmarshal(body, &errorResp); err == nil {
145+
if err := json.Unmarshal(body, &errorResp); err == nil && errorResp.Status == "error" {
143146
return fmt.Errorf("gofile API error: %s (code: %s)", errorResp.Error.Message, errorResp.Error.Code)
144147
}
145148

@@ -199,6 +202,11 @@ func (d *Gofile) uploadFile(ctx context.Context, folderId string, file model.Fil
199202
func (d *Gofile) createDirectLink(ctx context.Context, contentId string) (string, error) {
200203
data := map[string]interface{}{}
201204

205+
if d.DirectLinkExpiry > 0 {
206+
expireTime := time.Now().Add(time.Duration(d.DirectLinkExpiry) * time.Hour).Unix()
207+
data["expireTime"] = expireTime
208+
}
209+
202210
var result DirectLinkResponse
203211
err := d.postJSON(ctx, fmt.Sprintf("/contents/%s/directlinks", contentId), data, &result)
204212
if err != nil {

0 commit comments

Comments
 (0)