feat: virtual path

This commit is contained in:
Xhofe
2022-03-31 20:43:17 +08:00
parent a0f4383d41
commit ced61da33a
10 changed files with 204 additions and 134 deletions

View File

@ -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
View 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
}
}

View File

@ -3,6 +3,7 @@ package controllers
import (
"fmt"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
@ -37,7 +38,8 @@ func CreateAccount(c *gin.Context) {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("new account: %+v", req)
err = driver.Save(&req, nil)
//err = driver.Save(&req, nil)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -71,7 +73,8 @@ func SaveAccount(c *gin.Context) {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("save account: %+v", req)
err = driver.Save(&req, old)
//err = driver.Save(&req, old)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -93,7 +96,8 @@ func DeleteAccount(c *gin.Context) {
} else {
driver, ok := base.GetDriver(account.Type)
if ok {
_ = driver.Save(nil, account)
//_ = driver.Save(nil, account)
_ = operate.Save(driver, nil, account)
} else {
log.Errorf("no driver: %s", account.Type)
}

View File

@ -1,7 +1,6 @@
package file
import (
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
@ -18,31 +17,18 @@ func Folder(c *gin.Context) {
return
}
var files = make([]model.File, 0)
var err error
if model.AccountsCount() > 1 && (req.Path == "/" || req.Path == "") {
files, err = model.GetAccountFiles()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
} else {
account, path, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file, rawFiles, err := operate.Path(driver, account, path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if file != nil {
common.ErrorStrResp(c, "Not folder", 400)
}
for _, file := range rawFiles {
if file.IsDir() {
files = append(files, file)
}
_, rawFiles, _, _, _, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if rawFiles == nil {
common.ErrorStrResp(c, "not a folder", 400)
return
}
for _, file := range rawFiles {
if file.IsDir() {
files = append(files, file)
}
}
c.JSON(200, common.Resp{

View File

@ -5,7 +5,6 @@ import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
@ -79,39 +78,12 @@ func Path(c *gin.Context) {
if meta != nil && meta.Upload {
upload = true
}
if model.AccountsCount() > 1 && (req.Path == "/" || req.Path == "") {
files, err := model.GetAccountFiles()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if !ok {
files = common.Hide(meta, files)
}
c.JSON(200, common.Resp{
Code: 200,
Message: "success",
Data: PathResp{
Type: "folder",
Meta: Meta{
Driver: "root",
},
Files: files,
},
})
return
}
err := CheckPagination(&req)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
account, path, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file, files, err := operate.Path(driver, account, path)
file, files, account, driver, path, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -147,10 +119,14 @@ func Path(c *gin.Context) {
if !ok {
files = common.Hide(meta, files)
}
if driver.Config().LocalSort {
model.SortFiles(files, account)
driverName := "root"
if driver != nil {
if driver.Config().LocalSort {
model.SortFiles(files, account)
}
model.ExtractFolder(files, account)
driverName = driver.Config().Name
}
model.ExtractFolder(files, account)
total, files := Pagination(files, &req)
c.JSON(200, common.Resp{
Code: 200,
@ -158,7 +134,7 @@ func Path(c *gin.Context) {
Data: PathResp{
Type: "folder",
Meta: Meta{
Driver: driver.Config().Name,
Driver: driverName,
Upload: upload,
Total: total,
},

View File

@ -50,17 +50,18 @@ func (fs *FileSystem) File(rawPath string) (*model.File, error) {
func (fs *FileSystem) Files(ctx context.Context, rawPath string) ([]model.File, error) {
rawPath = utils.ParsePath(rawPath)
var files []model.File
var err error
if model.AccountsCount() > 1 && rawPath == "/" {
files, err = model.GetAccountFiles()
} else {
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return nil, err
}
files, err = operate.Files(driver, account, path_)
}
//var files []model.File
//var err error
//if model.AccountsCount() > 1 && rawPath == "/" {
// files, err = model.GetAccountFilesByPath("/")
//} else {
// account, path_, driver, err := common.ParsePath(rawPath)
// if err != nil {
// return nil, err
// }
// files, err = operate.Files(driver, account, path_)
//}
_, files, _, _, _, err := common.Path(rawPath)
if err != nil {
return nil, err
}