feat: virtual path
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
@ -25,30 +24,16 @@ type PathReq struct {
|
||||
}
|
||||
|
||||
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
||||
var path, name string
|
||||
switch model.AccountsCount() {
|
||||
case 0:
|
||||
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
|
||||
case 1:
|
||||
path = rawPath
|
||||
break
|
||||
default:
|
||||
if path == "/" {
|
||||
return nil, "", nil, errors.New("can't operate root of multiple accounts")
|
||||
}
|
||||
paths := strings.Split(rawPath, "/")
|
||||
path = "/" + strings.Join(paths[2:], "/")
|
||||
name = paths[1]
|
||||
}
|
||||
account, ok := model.GetBalancedAccount(name)
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
account, ok := model.GetBalancedAccount(rawPath)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] account", name)
|
||||
return nil, "", nil, fmt.Errorf("path not found")
|
||||
}
|
||||
driver, ok := base.GetDriver(account.Type)
|
||||
if !ok {
|
||||
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
|
||||
}
|
||||
return &account, path, driver, nil
|
||||
return &account, strings.TrimPrefix(rawPath, utils.ParsePath(account.Name)), driver, nil
|
||||
}
|
||||
|
||||
func ErrorResp(c *gin.Context, err error, code int) {
|
||||
|
37
server/common/files.go
Normal file
37
server/common/files.go
Normal file
@ -0,0 +1,37 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/drivers/operate"
|
||||
"github.com/Xhofe/alist/model"
|
||||
)
|
||||
|
||||
func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) {
|
||||
account, path, driver, err := ParsePath(rawPath)
|
||||
if err != nil {
|
||||
if err.Error() == "path not found" {
|
||||
accountFiles, err := model.GetAccountFilesByPath(rawPath)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, "", err
|
||||
}
|
||||
if len(accountFiles) != 0 {
|
||||
return nil, accountFiles, nil, nil, path, nil
|
||||
}
|
||||
}
|
||||
return nil, nil, nil, nil, "", err
|
||||
}
|
||||
file, files, err := operate.Path(driver, account, path)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, "", err
|
||||
}
|
||||
if file != nil {
|
||||
return file, nil, account, driver, path, nil
|
||||
} else {
|
||||
accountFiles, err := model.GetAccountFilesByPath(rawPath)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, "", err
|
||||
}
|
||||
files = append(files, accountFiles...)
|
||||
return nil, files, account, driver, path, nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user