2022-09-03 14:07:08 +00:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
stdpath "path"
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/jlaffaye/ftp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FTP struct {
|
|
|
|
model.Storage
|
|
|
|
Addition
|
|
|
|
conn *ftp.ServerConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Config() driver.Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
return &d.Addition
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 10:03:30 +00:00
|
|
|
func (d *FTP) Init(ctx context.Context) error {
|
2022-09-03 14:07:08 +00:00
|
|
|
return d.login()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Drop(ctx context.Context) error {
|
|
|
|
if d.conn != nil {
|
|
|
|
_ = d.conn.Logout()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-16 08:58:02 +00:00
|
|
|
entries, err := d.conn.List(encode(dir.GetPath(), d.Encoding))
|
2022-09-03 14:07:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := make([]model.Obj, 0)
|
2023-03-11 13:02:47 +00:00
|
|
|
for _, entry := range entries {
|
2022-09-03 14:07:08 +00:00
|
|
|
if entry.Name == "." || entry.Name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f := model.Object{
|
2024-06-16 08:58:02 +00:00
|
|
|
Name: decode(entry.Name, d.Encoding),
|
2022-09-03 14:07:08 +00:00
|
|
|
Size: int64(entry.Size),
|
|
|
|
Modified: entry.Time,
|
|
|
|
IsFolder: entry.Type == ftp.EntryTypeFolder,
|
|
|
|
}
|
|
|
|
res = append(res, &f)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-11 13:02:47 +00:00
|
|
|
|
2024-06-16 08:58:02 +00:00
|
|
|
r := NewFileReader(d.conn, encode(file.GetPath(), d.Encoding), file.GetSize())
|
2023-03-11 13:02:47 +00:00
|
|
|
link := &model.Link{
|
2023-08-27 13:14:23 +00:00
|
|
|
MFile: r,
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
2023-03-11 13:02:47 +00:00
|
|
|
return link, nil
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-16 08:58:02 +00:00
|
|
|
return d.conn.MakeDir(encode(stdpath.Join(parentDir.GetPath(), dirName), d.Encoding))
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-16 08:58:02 +00:00
|
|
|
return d.conn.Rename(
|
|
|
|
encode(srcObj.GetPath(), d.Encoding),
|
|
|
|
encode(stdpath.Join(dstDir.GetPath(), srcObj.GetName()), d.Encoding),
|
|
|
|
)
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-16 08:58:02 +00:00
|
|
|
return d.conn.Rename(
|
|
|
|
encode(srcObj.GetPath(), d.Encoding),
|
|
|
|
encode(stdpath.Join(stdpath.Dir(srcObj.GetPath()), newName), d.Encoding),
|
|
|
|
)
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
return errs.NotSupport
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Remove(ctx context.Context, obj model.Obj) error {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-16 08:58:02 +00:00
|
|
|
path := encode(obj.GetPath(), d.Encoding)
|
2022-09-03 14:07:08 +00:00
|
|
|
if obj.IsDir() {
|
2024-06-16 08:58:02 +00:00
|
|
|
return d.conn.RemoveDirRecur(path)
|
2022-09-03 14:07:08 +00:00
|
|
|
} else {
|
2024-06-16 08:58:02 +00:00
|
|
|
return d.conn.Delete(path)
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *FTP) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
|
|
|
if err := d.login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-21 07:03:09 +00:00
|
|
|
// TODO: support cancel
|
2024-06-16 08:58:02 +00:00
|
|
|
path := stdpath.Join(dstDir.GetPath(), stream.GetName())
|
|
|
|
return d.conn.Stor(encode(path, d.Encoding), stream)
|
2022-09-03 14:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.Driver = (*FTP)(nil)
|