🎨 improve code structure

pull/548/head
微凉 2021-11-30 09:37:51 +08:00
parent 50a02a7af7
commit 733b38b435
17 changed files with 102 additions and 130 deletions

View File

@ -1,10 +0,0 @@
package bootstrap
import (
_ "github.com/Xhofe/alist/drivers/123pan"
_ "github.com/Xhofe/alist/drivers/189cloud"
_ "github.com/Xhofe/alist/drivers/alidrive"
_ "github.com/Xhofe/alist/drivers/googledrive"
_ "github.com/Xhofe/alist/drivers/native"
_ "github.com/Xhofe/alist/drivers/onedrive"
)

View File

@ -5,7 +5,7 @@ import (
log "github.com/sirupsen/logrus"
)
// initLog init log
// InitLog init log
func InitLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
@ -18,4 +18,5 @@ func InitLog() {
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
log.Infof("init log...")
}

View File

@ -1,9 +1,8 @@
package _23pan
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -53,7 +52,7 @@ func (driver Pan123) Login(account *model.Account) error {
var resp Pan123TokenResp
_, err := pan123Client.R().
SetResult(&resp).
SetBody(drivers.Json{
SetBody(Json{
"passport": account.Username,
"password": account.Password,
}).Post("https://www.123pan.com/api/user/sign_in")
@ -76,7 +75,7 @@ func (driver Pan123) FormatFile(file *Pan123File) *model.File {
Id: strconv.FormatInt(file.FileId, 10),
Name: file.FileName,
Size: file.Size,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: file.UpdateAt,
}
if file.Type == 1 {
@ -136,14 +135,14 @@ func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File,
if file.Type != conf.FOLDER {
return &file, err
} else {
return nil, drivers.NotFile
return nil, NotFile
}
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func init() {
drivers.RegisterDriver(driverName, &Pan123{})
RegisterDriver(&Pan123{})
pan123Client.SetRetryCount(3)
}

View File

@ -1,9 +1,8 @@
package _23pan
package drivers
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"
@ -14,16 +13,15 @@ import (
type Pan123 struct {}
var driverName = "123Pan"
func (driver Pan123) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver Pan123) Config() DriverConfig {
return DriverConfig{
Name: "123Pan",
OnlyProxy: false,
}
}
func (driver Pan123) Items() []drivers.Item {
return []drivers.Item{
func (driver Pan123) Items() []Item {
return []Item{
{
Name: "username",
Label: "username",
@ -77,7 +75,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -91,7 +89,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) {
@ -127,7 +125,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(drivers.Json{
SetBody(Json{
"driveId": 0,
"etag": file.Etag,
"fileId": file.FileId,
@ -154,7 +152,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 := drivers.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
res, err := NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
if err != nil {
return "", err
}
@ -191,4 +189,4 @@ func (driver Pan123) Preview(path string, account *model.Account) (interface{},
return nil, nil
}
var _ drivers.Driver = (*Pan123)(nil)
var _ Driver = (*Pan123)(nil)

View File

@ -1,4 +1,4 @@
package _89cloud
package drivers
import (
"crypto/rand"
@ -9,7 +9,6 @@ import (
"encoding/pem"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -30,7 +29,7 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
Id: strconv.FormatInt(file.Id, 10),
Name: file.Name,
Size: file.Size,
Driver: "189Cloud",
Driver: driver.Config().Name,
UpdatedAt: nil,
Thumbnail: file.Icon.SmallUrl,
Url: file.Url,
@ -313,6 +312,6 @@ func b64tohex(a string) string {
}
func init() {
drivers.RegisterDriver(driverName, &Cloud189{})
RegisterDriver(&Cloud189{})
client189Map = make(map[string]*resty.Client, 0)
}

View File

@ -1,9 +1,8 @@
package _89cloud
package drivers
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"
@ -13,16 +12,15 @@ import (
type Cloud189 struct {}
var driverName = "189Cloud"
func (driver Cloud189) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver Cloud189) Config() DriverConfig {
return DriverConfig{
Name: "189Cloud",
OnlyProxy: false,
}
}
func (driver Cloud189) Items() []drivers.Item {
return []drivers.Item{
func (driver Cloud189) Items() []Item {
return []Item{
{
Name: "username",
Label: "username",
@ -85,7 +83,7 @@ func (driver Cloud189) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -99,7 +97,7 @@ func (driver Cloud189) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
@ -134,7 +132,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error)
return "", err
}
if file.Type == conf.FOLDER {
return "", drivers.NotFile
return "", NotFile
}
client, ok := client189Map[account.Name]
if !ok {
@ -163,7 +161,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error)
if resp.ResCode != 0 {
return "", fmt.Errorf(resp.ResMessage)
}
res, err := drivers.NoRedirectClient.R().Get(resp.FileDownloadUrl)
res, err := NoRedirectClient.R().Get(resp.FileDownloadUrl)
if err != nil {
return "", err
}
@ -199,4 +197,4 @@ func (driver Cloud189) Preview(path string, account *model.Account) (interface{}
return nil, nil
}
var _ drivers.Driver = (*Cloud189)(nil)
var _ Driver = (*Cloud189)(nil)

View File

@ -1,9 +1,8 @@
package alidrive
package drivers
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"
@ -14,16 +13,15 @@ import (
type AliDrive struct{}
var driverName = "AliDrive"
func (driver AliDrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver AliDrive) Config() DriverConfig {
return DriverConfig{
Name: "AliDrive",
OnlyProxy: false,
}
}
func (driver AliDrive) Items() []drivers.Item {
return []drivers.Item{
func (driver AliDrive) Items() []Item {
return []Item{
{
Name: "order_by",
Label: "order_by",
@ -74,7 +72,7 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
if err != nil {
return err
}
var resp drivers.Json
var resp Json
_, _ = aliClient.R().SetResult(&resp).
SetBody("{}").
SetHeader("authorization", "Bearer\t"+account.AccessToken).
@ -111,7 +109,7 @@ func (driver AliDrive) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -125,7 +123,7 @@ func (driver AliDrive) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver AliDrive) Files(path string, account *model.Account) ([]model.File, error) {
@ -159,12 +157,12 @@ func (driver AliDrive) Link(path string, account *model.Account) (string, error)
if err != nil {
return "", err
}
var resp drivers.Json
var resp Json
var e AliRespError
_, err = aliClient.R().SetResult(&resp).
SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(drivers.Json{
SetBody(Json{
"drive_id": account.DriveId,
"file_id": file.Id,
"expire_sec": 14400,
@ -216,10 +214,10 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{}
return nil, err
}
// office
var resp drivers.Json
var resp Json
var e AliRespError
var url string
req := drivers.Json{
req := Json{
"drive_id": account.DriveId,
"file_id": file.FileId,
}
@ -249,4 +247,4 @@ func (driver AliDrive) Preview(path string, account *model.Account) (interface{}
return resp, nil
}
var _ drivers.Driver = (*AliDrive)(nil)
var _ Driver = (*AliDrive)(nil)

View File

@ -1,9 +1,8 @@
package alidrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -46,7 +45,7 @@ func (driver AliDrive) FormatFile(file *AliFile) *model.File {
Size: file.Size,
UpdatedAt: file.UpdatedAt,
Thumbnail: file.Thumbnail,
Driver: driverName,
Driver: driver.Config().Name,
Url: file.Url,
}
if file.Type == "folder" {
@ -76,7 +75,7 @@ func (driver AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFil
SetResult(&resp).
SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(drivers.Json{
SetBody(Json{
"drive_id": account.DriveId,
"fields": "*",
"image_thumbnail_process": "image/resize,w_400/format,jpeg",
@ -128,16 +127,16 @@ func (driver AliDrive) GetFile(path string, account *model.Account) (*AliFile, e
}
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver AliDrive) RefreshToken(account *model.Account) error {
url := "https://auth.aliyundrive.com/v2/account/token"
var resp drivers.TokenResp
var resp TokenResp
var e AliRespError
_, err := aliClient.R().
//ForceContentType("application/json").
SetBody(drivers.Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
SetBody(Json{"refresh_token": account.RefreshToken, "grant_type": "refresh_token"}).
SetResult(&resp).
SetError(&e).
Post(url)
@ -157,7 +156,7 @@ func (driver AliDrive) RefreshToken(account *model.Account) error {
}
func init() {
drivers.RegisterDriver(driverName, &AliDrive{})
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").

View File

@ -9,6 +9,7 @@ import (
)
type DriverConfig struct {
Name string
OnlyProxy bool
}
@ -46,9 +47,9 @@ type TokenResp struct {
var driversMap = map[string]Driver{}
func RegisterDriver(name string, driver Driver) {
log.Infof("register driver: [%s]", name)
driversMap[name] = driver
func RegisterDriver(driver Driver) {
log.Infof("register driver: [%s]", driver.Config().Name)
driversMap[driver.Config().Name] = driver
}
func GetDriver(name string) (driver Driver, ok bool) {

View File

@ -5,4 +5,5 @@ import "fmt"
var (
PathNotFound = fmt.Errorf("path not found")
NotFile = fmt.Errorf("not file")
NotImplement = fmt.Errorf("not implement")
)

View File

@ -1,9 +1,8 @@
package googledrive
package drivers
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"
@ -13,16 +12,15 @@ import (
type GoogleDrive struct{}
var driverName = "GoogleDrive"
func (driver GoogleDrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver GoogleDrive) Config() DriverConfig {
return DriverConfig{
Name: "GoogleDrive",
OnlyProxy: true,
}
}
func (driver GoogleDrive) Items() []drivers.Item {
return []drivers.Item{
func (driver GoogleDrive) Items() []Item {
return []Item{
{
Name: "client_id",
Label: "client id",
@ -71,7 +69,7 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -85,7 +83,7 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) {
@ -120,7 +118,7 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (string, err
return "", err
}
if file.Type == conf.FOLDER {
return "", drivers.NotFile
return "", NotFile
}
link := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id)
var e GoogleError

View File

@ -1,9 +1,8 @@
package googledrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -21,7 +20,7 @@ type GoogleTokenError struct {
func (driver GoogleDrive) RefreshToken(account *model.Account) error {
url := "https://www.googleapis.com/oauth2/v4/token"
var resp drivers.TokenResp
var resp TokenResp
var e GoogleTokenError
_, err := googleClient.R().SetResult(&resp).SetError(&e).
SetFormData(map[string]string{
@ -57,7 +56,7 @@ func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
f := &model.File{
Id: file.Id,
Name: file.Name,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: file.ModifiedTime,
Thumbnail: "",
Url: "",
@ -152,9 +151,9 @@ func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleF
// return nil, drivers.PathNotFound
//}
var _ drivers.Driver = (*GoogleDrive)(nil)
var _ Driver = (*GoogleDrive)(nil)
func init() {
drivers.RegisterDriver(driverName, &GoogleDrive{})
RegisterDriver(&GoogleDrive{})
googleClient.SetRetryCount(3)
}

5
drivers/native.go Normal file
View File

@ -0,0 +1,5 @@
package drivers
func init() {
RegisterDriver(&Native{})
}

View File

@ -1,9 +0,0 @@
package native
import (
"github.com/Xhofe/alist/drivers"
)
func init() {
drivers.RegisterDriver(driverName, &Native{})
}

View File

@ -1,9 +1,8 @@
package native
package drivers
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"
@ -16,16 +15,15 @@ import (
type Native struct{}
var driverName = "Native"
func (driver Native) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver Native) Config() DriverConfig {
return DriverConfig{
Name: "Native",
OnlyProxy: true,
}
}
func (driver Native) Items() []drivers.Item {
return []drivers.Item{
func (driver Native) Items() []Item {
return []Item{
{
Name: "root_folder",
Label: "root folder path",
@ -68,7 +66,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, drivers.PathNotFound
return nil, PathNotFound
}
f, err := os.Stat(fullPath)
if err != nil {
@ -79,7 +77,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err
Name: f.Name(),
Size: f.Size(),
UpdatedAt: &time,
Driver: driverName,
Driver: driver.Config().Name,
}
if f.IsDir() {
file.Type = conf.FOLDER
@ -92,7 +90,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, drivers.PathNotFound
return nil, PathNotFound
}
files := make([]model.File, 0)
rawFiles, err := ioutil.ReadDir(fullPath)
@ -109,7 +107,7 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
Driver: driverName,
Driver: driver.Config().Name,
}
if f.IsDir() {
file.Type = conf.FOLDER
@ -160,4 +158,4 @@ func (driver Native) Preview(path string, account *model.Account) (interface{},
return nil, fmt.Errorf("no need")
}
var _ drivers.Driver = (*Native)(nil)
var _ Driver = (*Native)(nil)

View File

@ -1,9 +1,8 @@
package onedrive
package drivers
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"
@ -14,16 +13,15 @@ import (
type Onedrive struct{}
var driverName = "Onedrive"
func (driver Onedrive) Config() drivers.DriverConfig {
return drivers.DriverConfig{
func (driver Onedrive) Config() DriverConfig {
return DriverConfig{
Name: "Onedrive",
OnlyProxy: false,
}
}
func (driver Onedrive) Items() []drivers.Item {
return []drivers.Item{
func (driver Onedrive) Items() []Item {
return []Item{
{
Name: "zone",
Label: "zone",
@ -135,7 +133,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driverName,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
@ -149,7 +147,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
return &file, nil
}
}
return nil, drivers.PathNotFound
return nil, PathNotFound
}
func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) {

View File

@ -1,9 +1,8 @@
package onedrive
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
@ -74,7 +73,7 @@ type OneTokenErr struct {
func (driver Onedrive) RefreshToken(account *model.Account) error {
url := driver.GetMetaUrl(account, true, "") + "/common/oauth2/v2.0/token"
var resp drivers.TokenResp
var resp TokenResp
var e OneTokenErr
_, err := oneClient.R().SetResult(&resp).SetError(&e).SetFormData(map[string]string{
"grant_type": "refresh_token",
@ -124,7 +123,7 @@ func (driver Onedrive) FormatFile(file *OneFile) *model.File {
Name: file.Name,
Size: file.Size,
UpdatedAt: file.LastModifiedDateTime,
Driver: driverName,
Driver: driver.Config().Name,
Url: file.Url,
}
if file.File.MimeType == "" {
@ -177,9 +176,9 @@ func (driver Onedrive) GetFile(account *model.Account, path string) (*OneFile, e
return &file, nil
}
var _ drivers.Driver = (*Onedrive)(nil)
var _ Driver = (*Onedrive)(nil)
func init() {
drivers.RegisterDriver(driverName, &Onedrive{})
RegisterDriver(&Onedrive{})
oneClient.SetRetryCount(3)
}