Skip to content

Commit

Permalink
Update alist.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakura-Byte committed Jan 26, 2025
1 parent 5572538 commit fc8f8c5
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions backend/alist/alist.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
apiMkdir = "/api/fs/mkdir"
apiRemove = "/api/fs/remove"
apiGet = "/api/fs/get"
apiMe = "/api/me"
)

func init() {
Expand Down Expand Up @@ -121,6 +122,7 @@ type Fs struct {
fileListCacheMu sync.Mutex
fileListCache map[string]listResponse

userPermission int
// New fields for Cloudflare handling
cfCookie *http.Cookie
cfCookieExpiry time.Time
Expand All @@ -137,6 +139,14 @@ type loginResponse struct {
} `json:"data"`
}

type meResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Permission int `json:"permission"`
} `json:"data"`
}

type fileInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
Expand Down Expand Up @@ -202,6 +212,11 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
return nil, err
}

// Ensure url does not end with '/'
if strings.HasSuffix(opt.URL, "/") {

Check failure on line 216 in backend/alist/alist.go

View workflow job for this annotation

GitHub Actions / lint

S1017: should replace this if statement with an unconditional strings.TrimSuffix (gosimple)

Check failure on line 216 in backend/alist/alist.go

View workflow job for this annotation

GitHub Actions / lint

S1017: should replace this if statement with an unconditional strings.TrimSuffix (gosimple)
opt.URL = opt.URL[:len(opt.URL)-1]
}

// Ensure root starts with '/'
if !strings.HasPrefix(root, "/") {
root = "/" + root
Expand Down Expand Up @@ -245,6 +260,14 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
f.token = ""
}

// Get user permissions
var meResp meResponse
err = f.makeRequest(ctx, "GET", apiMe, nil, &meResp)
if err != nil {
return nil, fmt.Errorf("failed to retrieve user permissions: %w", err)
}
f.userPermission = meResp.Data.Permission

// Set supported hash types
f.features = (&fs.Features{
CanHaveEmptyDirectories: true,
Expand Down Expand Up @@ -480,9 +503,11 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
"path": path.Join(f.root, dir),
"per_page": 1000,
"page": 1,
"refresh": true,
"password": f.opt.MetaPass,
}
if f.userPermission == 2 {
data["refresh"] = true
}

var listResp listResponse
err = f.makeRequest(ctx, "POST", apiList, data, &listResp)
Expand Down Expand Up @@ -817,7 +842,11 @@ func (f *Fs) fetchCloudflare(ctx context.Context) error {
if err != nil {
return err
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
fs.Errorf(ctx, "Failed to close response body: %v", err)
}
}()

var cookieData struct {
Cookies []struct {
Expand Down

0 comments on commit fc8f8c5

Please sign in to comment.