🔨 refactor driver(123 and 189)

pull/548/head
微凉 2021-11-27 18:55:09 +08:00
parent 4fe6ed6c3e
commit 98691b2aa8
5 changed files with 162 additions and 125 deletions

View File

@ -18,6 +18,8 @@ type Pan123 struct {
var pan123Client = resty.New()
var pan123 = "123Pan"
func (p Pan123) Items() []Item {
return []Item{
{
@ -114,6 +116,7 @@ type Pan123File struct {
func (p Pan123) FormatFile(file *Pan123File) *model.File {
f := &model.File{
Id: strconv.FormatInt(file.FileId, 10),
Name: file.FileName,
Size: file.Size,
Driver: "123Pan",
@ -172,71 +175,79 @@ func (p Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File,
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) {
path = utils.ParsePath(path)
log.Debugf("pan123 path: %s", path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
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)
file, err := p.File(path, 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 file.Type != conf.FOLDER {
file.Url, _ = p.Link(path, account)
return file, nil, nil
}
files, err := p.Files(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 {
return nil, nil, err
}
log.Debugf("%+v", files)
_ = 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, *p.FormatFile(&file))
}
return nil, res, nil
return nil, files, nil
}
func (p Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
dir, name := filepath.Split(path)
dir = utils.ParsePath(dir)
_, _, err := p.Path(dir, account)
_, err := p.Files(dir, account)
if err != nil {
return nil, err
}
@ -247,11 +258,11 @@ func (p Pan123) GetFile(path string, account *model.Account) (*Pan123File, error
if file.Type != 1 {
return &file, err
} else {
return nil, fmt.Errorf("not file")
return nil, NotFile
}
}
}
return nil, fmt.Errorf("path not found")
return nil, PathNotFound
}
type Pan123DownResp struct {

View File

@ -92,6 +92,7 @@ func (c Cloud189) Save(account *model.Account, old *model.Account) error {
func (c Cloud189) FormatFile(file *Cloud189File) *model.File {
f := &model.File{
Id: strconv.FormatInt(file.Id, 10),
Name: file.Name,
Size: file.Size,
Driver: "189Cloud",
@ -113,85 +114,96 @@ func (c Cloud189) FormatFile(file *Cloud189File) *model.File {
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)
log.Debugf("189 path: %s", path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil {
files, _ := cache.([]Cloud189File)
if len(files) != 0 {
res := make([]model.File, 0)
for _, file := range files {
res = append(res, *c.FormatFile(&file))
if path == "/" {
return &model.File{
Id: account.RootFolder,
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: pan123,
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 = utils.ParsePath(dir)
_, _, err := c.Path(dir, account)
files, err := c.Files(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 {
for _, file := range files {
if file.Name == name {
if file.Size != -1 {
return &file, err
} else {
return nil, fmt.Errorf("not file")
return &file, nil
}
}
}
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 {
ResCode int `json:"res_code"`
ResMessage string `json:"res_message"`
@ -199,10 +211,13 @@ type Cloud189Down struct {
}
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 {
return "", err
}
if file.Type == conf.FOLDER {
return "", NotFile
}
client, ok := client189Map[account.Name]
if !ok {
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").
SetQueryParams(map[string]string{
"noCache": random(),
"fileId": strconv.FormatInt(file.Id, 10),
"fileId": file.Id,
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
if err != nil {
return "", err

View File

@ -10,6 +10,8 @@ import (
type Driver interface {
Items() []Item
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)
Link(path string, account *model.Account) (string, error)
Proxy(c *gin.Context, account *model.Account)

8
drivers/error.go Normal file
View File

@ -0,0 +1,8 @@
package drivers
import "fmt"
var (
PathNotFound = fmt.Errorf("path not found")
NotFile = fmt.Errorf("not file")
)

View File

@ -7,6 +7,7 @@ import (
)
type File struct {
Id string `json:"-"`
Name string `json:"name"`
Size int64 `json:"size"`
Type int `json:"type"`