From 1779617cb9198afcc3e8492bc8e9f8e1201d3fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Mon, 6 Dec 2021 15:55:05 +0800 Subject: [PATCH] :art: Improve the code structure --- alist.go | 3 +- bootstrap/account.go | 4 +- bootstrap/log.go | 4 ++ drivers/{ => 123}/123.go | 11 ++-- drivers/{123_driver.go => 123/driver.go} | 41 ++++++------- drivers/{ => 189}/189.go | 5 +- drivers/{189_driver.go => 189/driver.go} | 41 ++++++------- drivers/{ => alidrive}/alidrive.go | 13 ++-- drivers/{ali_driver.go => alidrive/driver.go} | 59 ++++++++++--------- drivers/all.go | 11 ++++ drivers/{ => base}/driver.go | 5 +- drivers/{ => base}/types.go | 2 +- .../{google_driver.go => google/driver.go} | 37 ++++++------ drivers/{ => google}/googledrive.go | 7 ++- .../{lanzou_driver.go => lanzou/driver.go} | 37 ++++++------ drivers/{ => lanzou}/lanzou.go | 5 +- .../{native_driver.go => native/driver.go} | 27 +++++---- drivers/{ => native}/native.go | 5 +- drivers/{one_driver.go => onedrive/driver.go} | 47 +++++++-------- drivers/{ => onedrive}/onedrive.go | 7 ++- server/account.go | 6 +- server/common.go | 6 +- server/driver.go | 4 +- server/webdav/file.go | 6 +- 24 files changed, 212 insertions(+), 181 deletions(-) rename drivers/{ => 123}/123.go (95%) rename drivers/{123_driver.go => 123/driver.go} (86%) rename drivers/{ => 189}/189.go (98%) rename drivers/{189_driver.go => 189/driver.go} (87%) rename drivers/{ => alidrive}/alidrive.go (94%) rename drivers/{ali_driver.go => alidrive/driver.go} (86%) create mode 100644 drivers/all.go rename drivers/{ => base}/driver.go (96%) rename drivers/{ => base}/types.go (95%) rename drivers/{google_driver.go => google/driver.go} (87%) rename drivers/{ => google}/googledrive.go (97%) rename drivers/{lanzou_driver.go => lanzou/driver.go} (86%) rename drivers/{ => lanzou}/lanzou.go (98%) rename drivers/{native_driver.go => native/driver.go} (91%) rename drivers/{ => native}/native.go (93%) rename drivers/{one_driver.go => onedrive/driver.go} (86%) rename drivers/{ => onedrive}/onedrive.go (97%) diff --git a/alist.go b/alist.go index dc7a67a8..e2d13237 100644 --- a/alist.go +++ b/alist.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/Xhofe/alist/bootstrap" "github.com/Xhofe/alist/conf" + _ "github.com/Xhofe/alist/drivers" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/server" "github.com/gin-gonic/gin" @@ -20,7 +21,7 @@ func init() { } func Init() bool { - bootstrap.InitLog() + //bootstrap.InitLog() bootstrap.InitConf() bootstrap.InitCron() bootstrap.InitModel() diff --git a/bootstrap/account.go b/bootstrap/account.go index 4ee52acc..dc781b4e 100644 --- a/bootstrap/account.go +++ b/bootstrap/account.go @@ -2,7 +2,7 @@ package bootstrap import ( "github.com/Xhofe/alist/conf" - "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" log "github.com/sirupsen/logrus" ) @@ -15,7 +15,7 @@ func InitAccounts() { } for i, account := range accounts { model.RegisterAccount(account) - driver, ok := drivers.GetDriver(account.Type) + driver, ok := base.GetDriver(account.Type) if !ok { log.Errorf("no [%s] driver", account.Type) } else { diff --git a/bootstrap/log.go b/bootstrap/log.go index 705689c6..2bdc1b19 100644 --- a/bootstrap/log.go +++ b/bootstrap/log.go @@ -19,4 +19,8 @@ func InitLog() { FullTimestamp: true, }) log.Infof("init log...") +} + +func init() { + InitLog() } \ No newline at end of file diff --git a/drivers/123.go b/drivers/123/123.go similarity index 95% rename from drivers/123.go rename to drivers/123/123.go index adcbd1d4..aec4ff05 100644 --- a/drivers/123.go +++ b/drivers/123/123.go @@ -1,8 +1,9 @@ -package drivers +package _23 import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -52,7 +53,7 @@ func (driver Pan123) Login(account *model.Account) error { var resp Pan123TokenResp _, err := pan123Client.R(). SetResult(&resp). - SetBody(Json{ + SetBody(base.Json{ "passport": account.Username, "password": account.Password, }).Post("https://www.123pan.com/api/user/sign_in") @@ -135,14 +136,14 @@ func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, if file.Type != conf.FOLDER { return &file, err } else { - return nil, ErrNotFile + return nil, base.ErrNotFile } } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func init() { - RegisterDriver(&Pan123{}) + base.RegisterDriver(&Pan123{}) pan123Client.SetRetryCount(3) } diff --git a/drivers/123_driver.go b/drivers/123/driver.go similarity index 86% rename from drivers/123_driver.go rename to drivers/123/driver.go index 740b46c6..a440bb6f 100644 --- a/drivers/123_driver.go +++ b/drivers/123/driver.go @@ -1,8 +1,9 @@ -package drivers +package _23 import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -13,46 +14,46 @@ import ( type Pan123 struct {} -func (driver Pan123) Config() DriverConfig { - return DriverConfig{ +func (driver Pan123) Config() base.DriverConfig { + return base.DriverConfig{ Name: "123Pan", OnlyProxy: false, } } -func (driver Pan123) Items() []Item { - return []Item{ +func (driver Pan123) Items() []base.Item { + return []base.Item{ { Name: "username", Label: "username", - Type: TypeString, + Type: base.TypeString, Required: true, Description: "account username/phone number", }, { Name: "password", Label: "password", - Type: TypeString, + Type: base.TypeString, Required: true, Description: "account password", }, { Name: "root_folder", Label: "root folder file_id", - Type: TypeString, + Type: base.TypeString, Required: false, }, { Name: "order_by", Label: "order_by", - Type: TypeSelect, + Type: base.TypeSelect, Values: "name,fileId,updateAt,createAt", Required: true, }, { Name: "order_direction", Label: "order_direction", - Type: TypeSelect, + Type: base.TypeSelect, Values: "asc,desc", Required: true, }, @@ -89,7 +90,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) { @@ -125,7 +126,7 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) { } var resp Pan123DownResp _, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken). - SetBody(Json{ + SetBody(base.Json{ "driveId": 0, "etag": file.Etag, "fileId": file.FileId, @@ -152,7 +153,7 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) { return "", err } u_ := fmt.Sprintf("https://%s%s",u.Host,u.Path) - res, err := NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_) + res, err := base.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_) if err != nil { return "", err } @@ -186,27 +187,27 @@ func (driver Pan123) Proxy(c *gin.Context, account *model.Account) { } func (driver Pan123) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver Pan123) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Pan123) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Pan123) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Pan123) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Pan123) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*Pan123)(nil) \ No newline at end of file +var _ base.Driver = (*Pan123)(nil) \ No newline at end of file diff --git a/drivers/189.go b/drivers/189/189.go similarity index 98% rename from drivers/189.go rename to drivers/189/189.go index 4cb74c65..61562443 100644 --- a/drivers/189.go +++ b/drivers/189/189.go @@ -1,4 +1,4 @@ -package drivers +package _89 import ( "crypto/rand" @@ -9,6 +9,7 @@ import ( "encoding/pem" "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -312,6 +313,6 @@ func b64tohex(a string) string { } func init() { - RegisterDriver(&Cloud189{}) + base.RegisterDriver(&Cloud189{}) client189Map = make(map[string]*resty.Client, 0) } \ No newline at end of file diff --git a/drivers/189_driver.go b/drivers/189/driver.go similarity index 87% rename from drivers/189_driver.go rename to drivers/189/driver.go index 23f16f1e..c30d5416 100644 --- a/drivers/189_driver.go +++ b/drivers/189/driver.go @@ -1,8 +1,9 @@ -package drivers +package _89 import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -12,46 +13,46 @@ import ( type Cloud189 struct {} -func (driver Cloud189) Config() DriverConfig { - return DriverConfig{ +func (driver Cloud189) Config() base.DriverConfig { + return base.DriverConfig{ Name: "189Cloud", OnlyProxy: false, } } -func (driver Cloud189) Items() []Item { - return []Item{ +func (driver Cloud189) Items() []base.Item { + return []base.Item{ { Name: "username", Label: "username", - Type: TypeString, + Type: base.TypeString, Required: true, Description: "account username/phone number", }, { Name: "password", Label: "password", - Type: TypeString, + Type: base.TypeString, Required: true, Description: "account password", }, { Name: "root_folder", Label: "root folder file_id", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "order_by", Label: "order_by", - Type: TypeSelect, + Type: base.TypeSelect, Values: "name,size,lastOpTime,createdDate", Required: true, }, { Name: "order_direction", Label: "desc", - Type: TypeSelect, + Type: base.TypeSelect, Values: "true,false", Required: true, }, @@ -97,7 +98,7 @@ func (driver Cloud189) File(path string, account *model.Account) (*model.File, e return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) { @@ -132,7 +133,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error) return "", err } if file.Type == conf.FOLDER { - return "", ErrNotFile + return "", base.ErrNotFile } client, ok := client189Map[account.Name] if !ok { @@ -161,7 +162,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error) if resp.ResCode != 0 { return "", fmt.Errorf(resp.ResMessage) } - res, err := NoRedirectClient.R().Get(resp.FileDownloadUrl) + res, err := base.NoRedirectClient.R().Get(resp.FileDownloadUrl) if err != nil { return "", err } @@ -194,28 +195,28 @@ func (driver Cloud189) Proxy(ctx *gin.Context, account *model.Account) { } func (driver Cloud189) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver Cloud189) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Cloud189) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Cloud189) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Cloud189) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*Cloud189)(nil) \ No newline at end of file +var _ base.Driver = (*Cloud189)(nil) \ No newline at end of file diff --git a/drivers/alidrive.go b/drivers/alidrive/alidrive.go similarity index 94% rename from drivers/alidrive.go rename to drivers/alidrive/alidrive.go index b23a9308..0c4aafac 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive/alidrive.go @@ -1,8 +1,9 @@ -package drivers +package alidrive import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -75,7 +76,7 @@ func (driver AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFil SetResult(&resp). SetError(&e). SetHeader("authorization", "Bearer\t"+account.AccessToken). - SetBody(Json{ + SetBody(base.Json{ "drive_id": account.DriveId, "fields": "*", "image_thumbnail_process": "image/resize,w_400/format,jpeg", @@ -127,16 +128,16 @@ func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, e } } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver AliDrive) RefreshToken(account *model.Account) error { url := "https://auth.aliyundrive.com/v2/account/token" - var resp TokenResp + var resp base.TokenResp var e AliRespError _, err := aliClient.R(). //ForceContentType("application/json"). - SetBody(Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}). + SetBody(base.Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}). SetResult(&resp). SetError(&e). Post(url) @@ -156,7 +157,7 @@ func (driver AliDrive) RefreshToken(account *model.Account) error { } func init() { - RegisterDriver(&AliDrive{}) + base.RegisterDriver(&AliDrive{}) aliClient. SetRetryCount(3). SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"). diff --git a/drivers/ali_driver.go b/drivers/alidrive/driver.go similarity index 86% rename from drivers/ali_driver.go rename to drivers/alidrive/driver.go index 48ec8fe6..f4574bd7 100644 --- a/drivers/ali_driver.go +++ b/drivers/alidrive/driver.go @@ -1,8 +1,9 @@ -package drivers +package alidrive import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -13,45 +14,45 @@ import ( type AliDrive struct{} -func (driver AliDrive) Config() DriverConfig { - return DriverConfig{ +func (driver AliDrive) Config() base.DriverConfig { + return base.DriverConfig{ Name: "AliDrive", OnlyProxy: false, } } -func (driver AliDrive) Items() []Item { - return []Item{ +func (driver AliDrive) Items() []base.Item { + return []base.Item{ { - Name: "order_by", - Label: "order_by", - Type: TypeSelect, - Values: "name,size,updated_at,created_at", + Name: "order_by", + Label: "order_by", + Type: base.TypeSelect, + Values: "name,size,updated_at,created_at", Required: false, }, { - Name: "order_direction", - Label: "order_direction", - Type: TypeSelect, - Values: "ASC,DESC", + Name: "order_direction", + Label: "order_direction", + Type: base.TypeSelect, + Values: "ASC,DESC", Required: false, }, { Name: "refresh_token", Label: "refresh token", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "root_folder", Label: "root folder file_id", - Type: TypeString, + Type: base.TypeString, Required: false, }, { Name: "limit", Label: "limit", - Type: TypeNumber, + Type: base.TypeNumber, Required: false, Description: ">0 and <=200", }, @@ -72,7 +73,7 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error { if err != nil { return err } - var resp Json + var resp base.Json _, _ = aliClient.R().SetResult(&resp). SetBody("{}"). SetHeader("authorization", "Bearer\t"+account.AccessToken). @@ -123,7 +124,7 @@ func (driver AliDrive) File(path string, account *model.Account) (*model.File, e return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver AliDrive) Files(path string, account *model.Account) ([]model.File, error) { @@ -157,12 +158,12 @@ func (driver AliDrive) Link(path string, account *model.Account) (string, error) if err != nil { return "", err } - var resp Json + var resp base.Json var e AliRespError _, err = aliClient.R().SetResult(&resp). SetError(&e). SetHeader("authorization", "Bearer\t"+account.AccessToken). - SetBody(Json{ + SetBody(base.Json{ "drive_id": account.DriveId, "file_id": file.Id, "expire_sec": 14400, @@ -214,10 +215,10 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{} return nil, err } // office - var resp Json + var resp base.Json var e AliRespError var url string - req := Json{ + req := base.Json{ "drive_id": account.DriveId, "file_id": file.FileId, } @@ -233,7 +234,7 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{} req["category"] = "live_transcoding" } default: - return nil, ErrNotSupport + return nil, base.ErrNotSupport } _, err = aliClient.R().SetResult(&resp).SetError(&e). SetHeader("authorization", "Bearer\t"+account.AccessToken). @@ -248,23 +249,23 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{} } func (driver AliDrive) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver AliDrive) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver AliDrive) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver AliDrive) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*AliDrive)(nil) \ No newline at end of file +var _ base.Driver = (*AliDrive)(nil) \ No newline at end of file diff --git a/drivers/all.go b/drivers/all.go new file mode 100644 index 00000000..328d0ecf --- /dev/null +++ b/drivers/all.go @@ -0,0 +1,11 @@ +package drivers + +import ( + _ "github.com/Xhofe/alist/drivers/123" + _ "github.com/Xhofe/alist/drivers/189" + _ "github.com/Xhofe/alist/drivers/alidrive" + _ "github.com/Xhofe/alist/drivers/google" + _ "github.com/Xhofe/alist/drivers/lanzou" + _ "github.com/Xhofe/alist/drivers/native" + _ "github.com/Xhofe/alist/drivers/onedrive" +) diff --git a/drivers/driver.go b/drivers/base/driver.go similarity index 96% rename from drivers/driver.go rename to drivers/base/driver.go index 348435cd..8cdbaa52 100644 --- a/drivers/driver.go +++ b/drivers/base/driver.go @@ -1,9 +1,10 @@ -package drivers +package base import ( "github.com/Xhofe/alist/model" "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" + log "github.com/sirupsen/logrus" "net/http" ) @@ -48,7 +49,7 @@ type TokenResp struct { var driversMap = map[string]Driver{} func RegisterDriver(driver Driver) { - //log.Infof("register driver: [%s]", driver.Config().Name) + log.Infof("register driver: [%s]", driver.Config().Name) driversMap[driver.Config().Name] = driver } diff --git a/drivers/types.go b/drivers/base/types.go similarity index 95% rename from drivers/types.go rename to drivers/base/types.go index bafd9228..f2c7160c 100644 --- a/drivers/types.go +++ b/drivers/base/types.go @@ -1,4 +1,4 @@ -package drivers +package base import "fmt" diff --git a/drivers/google_driver.go b/drivers/google/driver.go similarity index 87% rename from drivers/google_driver.go rename to drivers/google/driver.go index b54d2a6c..58dd1328 100644 --- a/drivers/google_driver.go +++ b/drivers/google/driver.go @@ -1,8 +1,9 @@ -package drivers +package google import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -12,37 +13,37 @@ import ( type GoogleDrive struct{} -func (driver GoogleDrive) Config() DriverConfig { - return DriverConfig{ +func (driver GoogleDrive) Config() base.DriverConfig { + return base.DriverConfig{ Name: "GoogleDrive", OnlyProxy: true, } } -func (driver GoogleDrive) Items() []Item { - return []Item{ +func (driver GoogleDrive) Items() []base.Item { + return []base.Item{ { Name: "client_id", Label: "client id", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "client_secret", Label: "client secret", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "refresh_token", Label: "refresh token", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "root_folder", Label: "root folder file_id", - Type: TypeString, + Type: base.TypeString, Required: false, }, } @@ -86,7 +87,7 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) { @@ -121,7 +122,7 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (string, err return "", err } if file.Type == conf.FOLDER { - return "", ErrNotFile + return "", base.ErrNotFile } link := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id) var e GoogleError @@ -165,27 +166,27 @@ func (driver GoogleDrive) Proxy(c *gin.Context, account *model.Account) { } func (driver GoogleDrive) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver GoogleDrive) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver GoogleDrive) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver GoogleDrive) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver GoogleDrive) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver GoogleDrive) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*GoogleDrive)(nil) \ No newline at end of file +var _ base.Driver = (*GoogleDrive)(nil) \ No newline at end of file diff --git a/drivers/googledrive.go b/drivers/google/googledrive.go similarity index 97% rename from drivers/googledrive.go rename to drivers/google/googledrive.go index e7b97c42..6e3c6833 100644 --- a/drivers/googledrive.go +++ b/drivers/google/googledrive.go @@ -1,8 +1,9 @@ -package drivers +package google import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -20,7 +21,7 @@ type GoogleTokenError struct { func (driver GoogleDrive) RefreshToken(account *model.Account) error { url := "https://www.googleapis.com/oauth2/v4/token" - var resp TokenResp + var resp base.TokenResp var e GoogleTokenError _, err := googleClient.R().SetResult(&resp).SetError(&e). SetFormData(map[string]string{ @@ -152,6 +153,6 @@ func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleF //} func init() { - RegisterDriver(&GoogleDrive{}) + base.RegisterDriver(&GoogleDrive{}) googleClient.SetRetryCount(3) } diff --git a/drivers/lanzou_driver.go b/drivers/lanzou/driver.go similarity index 86% rename from drivers/lanzou_driver.go rename to drivers/lanzou/driver.go index cb603476..991a4079 100644 --- a/drivers/lanzou_driver.go +++ b/drivers/lanzou/driver.go @@ -1,8 +1,9 @@ -package drivers +package lanzou import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -12,42 +13,42 @@ import ( type Lanzou struct{} -func (driver Lanzou) Config() DriverConfig { - return DriverConfig{ +func (driver Lanzou) Config() base.DriverConfig { + return base.DriverConfig{ Name: "Lanzou", OnlyProxy: false, } } -func (driver Lanzou) Items() []Item { - return []Item{ +func (driver Lanzou) Items() []base.Item { + return []base.Item{ { Name: "onedrive_type", Label: "lanzou type", - Type: TypeSelect, + Type: base.TypeSelect, Required: true, Values: "cookie,url", }, { Name: "access_token", Label: "cookie", - Type: TypeString, + Type: base.TypeString, Description: "about 15 days valid", }, { Name: "root_folder", Label: "root folder file_id", - Type: TypeString, + Type: base.TypeString, }, { Name: "site_url", Label: "share url", - Type: TypeString, + Type: base.TypeString, }, { Name: "password", Label: "share password", - Type: TypeString, + Type: base.TypeString, }, } } @@ -85,7 +86,7 @@ func (driver Lanzou) File(path string, account *model.Account) (*model.File, err return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, error) { @@ -157,27 +158,27 @@ func (driver Lanzou) Proxy(c *gin.Context, account *model.Account) { } func (driver Lanzou) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver *Lanzou) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver *Lanzou) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver *Lanzou) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver *Lanzou) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver *Lanzou) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*Lanzou)(nil) +var _ base.Driver = (*Lanzou)(nil) diff --git a/drivers/lanzou.go b/drivers/lanzou/lanzou.go similarity index 98% rename from drivers/lanzou.go rename to drivers/lanzou/lanzou.go index 7197aedb..aaa80d22 100644 --- a/drivers/lanzou.go +++ b/drivers/lanzou/lanzou.go @@ -1,8 +1,9 @@ -package drivers +package lanzou import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -231,7 +232,7 @@ func (driver *Lanzou) GetLink(downId string) (string, error) { } func init() { - RegisterDriver(&Lanzou{}) + base.RegisterDriver(&Lanzou{}) lanzouClient. SetRetryCount(3). SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36") diff --git a/drivers/native_driver.go b/drivers/native/driver.go similarity index 91% rename from drivers/native_driver.go rename to drivers/native/driver.go index eedfcb83..b65fc70e 100644 --- a/drivers/native_driver.go +++ b/drivers/native/driver.go @@ -1,8 +1,9 @@ -package drivers +package native import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -16,32 +17,32 @@ import ( type Native struct{} -func (driver Native) Config() DriverConfig { - return DriverConfig{ +func (driver Native) Config() base.DriverConfig { + return base.DriverConfig{ Name: "Native", OnlyProxy: true, } } -func (driver Native) Items() []Item { - return []Item{ +func (driver Native) Items() []base.Item { + return []base.Item{ { Name: "root_folder", Label: "root folder path", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "order_by", Label: "order_by", - Type: TypeSelect, + Type: base.TypeSelect, Values: "name,size,updated_at", Required: false, }, { Name: "order_direction", Label: "order_direction", - Type: TypeSelect, + Type: base.TypeSelect, Values: "ASC,DESC", Required: false, }, @@ -67,7 +68,7 @@ func (driver Native) Save(account *model.Account, old *model.Account) error { func (driver Native) File(path string, account *model.Account) (*model.File, error) { fullPath := filepath.Join(account.RootFolder, path) if !utils.Exists(fullPath) { - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } f, err := os.Stat(fullPath) if err != nil { @@ -91,7 +92,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err func (driver Native) Files(path string, account *model.Account) ([]model.File, error) { fullPath := filepath.Join(account.RootFolder, path) if !utils.Exists(fullPath) { - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } files := make([]model.File, 0) rawFiles, err := ioutil.ReadDir(fullPath) @@ -156,7 +157,7 @@ func (driver Native) Proxy(c *gin.Context, account *model.Account) { } func (driver Native) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver Native) MakeDir(path string, account *model.Account) error { @@ -181,7 +182,7 @@ func (driver Native) Copy(src string, dst string, account *model.Account) error dstFile, err := driver.File(dst, account) if err == nil { if !dstFile.IsDir() { - return ErrNotSupport + return base.ErrNotSupport } } if srcFile.IsDir() { @@ -226,4 +227,4 @@ func (driver Native) Upload(file *model.FileStream, account *model.Account) erro return err } -var _ Driver = (*Native)(nil) +var _ base.Driver = (*Native)(nil) diff --git a/drivers/native.go b/drivers/native/native.go similarity index 93% rename from drivers/native.go rename to drivers/native/native.go index aee2ed3d..0c32f769 100644 --- a/drivers/native.go +++ b/drivers/native/native.go @@ -1,7 +1,8 @@ -package drivers +package native import ( "fmt" + "github.com/Xhofe/alist/drivers/base" "io" "io/ioutil" "os" @@ -69,5 +70,5 @@ func (driver *Native) CopyDir(src string, dst string) error { } func init() { - RegisterDriver(&Native{}) + base.RegisterDriver(&Native{}) } diff --git a/drivers/one_driver.go b/drivers/onedrive/driver.go similarity index 86% rename from drivers/one_driver.go rename to drivers/onedrive/driver.go index 6fad7629..344762c2 100644 --- a/drivers/one_driver.go +++ b/drivers/onedrive/driver.go @@ -1,8 +1,9 @@ -package drivers +package onedrive import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" @@ -14,19 +15,19 @@ import ( type Onedrive struct{} -func (driver Onedrive) Config() DriverConfig { - return DriverConfig{ +func (driver Onedrive) Config() base.DriverConfig { + return base.DriverConfig{ Name: "Onedrive", OnlyProxy: false, } } -func (driver Onedrive) Items() []Item { - return []Item{ +func (driver Onedrive) Items() []base.Item { + return []base.Item{ { Name: "zone", Label: "zone", - Type: TypeSelect, + Type: base.TypeSelect, Required: true, Values: "global,cn,us,de", Description: "", @@ -34,57 +35,57 @@ func (driver Onedrive) Items() []Item { { Name: "onedrive_type", Label: "onedrive type", - Type: TypeSelect, + Type: base.TypeSelect, Required: true, Values: "onedrive,sharepoint", }, { Name: "client_id", Label: "client id", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "client_secret", Label: "client secret", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "redirect_uri", Label: "redirect uri", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "refresh_token", Label: "refresh token", - Type: TypeString, + Type: base.TypeString, Required: true, }, { Name: "site_id", Label: "site id", - Type: TypeString, + Type: base.TypeString, Required: false, }, { Name: "root_folder", Label: "root folder path", - Type: TypeString, + Type: base.TypeString, Required: false, }, { Name: "order_by", Label: "order_by", - Type: TypeSelect, + Type: base.TypeSelect, Values: "name,size,lastModifiedDateTime", Required: false, }, { Name: "order_direction", Label: "order_direction", - Type: TypeSelect, + Type: base.TypeSelect, Values: "asc,desc", Required: false, }, @@ -148,7 +149,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e return &file, nil } } - return nil, ErrPathNotFound + return nil, base.ErrPathNotFound } func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) { @@ -205,27 +206,27 @@ func (driver Onedrive) Proxy(c *gin.Context, account *model.Account) { } func (driver Onedrive) Preview(path string, account *model.Account) (interface{}, error) { - return nil, ErrNotSupport + return nil, base.ErrNotSupport } func (driver Onedrive) MakeDir(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Onedrive) Move(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Onedrive) Copy(src string, dst string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Onedrive) Delete(path string, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) error { - return ErrNotImplement + return base.ErrNotImplement } -var _ Driver = (*Onedrive)(nil) \ No newline at end of file +var _ base.Driver = (*Onedrive)(nil) \ No newline at end of file diff --git a/drivers/onedrive.go b/drivers/onedrive/onedrive.go similarity index 97% rename from drivers/onedrive.go rename to drivers/onedrive/onedrive.go index a82d379a..79102e1e 100644 --- a/drivers/onedrive.go +++ b/drivers/onedrive/onedrive.go @@ -1,8 +1,9 @@ -package drivers +package onedrive import ( "fmt" "github.com/Xhofe/alist/conf" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" @@ -73,7 +74,7 @@ type OneTokenErr struct { func (driver Onedrive) RefreshToken(account *model.Account) error { url := driver.GetMetaUrl(account, true, "") + "/common/oauth2/v2.0/token" - var resp TokenResp + var resp base.TokenResp var e OneTokenErr _, err := oneClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{ "grant_type": "refresh_token", @@ -185,6 +186,6 @@ func (driver Onedrive) GetFile(account *model.Account, path string) (*OneFile, e } func init() { - RegisterDriver(&Onedrive{}) + base.RegisterDriver(&Onedrive{}) oneClient.SetRetryCount(3) } \ No newline at end of file diff --git a/server/account.go b/server/account.go index 46615fb6..0b0216f0 100644 --- a/server/account.go +++ b/server/account.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" @@ -25,7 +25,7 @@ func CreateAccount(c *gin.Context) { ErrorResp(c, err, 400) return } - driver, ok := drivers.GetDriver(req.Type) + driver, ok := base.GetDriver(req.Type) if !ok { ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) return @@ -51,7 +51,7 @@ func SaveAccount(c *gin.Context) { ErrorResp(c, err, 400) return } - driver, ok := drivers.GetDriver(req.Type) + driver, ok := base.GetDriver(req.Type) if !ok { ErrorResp(c, fmt.Errorf("no [%s] driver", req.Type), 400) return diff --git a/server/common.go b/server/common.go index 08896f34..80ed4d69 100644 --- a/server/common.go +++ b/server/common.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" @@ -15,7 +15,7 @@ type Resp struct { Data interface{} `json:"data"` } -func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) { +func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) { var path, name string switch model.AccountsCount() { case 0: @@ -32,7 +32,7 @@ func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) { if !ok { return nil, "", nil, fmt.Errorf("no [%s] account", name) } - driver, ok := drivers.GetDriver(account.Type) + driver, ok := base.GetDriver(account.Type) if !ok { return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type) } diff --git a/server/driver.go b/server/driver.go index 97e239a1..b7a40c46 100644 --- a/server/driver.go +++ b/server/driver.go @@ -1,10 +1,10 @@ package server import ( - "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/drivers/base" "github.com/gin-gonic/gin" ) func GetDrivers(c *gin.Context) { - SuccessResp(c, drivers.GetDrivers()) + SuccessResp(c, base.GetDrivers()) } \ No newline at end of file diff --git a/server/webdav/file.go b/server/webdav/file.go index c834600d..94f60290 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -8,7 +8,7 @@ import ( "context" "fmt" "github.com/Xhofe/alist/conf" - "github.com/Xhofe/alist/drivers" + "github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" log "github.com/sirupsen/logrus" @@ -22,7 +22,7 @@ import ( type FileSystem struct{} -func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) { +func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) { var path, name string switch model.AccountsCount() { case 0: @@ -39,7 +39,7 @@ func ParsePath(rawPath string) (*model.Account, string, drivers.Driver, error) { if !ok { return nil, "", nil, fmt.Errorf("no [%s] account", name) } - driver, ok := drivers.GetDriver(account.Type) + driver, ok := base.GetDriver(account.Type) if !ok { return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type) }