-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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/alist-org/alist/v3/pkg/utils" | ||
"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 { | ||
return d.Addition | ||
} | ||
|
||
func (d *FTP) Init(ctx context.Context, storage model.Storage) error { | ||
d.Storage = storage | ||
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} | ||
entries, err := d.conn.List(dir.GetPath()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := make([]model.Obj, 0) | ||
for i, _ := range entries { | ||
entry := entries[i] | ||
if entry.Name == "." || entry.Name == ".." { | ||
continue | ||
} | ||
f := model.Object{ | ||
Name: entry.Name, | ||
Size: int64(entry.Size), | ||
Modified: entry.Time, | ||
IsFolder: entry.Type == ftp.EntryTypeFolder, | ||
} | ||
res = append(res, &f) | ||
} | ||
return res, nil | ||
} | ||
|
||
//func (d *FTP) Get(ctx context.Context, path string) (model.Obj, error) { | ||
// // this is optional | ||
// return nil, errs.NotImplement | ||
//} | ||
|
||
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 | ||
} | ||
resp, err := d.conn.Retr(file.GetPath()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &model.Link{ | ||
Data: resp, | ||
}, nil | ||
} | ||
|
||
func (d *FTP) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { | ||
if err := d.login(); err != nil { | ||
return err | ||
} | ||
return d.conn.MakeDir(stdpath.Join(parentDir.GetPath(), dirName)) | ||
} | ||
|
||
func (d *FTP) Move(ctx context.Context, srcObj, dstDir model.Obj) error { | ||
if err := d.login(); err != nil { | ||
return err | ||
} | ||
return d.conn.Rename(srcObj.GetPath(), stdpath.Join(dstDir.GetPath(), srcObj.GetName())) | ||
} | ||
|
||
func (d *FTP) Rename(ctx context.Context, srcObj model.Obj, newName string) error { | ||
if err := d.login(); err != nil { | ||
return err | ||
} | ||
return d.conn.Rename(srcObj.GetPath(), stdpath.Join(stdpath.Dir(srcObj.GetPath()), newName)) | ||
} | ||
|
||
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 | ||
} | ||
if obj.IsDir() { | ||
return d.conn.RemoveDirRecur(obj.GetPath()) | ||
} else { | ||
return d.conn.Delete(obj.GetPath()) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
return d.conn.Stor(stdpath.Join(dstDir.GetPath(), stream.GetName()), stream) | ||
} | ||
|
||
func (d *FTP) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) { | ||
return nil, errs.NotSupport | ||
} | ||
|
||
var _ driver.Driver = (*FTP)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ftp | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/driver" | ||
"github.com/alist-org/alist/v3/internal/op" | ||
) | ||
|
||
type Addition struct { | ||
Address string `json:"address" required:"true"` | ||
Username string `json:"username" required:"true"` | ||
Password string `json:"password" required:"true"` | ||
driver.RootFolderPath | ||
} | ||
|
||
var config = driver.Config{ | ||
Name: "FTP", | ||
LocalSort: false, | ||
OnlyLocal: false, | ||
OnlyProxy: false, | ||
NoCache: false, | ||
NoUpload: false, | ||
NeedMs: false, | ||
DefaultRoot: "root, / or other", | ||
} | ||
|
||
func New() driver.Driver { | ||
return &FTP{} | ||
} | ||
|
||
func init() { | ||
op.RegisterDriver(config, New) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package ftp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ftp | ||
|
||
import "github.com/jlaffaye/ftp" | ||
|
||
// do others that not defined in Driver interface | ||
|
||
func (d *FTP) login() error { | ||
if d.conn != nil { | ||
_, err := d.conn.CurrentDir() | ||
if err == nil { | ||
return nil | ||
} | ||
} | ||
conn, err := ftp.Dial(d.Address) | ||
if err != nil { | ||
return err | ||
} | ||
err = conn.Login(d.Username, d.Password) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters