Skip to content

Commit 3abdf79

Browse files
sorubedoclaude
andauthored
fix(drivers/ftp): add cwd_list option and filter path-separated entries (#2621)
1. Add `cwd_list` option to switch LIST command logic Use CWD + LIST "" instead of LIST <path> to fix garbled non-UTF8 paths on legacy Chinese FTP servers, eliminate fake duplicate folders. 2. Filter entries with "/" in filename Skip cdir marker entries carrying full path names, compliant with FTP filename specification. Co-authored-by: Claude <81847+claude@users.noreply.github.com>
1 parent 3b017c2 commit 3abdf79

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎drivers/ftp/driver.go‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
stdpath "path"
8+
"strings"
89

910
"github.com/OpenListTeam/OpenList/v4/internal/driver"
1011
"github.com/OpenListTeam/OpenList/v4/internal/errs"
@@ -51,13 +52,35 @@ func (d *FTP) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]m
5152
if err := d.login(); err != nil {
5253
return nil, err
5354
}
54-
entries, err := d.conn.List(encode(dir.GetPath(), d.Encoding))
55+
56+
path := encode(dir.GetPath(), d.Encoding)
57+
58+
var entries []*ftp.Entry
59+
var err error
60+
61+
if d.CwdList {
62+
origDir, cwdErr := d.conn.CurrentDir()
63+
if cwdErr != nil {
64+
return nil, cwdErr
65+
}
66+
if cwdErr = d.conn.ChangeDir(path); cwdErr != nil {
67+
return nil, cwdErr
68+
}
69+
entries, err = d.conn.List("")
70+
if restoreErr := d.conn.ChangeDir(origDir); restoreErr != nil {
71+
d.conn = nil
72+
}
73+
} else {
74+
entries, err = d.conn.List(path)
75+
}
76+
5577
if err != nil {
5678
return nil, err
5779
}
80+
5881
res := make([]model.Obj, 0)
5982
for _, entry := range entries {
60-
if entry.Name == "." || entry.Name == ".." {
83+
if entry.Name == "." || entry.Name == ".." || strings.Contains(entry.Name, "/") {
6184
continue
6285
}
6386
name := decode(entry.Name, d.Encoding)

‎drivers/ftp/meta.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Addition struct {
2727
Encoding string `json:"encoding" required:"true"`
2828
Username string `json:"username" required:"true"`
2929
Password string `json:"password" required:"true"`
30+
CwdList bool `json:"cwd_list" type:"bool" default:"false" help:"enter directory before listing"`
3031
driver.RootPath
3132
}
3233

0 commit comments

Comments
 (0)