mirror of https://github.com/Xhofe/alist
🔨 refactor driver(123 and 189)
parent
4fe6ed6c3e
commit
98691b2aa8
|
@ -18,6 +18,8 @@ type Pan123 struct {
|
||||||
|
|
||||||
var pan123Client = resty.New()
|
var pan123Client = resty.New()
|
||||||
|
|
||||||
|
var pan123 = "123Pan"
|
||||||
|
|
||||||
func (p Pan123) Items() []Item {
|
func (p Pan123) Items() []Item {
|
||||||
return []Item{
|
return []Item{
|
||||||
{
|
{
|
||||||
|
@ -114,6 +116,7 @@ type Pan123File struct {
|
||||||
|
|
||||||
func (p Pan123) FormatFile(file *Pan123File) *model.File {
|
func (p Pan123) FormatFile(file *Pan123File) *model.File {
|
||||||
f := &model.File{
|
f := &model.File{
|
||||||
|
Id: strconv.FormatInt(file.FileId, 10),
|
||||||
Name: file.FileName,
|
Name: file.FileName,
|
||||||
Size: file.Size,
|
Size: file.Size,
|
||||||
Driver: "123Pan",
|
Driver: "123Pan",
|
||||||
|
@ -172,71 +175,79 @@ func (p Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File,
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Pan123) File(path string, account *model.Account) (*model.File, error) {
|
||||||
|
path = utils.ParsePath(path)
|
||||||
|
if path == "/" {
|
||||||
|
return &model.File{
|
||||||
|
Id: account.RootFolder,
|
||||||
|
Name: account.Name,
|
||||||
|
Size: 0,
|
||||||
|
Type: conf.FOLDER,
|
||||||
|
Driver: pan123,
|
||||||
|
UpdatedAt: account.UpdatedAt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
dir, name := filepath.Split(path)
|
||||||
|
files, err := p.Files(dir, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
if file.Name == name {
|
||||||
|
return &file, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, PathNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Pan123) Files(path string, account *model.Account) ([]model.File, error) {
|
||||||
|
path = utils.ParsePath(path)
|
||||||
|
var rawFiles []Pan123File
|
||||||
|
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||||
|
if err == nil {
|
||||||
|
rawFiles, _ = cache.([]Pan123File)
|
||||||
|
} else {
|
||||||
|
file, err := p.File(path, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rawFiles, err = p.GetFiles(file.Id, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(rawFiles) > 0 {
|
||||||
|
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files := make([]model.File, 0)
|
||||||
|
for _, file := range rawFiles {
|
||||||
|
files = append(files, *p.FormatFile(&file))
|
||||||
|
}
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
func (p Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||||
path = utils.ParsePath(path)
|
path = utils.ParsePath(path)
|
||||||
log.Debugf("pan123 path: %s", path)
|
log.Debugf("pan123 path: %s", path)
|
||||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
file, err := p.File(path, account)
|
||||||
if err == nil {
|
|
||||||
files, _ := cache.([]Pan123File)
|
|
||||||
if len(files) != 0 {
|
|
||||||
res := make([]model.File, 0)
|
|
||||||
for _, file := range files {
|
|
||||||
res = append(res, *p.FormatFile(&file))
|
|
||||||
}
|
|
||||||
return nil, res, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// no cache or len(files) == 0
|
|
||||||
fileId := account.RootFolder
|
|
||||||
if path != "/" {
|
|
||||||
dir, name := filepath.Split(path)
|
|
||||||
dir = utils.ParsePath(dir)
|
|
||||||
_, _, err = p.Path(dir, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
|
||||||
parentFiles, _ := parentFiles_.([]Pan123File)
|
|
||||||
found := false
|
|
||||||
for _, file := range parentFiles {
|
|
||||||
if file.FileName == name {
|
|
||||||
found = true
|
|
||||||
if file.Type != 1 {
|
|
||||||
url, err := p.Link(path, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
f := p.FormatFile(&file)
|
|
||||||
f.Url = url
|
|
||||||
return f, nil, nil
|
|
||||||
} else {
|
|
||||||
fileId = strconv.FormatInt(file.FileId, 10)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return nil, nil, fmt.Errorf("path not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
files, err := p.GetFiles(fileId, account)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
log.Debugf("%+v", files)
|
if file.Type != conf.FOLDER {
|
||||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
|
file.Url, _ = p.Link(path, account)
|
||||||
res := make([]model.File, 0)
|
return file, nil, nil
|
||||||
for _, file := range files {
|
|
||||||
res = append(res, *p.FormatFile(&file))
|
|
||||||
}
|
}
|
||||||
return nil, res, nil
|
files, err := p.Files(path, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return nil, files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
func (p Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
||||||
dir, name := filepath.Split(path)
|
dir, name := filepath.Split(path)
|
||||||
dir = utils.ParsePath(dir)
|
dir = utils.ParsePath(dir)
|
||||||
_, _, err := p.Path(dir, account)
|
_, err := p.Files(dir, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -247,11 +258,11 @@ func (p Pan123) GetFile(path string, account *model.Account) (*Pan123File, error
|
||||||
if file.Type != 1 {
|
if file.Type != 1 {
|
||||||
return &file, err
|
return &file, err
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("not file")
|
return nil, NotFile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("path not found")
|
return nil, PathNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pan123DownResp struct {
|
type Pan123DownResp struct {
|
||||||
|
|
155
drivers/189.go
155
drivers/189.go
|
@ -92,6 +92,7 @@ func (c Cloud189) Save(account *model.Account, old *model.Account) error {
|
||||||
|
|
||||||
func (c Cloud189) FormatFile(file *Cloud189File) *model.File {
|
func (c Cloud189) FormatFile(file *Cloud189File) *model.File {
|
||||||
f := &model.File{
|
f := &model.File{
|
||||||
|
Id: strconv.FormatInt(file.Id, 10),
|
||||||
Name: file.Name,
|
Name: file.Name,
|
||||||
Size: file.Size,
|
Size: file.Size,
|
||||||
Driver: "189Cloud",
|
Driver: "189Cloud",
|
||||||
|
@ -113,85 +114,96 @@ func (c Cloud189) FormatFile(file *Cloud189File) *model.File {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
func (c Cloud189) File(path string, account *model.Account) (*model.File, error) {
|
||||||
path = utils.ParsePath(path)
|
path = utils.ParsePath(path)
|
||||||
log.Debugf("189 path: %s", path)
|
if path == "/" {
|
||||||
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
return &model.File{
|
||||||
if err == nil {
|
Id: account.RootFolder,
|
||||||
files, _ := cache.([]Cloud189File)
|
Name: account.Name,
|
||||||
if len(files) != 0 {
|
Size: 0,
|
||||||
res := make([]model.File, 0)
|
Type: conf.FOLDER,
|
||||||
for _, file := range files {
|
Driver: pan123,
|
||||||
res = append(res, *c.FormatFile(&file))
|
UpdatedAt: account.UpdatedAt,
|
||||||
}
|
}, nil
|
||||||
return nil, res, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// no cache or len(files) == 0
|
|
||||||
fileId := account.RootFolder
|
|
||||||
if path != "/" {
|
|
||||||
dir, name := filepath.Split(path)
|
|
||||||
dir = utils.ParsePath(dir)
|
|
||||||
_, _, err = c.Path(dir, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
|
||||||
parentFiles, _ := parentFiles_.([]Cloud189File)
|
|
||||||
found := false
|
|
||||||
for _, file := range parentFiles {
|
|
||||||
if file.Name == name {
|
|
||||||
found = true
|
|
||||||
if file.Size != -1 {
|
|
||||||
url, err := c.Link(path, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
file.Url = url
|
|
||||||
return c.FormatFile(&file), nil, nil
|
|
||||||
} else {
|
|
||||||
fileId = strconv.FormatInt(file.Id, 10)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return nil, nil, fmt.Errorf("path not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
files, err := c.GetFiles(fileId, account)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
|
|
||||||
res := make([]model.File, 0)
|
|
||||||
for _, file := range files {
|
|
||||||
res = append(res, *c.FormatFile(&file))
|
|
||||||
}
|
|
||||||
return nil, res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Cloud189) GetFile(path string, account *model.Account) (*Cloud189File, error) {
|
|
||||||
dir, name := filepath.Split(path)
|
dir, name := filepath.Split(path)
|
||||||
dir = utils.ParsePath(dir)
|
files, err := c.Files(dir, account)
|
||||||
_, _, err := c.Path(dir, account)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
for _, file := range files {
|
||||||
parentFiles, _ := parentFiles_.([]Cloud189File)
|
|
||||||
for _, file := range parentFiles {
|
|
||||||
if file.Name == name {
|
if file.Name == name {
|
||||||
if file.Size != -1 {
|
return &file, nil
|
||||||
return &file, err
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("not file")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("path not found")
|
return nil, PathNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
|
||||||
|
path = utils.ParsePath(path)
|
||||||
|
var rawFiles []Cloud189File
|
||||||
|
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
|
||||||
|
if err == nil {
|
||||||
|
rawFiles, _ = cache.([]Cloud189File)
|
||||||
|
} else {
|
||||||
|
file, err := c.File(path, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rawFiles, err = c.GetFiles(file.Id, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(rawFiles) > 0 {
|
||||||
|
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), rawFiles, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files := make([]model.File, 0)
|
||||||
|
for _, file := range rawFiles {
|
||||||
|
files = append(files, *c.FormatFile(&file))
|
||||||
|
}
|
||||||
|
return files, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||||
|
path = utils.ParsePath(path)
|
||||||
|
log.Debugf("189 path: %s", path)
|
||||||
|
file, err := c.File(path, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if file.Type != conf.FOLDER {
|
||||||
|
file.Url, _ = c.Link(path, account)
|
||||||
|
return file, nil, nil
|
||||||
|
}
|
||||||
|
files, err := c.Files(path, account)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return nil, files, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (c Cloud189) GetFile(path string, account *model.Account) (*Cloud189File, error) {
|
||||||
|
// dir, name := filepath.Split(path)
|
||||||
|
// dir = utils.ParsePath(dir)
|
||||||
|
// _, _, err := c.Path(dir, account)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
||||||
|
// parentFiles, _ := parentFiles_.([]Cloud189File)
|
||||||
|
// for _, file := range parentFiles {
|
||||||
|
// if file.Name == name {
|
||||||
|
// if file.Size != -1 {
|
||||||
|
// return &file, err
|
||||||
|
// } else {
|
||||||
|
// return nil, NotFile
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return nil, PathNotFound
|
||||||
|
//}
|
||||||
|
|
||||||
type Cloud189Down struct {
|
type Cloud189Down struct {
|
||||||
ResCode int `json:"res_code"`
|
ResCode int `json:"res_code"`
|
||||||
ResMessage string `json:"res_message"`
|
ResMessage string `json:"res_message"`
|
||||||
|
@ -199,10 +211,13 @@ type Cloud189Down struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cloud189) Link(path string, account *model.Account) (string, error) {
|
func (c Cloud189) Link(path string, account *model.Account) (string, error) {
|
||||||
file, err := c.GetFile(utils.ParsePath(path), account)
|
file, err := c.File(utils.ParsePath(path), account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if file.Type == conf.FOLDER {
|
||||||
|
return "", NotFile
|
||||||
|
}
|
||||||
client, ok := client189Map[account.Name]
|
client, ok := client189Map[account.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("can't find [%s] client", account.Name)
|
return "", fmt.Errorf("can't find [%s] client", account.Name)
|
||||||
|
@ -213,7 +228,7 @@ func (c Cloud189) Link(path string, account *model.Account) (string, error) {
|
||||||
SetHeader("Accept", "application/json;charset=UTF-8").
|
SetHeader("Accept", "application/json;charset=UTF-8").
|
||||||
SetQueryParams(map[string]string{
|
SetQueryParams(map[string]string{
|
||||||
"noCache": random(),
|
"noCache": random(),
|
||||||
"fileId": strconv.FormatInt(file.Id, 10),
|
"fileId": file.Id,
|
||||||
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
|
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
@ -10,6 +10,8 @@ import (
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
Items() []Item
|
Items() []Item
|
||||||
Save(account *model.Account, old *model.Account) error
|
Save(account *model.Account, old *model.Account) error
|
||||||
|
//File(path string, account *model.Account) (*model.File, error)
|
||||||
|
//Files(path string, account *model.Account) ([]model.File, error)
|
||||||
Path(path string, account *model.Account) (*model.File, []model.File, error)
|
Path(path string, account *model.Account) (*model.File, []model.File, error)
|
||||||
Link(path string, account *model.Account) (string, error)
|
Link(path string, account *model.Account) (string, error)
|
||||||
Proxy(c *gin.Context, account *model.Account)
|
Proxy(c *gin.Context, account *model.Account)
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package drivers
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var (
|
||||||
|
PathNotFound = fmt.Errorf("path not found")
|
||||||
|
NotFile = fmt.Errorf("not file")
|
||||||
|
)
|
|
@ -7,6 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
|
Id string `json:"-"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
Type int `json:"type"`
|
Type int `json:"type"`
|
||||||
|
|
Loading…
Reference in New Issue