From e76fc3e61669d3041b3b03c735ba6516485da1c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Sat, 27 Nov 2021 20:07:32 +0800 Subject: [PATCH] :hammer: refactor driver: native --- bootstrap/drivers.go | 1 + drivers/native.go | 141 ----------------------------------- drivers/native/driver.go | 157 +++++++++++++++++++++++++++++++++++++++ drivers/native/native.go | 9 +++ 4 files changed, 167 insertions(+), 141 deletions(-) delete mode 100644 drivers/native.go create mode 100644 drivers/native/driver.go create mode 100644 drivers/native/native.go diff --git a/bootstrap/drivers.go b/bootstrap/drivers.go index e09f9016..826200ce 100644 --- a/bootstrap/drivers.go +++ b/bootstrap/drivers.go @@ -5,4 +5,5 @@ import ( _ "github.com/Xhofe/alist/drivers/189cloud" _ "github.com/Xhofe/alist/drivers/alidrive" _ "github.com/Xhofe/alist/drivers/googledrive" + _ "github.com/Xhofe/alist/drivers/native" ) \ No newline at end of file diff --git a/drivers/native.go b/drivers/native.go deleted file mode 100644 index dc2c10cd..00000000 --- a/drivers/native.go +++ /dev/null @@ -1,141 +0,0 @@ -package drivers - -import ( - "fmt" - "github.com/Xhofe/alist/conf" - "github.com/Xhofe/alist/model" - "github.com/Xhofe/alist/utils" - "github.com/gin-gonic/gin" - log "github.com/sirupsen/logrus" - "io/ioutil" - "os" - "path/filepath" - "strings" -) - -type Native struct { -} - -func (n Native) File(path string, account *model.Account) (*model.File, error) { - panic("implement me") -} - -func (n Native) Files(path string, account *model.Account) ([]model.File, error) { - panic("implement me") -} - -func (n Native) Preview(path string, account *model.Account) (interface{}, error) { - return nil, fmt.Errorf("no need") -} - -func (n Native) Items() []Item { - return []Item{ - { - Name: "root_folder", - Label: "root folder path", - Type: "string", - Required: true, - }, - { - Name: "order_by", - Label: "order_by", - Type: "select", - Values: "name,size,updated_at", - Required: false, - }, - { - Name: "order_direction", - Label: "order_direction", - Type: "select", - Values: "ASC,DESC", - Required: false, - }, - } -} - -func (n Native) Proxy(c *gin.Context, account *model.Account) { - // unnecessary -} - -func (n Native) Save(account *model.Account, old *model.Account) error { - log.Debugf("save a account: [%s]", account.Name) - if !utils.Exists(account.RootFolder) { - account.Status = fmt.Sprintf("[%s] not exist", account.RootFolder) - _ = model.SaveAccount(account) - return fmt.Errorf("[%s] not exist", account.RootFolder) - } - account.Status = "work" - account.Proxy = true - err := model.SaveAccount(account) - if err != nil { - return err - } - return nil -} - -func (n Native) Path(path string, account *model.Account) (*model.File, []model.File, error) { - fullPath := filepath.Join(account.RootFolder, path) - log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath) - if !utils.Exists(fullPath) { - return nil, nil, fmt.Errorf("path not found") - } - if utils.IsDir(fullPath) { - result := make([]model.File, 0) - files, err := ioutil.ReadDir(fullPath) - if err != nil { - return nil, nil, err - } - for _, f := range files { - if strings.HasPrefix(f.Name(), ".") { - continue - } - time := f.ModTime() - file := model.File{ - Name: f.Name(), - Size: f.Size(), - Type: 0, - UpdatedAt: &time, - Driver: "Native", - } - if f.IsDir() { - file.Type = conf.FOLDER - } else { - file.Type = utils.GetFileType(filepath.Ext(f.Name())) - } - result = append(result, file) - } - model.SortFiles(result, account) - return nil, result, nil - } - f, err := os.Stat(fullPath) - if err != nil { - return nil, nil, err - } - time := f.ModTime() - file := &model.File{ - Name: f.Name(), - Size: f.Size(), - Type: utils.GetFileType(filepath.Ext(f.Name())), - UpdatedAt: &time, - Driver: "Native", - } - return file, nil, nil -} - -func (n Native) Link(path string, account *model.Account) (string, error) { - fullPath := filepath.Join(account.RootFolder, path) - s, err := os.Stat(fullPath) - if err != nil { - return "", err - } - if s.IsDir() { - return "", fmt.Errorf("can't down folder") - } - return fullPath, nil -} - -var _ Driver = (*Native)(nil) - -func init() { - RegisterDriver("Native", &Native{}) -} diff --git a/drivers/native/driver.go b/drivers/native/driver.go new file mode 100644 index 00000000..e0436f53 --- /dev/null +++ b/drivers/native/driver.go @@ -0,0 +1,157 @@ +package native + +import ( + "fmt" + "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/model" + "github.com/Xhofe/alist/utils" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "io/ioutil" + "os" + "path/filepath" + "strings" +) + +type Native struct{} + +var driverName = "Native" + +func (driver Native) Items() []drivers.Item { + return []drivers.Item{ + { + Name: "root_folder", + Label: "root folder path", + Type: "string", + Required: true, + }, + { + Name: "order_by", + Label: "order_by", + Type: "select", + Values: "name,size,updated_at", + Required: false, + }, + { + Name: "order_direction", + Label: "order_direction", + Type: "select", + Values: "ASC,DESC", + Required: false, + }, + } +} + +func (driver Native) Save(account *model.Account, old *model.Account) error { + log.Debugf("save a account: [%s]", account.Name) + if !utils.Exists(account.RootFolder) { + account.Status = fmt.Sprintf("[%s] not exist", account.RootFolder) + _ = model.SaveAccount(account) + return fmt.Errorf("[%s] not exist", account.RootFolder) + } + account.Status = "work" + account.Proxy = true + err := model.SaveAccount(account) + if err != nil { + return err + } + return nil +} + +func (driver Native) File(path string, account *model.Account) (*model.File, error) { + fullPath := filepath.Join(account.RootFolder, path) + if !utils.Exists(fullPath) { + return nil, drivers.PathNotFound + } + f, err := os.Stat(fullPath) + if err != nil { + return nil, err + } + time := f.ModTime() + file := &model.File{ + Name: f.Name(), + Size: f.Size(), + UpdatedAt: &time, + Driver: driverName, + } + if f.IsDir() { + file.Type = conf.FOLDER + } else { + file.Type = utils.GetFileType(filepath.Ext(f.Name())) + } + return file, nil +} + +func (driver Native) Files(path string, account *model.Account) ([]model.File, error) { + fullPath := filepath.Join(account.RootFolder, path) + if !utils.Exists(fullPath) { + return nil, drivers.PathNotFound + } + files := make([]model.File, 0) + rawFiles, err := ioutil.ReadDir(fullPath) + if err != nil { + return nil, err + } + for _, f := range rawFiles { + if strings.HasPrefix(f.Name(), ".") { + continue + } + time := f.ModTime() + file := model.File{ + Name: f.Name(), + Size: f.Size(), + Type: 0, + UpdatedAt: &time, + Driver: driverName, + } + if f.IsDir() { + file.Type = conf.FOLDER + } else { + file.Type = utils.GetFileType(filepath.Ext(f.Name())) + } + files = append(files, file) + } + model.SortFiles(files, account) + return files, nil +} + +func (driver Native) Link(path string, account *model.Account) (string, error) { + fullPath := filepath.Join(account.RootFolder, path) + s, err := os.Stat(fullPath) + if err != nil { + return "", err + } + if s.IsDir() { + return "", fmt.Errorf("can't down folder") + } + return fullPath, nil +} + +func (driver Native) Path(path string, account *model.Account) (*model.File, []model.File, error) { + log.Debugf("native path: %s", path) + file, err := driver.File(path, account) + if err != nil { + return nil, nil, err + } + if file.Type != conf.FOLDER { + //file.Url, _ = driver.Link(path, account) + return file, nil, nil + } + files, err := driver.Files(path, account) + if err != nil { + return nil, nil, err + } + model.SortFiles(files, account) + return nil, files, nil +} + +func (driver Native) Proxy(c *gin.Context, account *model.Account) { + // unnecessary +} + +func (driver Native) Preview(path string, account *model.Account) (interface{}, error) { + return nil, fmt.Errorf("no need") +} + +var _ drivers.Driver = (*Native)(nil) diff --git a/drivers/native/native.go b/drivers/native/native.go new file mode 100644 index 00000000..e67a87d1 --- /dev/null +++ b/drivers/native/native.go @@ -0,0 +1,9 @@ +package native + +import ( + "github.com/Xhofe/alist/drivers" +) + +func init() { + drivers.RegisterDriver("Native", &Native{}) +}