Skip to content

Commit 18c82e7

Browse files
committed
feat: sftp support
1 parent d69d24a commit 18c82e7

File tree

7 files changed

+367
-6
lines changed

7 files changed

+367
-6
lines changed

drivers/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
_ "github.com/Xhofe/alist/drivers/pikpak"
1919
_ "github.com/Xhofe/alist/drivers/quark"
2020
_ "github.com/Xhofe/alist/drivers/s3"
21+
_ "github.com/Xhofe/alist/drivers/sftp"
2122
_ "github.com/Xhofe/alist/drivers/shandian"
2223
_ "github.com/Xhofe/alist/drivers/teambition"
2324
_ "github.com/Xhofe/alist/drivers/uss"

drivers/sftp/driver.go

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package template
2+
3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/drivers/base"
6+
"github.com/Xhofe/alist/model"
7+
"github.com/Xhofe/alist/utils"
8+
"io"
9+
"path"
10+
"path/filepath"
11+
)
12+
13+
type SFTP struct {
14+
}
15+
16+
func (driver SFTP) Config() base.DriverConfig {
17+
return base.DriverConfig{
18+
Name: "SFTP",
19+
OnlyProxy: true,
20+
OnlyLocal: true,
21+
LocalSort: true,
22+
}
23+
}
24+
25+
func (driver SFTP) Items() []base.Item {
26+
// TODO fill need info
27+
return []base.Item{
28+
{
29+
Name: "site_url",
30+
Label: "ip/host",
31+
Type: base.TypeString,
32+
Required: true,
33+
},
34+
{
35+
Name: "limit",
36+
Label: "port",
37+
Type: base.TypeNumber,
38+
Required: true,
39+
Default: "22",
40+
},
41+
{
42+
Name: "username",
43+
Label: "username",
44+
Type: base.TypeString,
45+
Required: true,
46+
},
47+
{
48+
Name: "password",
49+
Label: "password",
50+
Type: base.TypeString,
51+
Required: true,
52+
},
53+
{
54+
Name: "root_folder",
55+
Label: "root folder path",
56+
Type: base.TypeString,
57+
Default: "/",
58+
Required: true,
59+
},
60+
}
61+
}
62+
63+
func (driver SFTP) Save(account *model.Account, old *model.Account) error {
64+
if old != nil {
65+
clientsMap.Lock()
66+
defer clientsMap.Unlock()
67+
delete(clientsMap.clients, old.Name)
68+
}
69+
if account == nil {
70+
return nil
71+
}
72+
_, err := GetClient(account)
73+
if err != nil {
74+
account.Status = err.Error()
75+
} else {
76+
account.Status = "work"
77+
}
78+
_ = model.SaveAccount(account)
79+
return err
80+
}
81+
82+
func (driver SFTP) File(path string, account *model.Account) (*model.File, error) {
83+
path = utils.ParsePath(path)
84+
if path == "/" {
85+
return &model.File{
86+
Id: account.RootFolder,
87+
Name: account.Name,
88+
Size: 0,
89+
Type: conf.FOLDER,
90+
Driver: driver.Config().Name,
91+
UpdatedAt: account.UpdatedAt,
92+
}, nil
93+
}
94+
dir, name := filepath.Split(path)
95+
files, err := driver.Files(dir, account)
96+
if err != nil {
97+
return nil, err
98+
}
99+
for _, file := range files {
100+
if file.Name == name {
101+
return &file, nil
102+
}
103+
}
104+
return nil, base.ErrPathNotFound
105+
}
106+
107+
func (driver SFTP) Files(path string, account *model.Account) ([]model.File, error) {
108+
path = utils.ParsePath(path)
109+
remotePath := utils.Join(account.RootFolder, path)
110+
cache, err := base.GetCache(path, account)
111+
if err == nil {
112+
files, _ := cache.([]model.File)
113+
return files, nil
114+
}
115+
client, err := GetClient(account)
116+
if err != nil {
117+
return nil, err
118+
}
119+
var files []model.File
120+
rawFiles, err := client.Files(remotePath)
121+
if err != nil {
122+
return nil, err
123+
}
124+
for i := 0; i < len(rawFiles); i++ {
125+
files = append(files, driver.formatFile(rawFiles[i]))
126+
}
127+
if len(files) > 0 {
128+
_ = base.SetCache(path, files, account)
129+
}
130+
return files, nil
131+
}
132+
133+
func (driver SFTP) Link(args base.Args, account *model.Account) (*base.Link, error) {
134+
client, err := GetClient(account)
135+
if err != nil {
136+
return nil, err
137+
}
138+
remoteFileName := utils.Join(account.RootFolder, args.Path)
139+
remoteFile, err := client.Open(remoteFileName)
140+
if err != nil {
141+
return nil, err
142+
}
143+
return &base.Link{
144+
Data: remoteFile,
145+
}, nil
146+
}
147+
148+
func (driver SFTP) Path(path string, account *model.Account) (*model.File, []model.File, error) {
149+
path = utils.ParsePath(path)
150+
file, err := driver.File(path, account)
151+
if err != nil {
152+
return nil, nil, err
153+
}
154+
if !file.IsDir() {
155+
return file, nil, nil
156+
}
157+
files, err := driver.Files(path, account)
158+
if err != nil {
159+
return nil, nil, err
160+
}
161+
return nil, files, nil
162+
}
163+
164+
func (driver SFTP) Preview(path string, account *model.Account) (interface{}, error) {
165+
//TODO preview interface if driver support
166+
return nil, base.ErrNotImplement
167+
}
168+
169+
func (driver SFTP) MakeDir(path string, account *model.Account) error {
170+
client, err := GetClient(account)
171+
if err != nil {
172+
return err
173+
}
174+
return client.MkdirAll(utils.Join(account.RootFolder, path))
175+
}
176+
177+
func (driver SFTP) Move(src string, dst string, account *model.Account) error {
178+
return driver.Rename(src, dst, account)
179+
}
180+
181+
func (driver SFTP) Rename(src string, dst string, account *model.Account) error {
182+
client, err := GetClient(account)
183+
if err != nil {
184+
return err
185+
}
186+
return client.Rename(utils.Join(account.RootFolder, src), utils.Join(account.RootFolder, dst))
187+
}
188+
189+
func (driver SFTP) Copy(src string, dst string, account *model.Account) error {
190+
return base.ErrNotSupport
191+
}
192+
193+
func (driver SFTP) Delete(path string, account *model.Account) error {
194+
client, err := GetClient(account)
195+
if err != nil {
196+
return err
197+
}
198+
return client.Remove(utils.Join(account.RootFolder, path))
199+
}
200+
201+
func (driver SFTP) Upload(file *model.FileStream, account *model.Account) error {
202+
if file == nil {
203+
return base.ErrEmptyFile
204+
}
205+
client, err := GetClient(account)
206+
if err != nil {
207+
return err
208+
}
209+
dstFile, err := client.Create(path.Join(account.RootFolder, file.ParentPath, file.Name))
210+
if err != nil {
211+
return err
212+
}
213+
defer func() {
214+
_ = dstFile.Close()
215+
}()
216+
_, err = io.Copy(dstFile, file)
217+
return err
218+
}
219+
220+
var _ base.Driver = (*SFTP)(nil)

drivers/sftp/sftp.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package template
2+
3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/drivers/base"
6+
"github.com/Xhofe/alist/model"
7+
"github.com/Xhofe/alist/utils"
8+
"github.com/pkg/sftp"
9+
"golang.org/x/crypto/ssh"
10+
"os"
11+
"path"
12+
"sync"
13+
)
14+
15+
var clientsMap = struct {
16+
sync.Mutex
17+
clients map[string]*Client
18+
}{clients: make(map[string]*Client)}
19+
20+
func GetClient(account *model.Account) (*Client, error) {
21+
clientsMap.Lock()
22+
defer clientsMap.Unlock()
23+
if v, ok := clientsMap.clients[account.Name]; ok {
24+
return v, nil
25+
}
26+
conn, err := ssh.Dial("tcp", account.SiteUrl, &ssh.ClientConfig{
27+
User: account.Username,
28+
Auth: []ssh.AuthMethod{ssh.Password(account.Password)},
29+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
30+
})
31+
if err != nil {
32+
return nil, err
33+
}
34+
client, err := sftp.NewClient(conn)
35+
if err != nil {
36+
return nil, err
37+
}
38+
c := &Client{client}
39+
clientsMap.clients[account.Name] = c
40+
return c, nil
41+
}
42+
43+
type Client struct {
44+
*sftp.Client
45+
}
46+
47+
func (client *Client) Files(remotePath string) ([]os.FileInfo, error) {
48+
return client.ReadDir(remotePath)
49+
}
50+
51+
func (client *Client) Remove(remotePath string) error {
52+
f, err := client.Stat(remotePath)
53+
if err != nil {
54+
return nil
55+
}
56+
if f.IsDir() {
57+
return client.removeDirectory(remotePath)
58+
} else {
59+
return client.removeFile(remotePath)
60+
}
61+
}
62+
63+
func (client *Client) removeDirectory(remotePath string) error {
64+
//打不开,说明要么文件路径错误了,要么是第一次部署
65+
remoteFiles, err := client.ReadDir(remotePath)
66+
if err != nil {
67+
return err
68+
}
69+
for _, backupDir := range remoteFiles {
70+
remoteFilePath := path.Join(remotePath, backupDir.Name())
71+
if backupDir.IsDir() {
72+
err := client.removeDirectory(remoteFilePath)
73+
if err != nil {
74+
return err
75+
}
76+
} else {
77+
err := client.Remove(path.Join(remoteFilePath))
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
}
83+
return client.RemoveDirectory(remotePath)
84+
}
85+
86+
func (client *Client) removeFile(remotePath string) error {
87+
return client.Remove(utils.Join(remotePath))
88+
}
89+
90+
func (driver SFTP) formatFile(f os.FileInfo) model.File {
91+
t := f.ModTime()
92+
file := model.File{
93+
//Id: f.Id,
94+
Name: f.Name(),
95+
Size: f.Size(),
96+
Driver: driver.Config().Name,
97+
UpdatedAt: &t,
98+
}
99+
if f.IsDir() {
100+
file.Type = conf.FOLDER
101+
} else {
102+
file.Type = utils.GetFileType(path.Ext(f.Name()))
103+
}
104+
return file
105+
}
106+
107+
func init() {
108+
base.RegisterDriver(&SFTP{})
109+
}

drivers/sftp/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package template
2+
3+
import "time"
4+
5+
// write all struct here
6+
7+
type Resp struct {
8+
Code int `json:"code"`
9+
Message string `json:"message"`
10+
}
11+
12+
type File struct {
13+
Id string `json:"id"`
14+
FileName string `json:"file_name"`
15+
Size int64 `json:"size"`
16+
File bool `json:"file"`
17+
UpdatedAt *time.Time `json:"updated_at"`
18+
}

drivers/sftp/util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package template
2+
3+
// write util func here, such as cal sign

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/jlaffaye/ftp v0.0.0-20211117213618-11820403398b
1414
github.com/json-iterator/go v1.1.12
1515
github.com/patrickmn/go-cache v2.1.0+incompatible
16+
github.com/pkg/sftp v1.13.4
1617
github.com/robfig/cron/v3 v3.0.0
1718
github.com/sirupsen/logrus v1.8.1
1819
github.com/studio-b12/gowebdav v0.0.0-20211109083228-3f8721cd4b6f
@@ -24,6 +25,8 @@ require (
2425
gorm.io/gorm v1.23.1
2526
)
2627

28+
require github.com/kr/fs v0.1.0 // indirect
29+
2730
require (
2831
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
2932
github.com/fatih/color v1.13.0
@@ -73,9 +76,9 @@ require (
7376
go.opentelemetry.io/otel v0.20.0 // indirect
7477
go.opentelemetry.io/otel/metric v0.20.0 // indirect
7578
go.opentelemetry.io/otel/trace v0.20.0 // indirect
76-
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
79+
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29
7780
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
78-
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 // indirect
81+
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
7982
google.golang.org/protobuf v1.27.1 // indirect
8083
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
8184
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect

0 commit comments

Comments
 (0)