From 8036a7faee472256524ff11745bb9e20e3b01590 Mon Sep 17 00:00:00 2001 From: destrodxbad Date: Tue, 16 Sep 2025 10:13:10 +0200 Subject: [PATCH] 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). --- drivers/gofile/driver.go | 18 ++++++++++++++---- drivers/gofile/meta.go | 24 +++++++++++++----------- drivers/gofile/types.go | 2 +- drivers/gofile/util.go | 10 +++++++++- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/drivers/gofile/driver.go b/drivers/gofile/driver.go index 301eaef3..8046bd16 100644 --- a/drivers/gofile/driver.go +++ b/drivers/gofile/driver.go @@ -3,6 +3,7 @@ package gofile import ( "context" "fmt" + "time" "github.com/alist-org/alist/v3/internal/driver" "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) ( } var objects []model.Obj - + // Process children or contents contents := response.Data.Children if contents == nil { @@ -97,9 +98,18 @@ func (d *Gofile) Link(ctx context.Context, file model.Obj, args model.LinkArgs) return nil, fmt.Errorf("failed to create direct link: %w", err) } - return &model.Link{ + // Configure cache expiration based on user setting + link := &model.Link{ URL: directLink, - }, nil + } + + // Only set expiration if LinkExpiry > 0 (0 means no caching) + if d.LinkExpiry > 0 { + expiration := time.Duration(d.LinkExpiry) * 24 * time.Hour + link.Expiration = &expiration + } + + return link, nil } 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 return nil, errs.NotImplement } -var _ driver.Driver = (*Gofile)(nil) \ No newline at end of file +var _ driver.Driver = (*Gofile)(nil) diff --git a/drivers/gofile/meta.go b/drivers/gofile/meta.go index b8126e33..00656025 100644 --- a/drivers/gofile/meta.go +++ b/drivers/gofile/meta.go @@ -1,26 +1,28 @@ package gofile import ( - "github.com/alist-org/alist/v3/internal/driver" - "github.com/alist-org/alist/v3/internal/op" + "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/op" ) type Addition struct { - driver.RootID - APIToken string `json:"api_token" required:"true" help:"Get your API token from your Gofile profile page"` + driver.RootID + APIToken string `json:"api_token" required:"true" help:"Get your API token from your Gofile profile page"` + LinkExpiry int `json:"link_expiry" type:"number" default:"30" help:"Direct link cache duration in days. Set to 0 to disable caching"` + 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"` } var config = driver.Config{ - Name: "Gofile", - DefaultRoot: "", - LocalSort: false, - OnlyProxy: false, - NoCache: false, - NoUpload: false, + Name: "Gofile", + DefaultRoot: "", + LocalSort: false, + OnlyProxy: false, + NoCache: false, + NoUpload: false, } func init() { op.RegisterDriver(func() driver.Driver { return &Gofile{} }) -} \ No newline at end of file +} diff --git a/drivers/gofile/types.go b/drivers/gofile/types.go index 93c9f5d2..be307347 100644 --- a/drivers/gofile/types.go +++ b/drivers/gofile/types.go @@ -121,4 +121,4 @@ func (c *Content) ModifiedTime() time.Time { func (c *Content) IsDir() bool { return c.Type == "folder" -} \ No newline at end of file +} diff --git a/drivers/gofile/util.go b/drivers/gofile/util.go index 5f39dae5..1dd6229a 100644 --- a/drivers/gofile/util.go +++ b/drivers/gofile/util.go @@ -10,10 +10,12 @@ import ( "net/http" "path/filepath" "strings" + "time" "github.com/alist-org/alist/v3/drivers/base" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" + log "github.com/sirupsen/logrus" ) const ( @@ -137,9 +139,10 @@ func (d *Gofile) deleteJSON(ctx context.Context, endpoint string, data interface func (d *Gofile) handleError(resp *http.Response) error { body, _ := io.ReadAll(resp.Body) + log.Debugf("Gofile API error (HTTP %d): %s", resp.StatusCode, string(body)) var errorResp ErrorResponse - if err := json.Unmarshal(body, &errorResp); err == nil { + if err := json.Unmarshal(body, &errorResp); err == nil && errorResp.Status == "error" { return fmt.Errorf("gofile API error: %s (code: %s)", errorResp.Error.Message, errorResp.Error.Code) } @@ -199,6 +202,11 @@ func (d *Gofile) uploadFile(ctx context.Context, folderId string, file model.Fil func (d *Gofile) createDirectLink(ctx context.Context, contentId string) (string, error) { data := map[string]interface{}{} + if d.DirectLinkExpiry > 0 { + expireTime := time.Now().Add(time.Duration(d.DirectLinkExpiry) * time.Hour).Unix() + data["expireTime"] = expireTime + } + var result DirectLinkResponse err := d.postJSON(ctx, fmt.Sprintf("/contents/%s/directlinks", contentId), data, &result) if err != nil {