change path interface

This commit is contained in:
微凉 2021-11-23 15:58:03 +08:00
parent e31402e94f
commit 163ee1159e
9 changed files with 32 additions and 32 deletions

View File

@ -172,16 +172,16 @@ func (p Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File,
return res, nil return res, 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)) cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil { if err == nil {
files, _ := cache.([]Pan123File) files, _ := cache.([]Pan123File)
if len(files) != 0 { if len(files) != 0 {
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, p.FormatFile(&file)) res = append(res, *p.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }
@ -226,9 +226,9 @@ func (p Pan123) Path(path string, account *model.Account) (*model.File, []*model
} }
log.Debugf("%+v", files) log.Debugf("%+v", files)
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil) _ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, p.FormatFile(&file)) res = append(res, *p.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }

View File

@ -113,16 +113,16 @@ 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) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path) path = utils.ParsePath(path)
log.Debugf("189 path: %s", path) log.Debugf("189 path: %s", path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path)) cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil { if err == nil {
files, _ := cache.([]Cloud189File) files, _ := cache.([]Cloud189File)
if len(files) != 0 { if len(files) != 0 {
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, c.FormatFile(&file)) res = append(res, *c.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }
@ -164,9 +164,9 @@ func (c Cloud189) Path(path string, account *model.Account) (*model.File, []*mod
return nil, nil, err return nil, nil, err
} }
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil) _ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, c.FormatFile(&file)) res = append(res, *c.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }

View File

@ -232,16 +232,16 @@ func (a AliDrive) GetFile(path string, account *model.Account) (*AliFile, error)
} }
// path: /aaa/bbb // path: /aaa/bbb
func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (a AliDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path) path = utils.ParsePath(path)
log.Debugf("ali path: %s", path) log.Debugf("ali path: %s", path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path)) cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil { if err == nil {
files, _ := cache.([]AliFile) files, _ := cache.([]AliFile)
if len(files) != 0 { if len(files) != 0 {
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, a.FormatFile(&file)) res = append(res, *a.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }
@ -283,9 +283,9 @@ func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*mod
return nil, nil, err return nil, nil, err
} }
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil) _ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, a.FormatFile(&file)) res = append(res, *a.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }

View File

@ -10,7 +10,7 @@ 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
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)
Preview(path string, account *model.Account) (interface{}, error) Preview(path string, account *model.Account) (interface{}, error)

View File

@ -197,16 +197,16 @@ func (g GoogleDrive) GetFile(path string, account *model.Account) (*GoogleFile,
return nil, fmt.Errorf("path not found") return nil, fmt.Errorf("path not found")
} }
func (g GoogleDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (g GoogleDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path) path = utils.ParsePath(path)
log.Debugf("google path: %s", path) log.Debugf("google path: %s", path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path)) cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil { if err == nil {
files, _ := cache.([]GoogleFile) files, _ := cache.([]GoogleFile)
if len(files) != 0 { if len(files) != 0 {
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, g.FormatFile(&file)) res = append(res, *g.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }
@ -243,9 +243,9 @@ func (g GoogleDrive) Path(path string, account *model.Account) (*model.File, []*
return nil, nil, err return nil, nil, err
} }
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil) _ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), files, nil)
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, g.FormatFile(&file)) res = append(res, *g.FormatFile(&file))
} }
return nil, res, nil return nil, res, nil
} }

View File

@ -52,14 +52,14 @@ func (n Native) Save(account *model.Account, old *model.Account) error {
} }
// TODO sort files // TODO sort files
func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (n Native) Path(path string, account *model.Account) (*model.File, []model.File, error) {
fullPath := filepath.Join(account.RootFolder, path) fullPath := filepath.Join(account.RootFolder, path)
log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath) log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath)
if !utils.Exists(fullPath) { if !utils.Exists(fullPath) {
return nil, nil, fmt.Errorf("path not found") return nil, nil, fmt.Errorf("path not found")
} }
if utils.IsDir(fullPath) { if utils.IsDir(fullPath) {
result := make([]*model.File, 0) result := make([]model.File, 0)
files, err := ioutil.ReadDir(fullPath) files, err := ioutil.ReadDir(fullPath)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -69,7 +69,7 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []*model
continue continue
} }
time := f.ModTime() time := f.ModTime()
file := &model.File{ file := model.File{
Name: f.Name(), Name: f.Name(),
Size: f.Size(), Size: f.Size(),
Type: 0, Type: 0,

View File

@ -262,11 +262,11 @@ func (o Onedrive) GetFile(account *model.Account, path string) (*OneFile, error)
return &file, nil return &file, nil
} }
func (o Onedrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (o Onedrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path) path = utils.ParsePath(path)
cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path)) cache, err := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path))
if err == nil { if err == nil {
files, _ := cache.([]*model.File) files, _ := cache.([]model.File)
return nil, files, nil return nil, files, nil
} }
file, err := o.GetFile(account, path) file, err := o.GetFile(account, path)
@ -280,9 +280,9 @@ func (o Onedrive) Path(path string, account *model.Account) (*model.File, []*mod
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
res := make([]*model.File, 0) res := make([]model.File, 0)
for _, file := range files { for _, file := range files {
res = append(res, o.FormatFile(&file)) res = append(res, *o.FormatFile(&file))
} }
_ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), res, nil) _ = conf.Cache.Set(conf.Ctx, fmt.Sprintf("%s%s", account.Name, path), res, nil)
return nil, res, nil return nil, res, nil

View File

@ -99,14 +99,14 @@ func GetAccountById(id uint) (*Account, error) {
return &account, nil return &account, nil
} }
func GetAccountFiles() ([]*File, error) { func GetAccountFiles() ([]File, error) {
files := make([]*File, 0) files := make([]File, 0)
var accounts []Account var accounts []Account
if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil { if err := conf.DB.Order("`index`").Find(&accounts).Error; err != nil {
return nil, err return nil, err
} }
for _, v := range accounts { for _, v := range accounts {
files = append(files, &File{ files = append(files, File{
Name: v.Name, Name: v.Name,
Size: 0, Size: 0,
Type: conf.FOLDER, Type: conf.FOLDER,

View File

@ -72,7 +72,7 @@ func Path(c *gin.Context) {
}) })
} else { } else {
if meta != nil && meta.Hide != "" { if meta != nil && meta.Hide != "" {
tmpFiles := make([]*model.File, 0) tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",") hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files { for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) { if !utils.IsContain(hideFiles, item.Name) {