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

View File

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

View File

@ -1,9 +1,8 @@
package _23pan package drivers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -14,16 +13,15 @@ import (
type Pan123 struct {} type Pan123 struct {}
var driverName = "123Pan" func (driver Pan123) Config() DriverConfig {
return DriverConfig{
func (driver Pan123) Config() drivers.DriverConfig { Name: "123Pan",
return drivers.DriverConfig{
OnlyProxy: false, OnlyProxy: false,
} }
} }
func (driver Pan123) Items() []drivers.Item { func (driver Pan123) Items() []Item {
return []drivers.Item{ return []Item{
{ {
Name: "username", Name: "username",
Label: "username", Label: "username",
@ -77,7 +75,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
Name: account.Name, Name: account.Name,
Size: 0, Size: 0,
Type: conf.FOLDER, Type: conf.FOLDER,
Driver: driverName, Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt, UpdatedAt: account.UpdatedAt,
}, nil }, nil
} }
@ -91,7 +89,7 @@ func (driver Pan123) File(path string, account *model.Account) (*model.File, err
return &file, nil return &file, nil
} }
} }
return nil, drivers.PathNotFound return nil, PathNotFound
} }
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) { 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 var resp Pan123DownResp
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken). _, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
SetBody(drivers.Json{ SetBody(Json{
"driveId": 0, "driveId": 0,
"etag": file.Etag, "etag": file.Etag,
"fileId": file.FileId, "fileId": file.FileId,
@ -154,7 +152,7 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) {
return "", err return "", err
} }
u_ := fmt.Sprintf("https://%s%s",u.Host,u.Path) 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 { if err != nil {
return "", err return "", err
} }
@ -191,4 +189,4 @@ func (driver Pan123) Preview(path string, account *model.Account) (interface{},
return nil, nil return nil, nil
} }
var _ drivers.Driver = (*Pan123)(nil) var _ Driver = (*Pan123)(nil)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,8 @@
package googledrive package drivers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -21,7 +20,7 @@ type GoogleTokenError struct {
func (driver GoogleDrive) RefreshToken(account *model.Account) error { func (driver GoogleDrive) RefreshToken(account *model.Account) error {
url := "https://www.googleapis.com/oauth2/v4/token" url := "https://www.googleapis.com/oauth2/v4/token"
var resp drivers.TokenResp var resp TokenResp
var e GoogleTokenError var e GoogleTokenError
_, err := googleClient.R().SetResult(&resp).SetError(&e). _, err := googleClient.R().SetResult(&resp).SetError(&e).
SetFormData(map[string]string{ SetFormData(map[string]string{
@ -57,7 +56,7 @@ func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
f := &model.File{ f := &model.File{
Id: file.Id, Id: file.Id,
Name: file.Name, Name: file.Name,
Driver: driverName, Driver: driver.Config().Name,
UpdatedAt: file.ModifiedTime, UpdatedAt: file.ModifiedTime,
Thumbnail: "", Thumbnail: "",
Url: "", Url: "",
@ -152,9 +151,9 @@ func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleF
// return nil, drivers.PathNotFound // return nil, drivers.PathNotFound
//} //}
var _ drivers.Driver = (*GoogleDrive)(nil) var _ Driver = (*GoogleDrive)(nil)
func init() { func init() {
drivers.RegisterDriver(driverName, &GoogleDrive{}) RegisterDriver(&GoogleDrive{})
googleClient.SetRetryCount(3) 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 ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -16,16 +15,15 @@ import (
type Native struct{} type Native struct{}
var driverName = "Native" func (driver Native) Config() DriverConfig {
return DriverConfig{
func (driver Native) Config() drivers.DriverConfig { Name: "Native",
return drivers.DriverConfig{
OnlyProxy: true, OnlyProxy: true,
} }
} }
func (driver Native) Items() []drivers.Item { func (driver Native) Items() []Item {
return []drivers.Item{ return []Item{
{ {
Name: "root_folder", Name: "root_folder",
Label: "root folder path", 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) { func (driver Native) File(path string, account *model.Account) (*model.File, error) {
fullPath := filepath.Join(account.RootFolder, path) fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) { if !utils.Exists(fullPath) {
return nil, drivers.PathNotFound return nil, PathNotFound
} }
f, err := os.Stat(fullPath) f, err := os.Stat(fullPath)
if err != nil { if err != nil {
@ -79,7 +77,7 @@ func (driver Native) File(path string, account *model.Account) (*model.File, err
Name: f.Name(), Name: f.Name(),
Size: f.Size(), Size: f.Size(),
UpdatedAt: &time, UpdatedAt: &time,
Driver: driverName, Driver: driver.Config().Name,
} }
if f.IsDir() { if f.IsDir() {
file.Type = conf.FOLDER 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) { func (driver Native) Files(path string, account *model.Account) ([]model.File, error) {
fullPath := filepath.Join(account.RootFolder, path) fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) { if !utils.Exists(fullPath) {
return nil, drivers.PathNotFound return nil, PathNotFound
} }
files := make([]model.File, 0) files := make([]model.File, 0)
rawFiles, err := ioutil.ReadDir(fullPath) rawFiles, err := ioutil.ReadDir(fullPath)
@ -109,7 +107,7 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
Size: f.Size(), Size: f.Size(),
Type: 0, Type: 0,
UpdatedAt: &time, UpdatedAt: &time,
Driver: driverName, Driver: driver.Config().Name,
} }
if f.IsDir() { if f.IsDir() {
file.Type = conf.FOLDER file.Type = conf.FOLDER
@ -160,4 +158,4 @@ func (driver Native) Preview(path string, account *model.Account) (interface{},
return nil, fmt.Errorf("no need") 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 ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -14,16 +13,15 @@ import (
type Onedrive struct{} type Onedrive struct{}
var driverName = "Onedrive" func (driver Onedrive) Config() DriverConfig {
return DriverConfig{
func (driver Onedrive) Config() drivers.DriverConfig { Name: "Onedrive",
return drivers.DriverConfig{
OnlyProxy: false, OnlyProxy: false,
} }
} }
func (driver Onedrive) Items() []drivers.Item { func (driver Onedrive) Items() []Item {
return []drivers.Item{ return []Item{
{ {
Name: "zone", Name: "zone",
Label: "zone", Label: "zone",
@ -135,7 +133,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
Name: account.Name, Name: account.Name,
Size: 0, Size: 0,
Type: conf.FOLDER, Type: conf.FOLDER,
Driver: driverName, Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt, UpdatedAt: account.UpdatedAt,
}, nil }, nil
} }
@ -149,7 +147,7 @@ func (driver Onedrive) File(path string, account *model.Account) (*model.File, e
return &file, nil return &file, nil
} }
} }
return nil, drivers.PathNotFound return nil, PathNotFound
} }
func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) { func (driver Onedrive) Files(path string, account *model.Account) ([]model.File, error) {

View File

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