mirror of https://github.com/Xhofe/alist
feat: get account by path
parent
2481676c46
commit
7b6f11fa52
|
@ -3,12 +3,12 @@ package model
|
|||
import "time"
|
||||
|
||||
type Account struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"`
|
||||
VirtualPath string `json:"virtual_path" gorm:"unique" binding:"required"`
|
||||
Index int `json:"index"`
|
||||
ID uint `json:"id" gorm:"primaryKey"` // unique key
|
||||
VirtualPath string `json:"virtual_path" gorm:"unique" binding:"required"` // must be standardized
|
||||
Index int `json:"index"` // use to sort
|
||||
Driver string `json:"driver"`
|
||||
Status string `json:"status"`
|
||||
Addition string `json:"addition"`
|
||||
Addition string `json:"addition"` // Additional information, defined in the corresponding driver
|
||||
Remark string `json:"remark"`
|
||||
Modified time.Time `json:"modified"`
|
||||
Sort
|
||||
|
|
|
@ -7,8 +7,10 @@ import (
|
|||
"github.com/alist-org/alist/v3/internal/store"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Although the driver type is stored,
|
||||
|
@ -94,8 +96,6 @@ func SaveDriverAccount(driver driver.Driver) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
var balance = ".balance"
|
||||
|
||||
// GetAccountsByPath get account by longest match path, contains balance account.
|
||||
// for example, there is /a/b,/a/c,/a/d/e,/a/d/e.balance
|
||||
// GetAccountsByPath(/a/d/e/f) => /a/d/e,/a/d/e.balance
|
||||
|
@ -103,11 +103,7 @@ func GetAccountsByPath(path string) []driver.Driver {
|
|||
accounts := make([]driver.Driver, 0)
|
||||
curSlashCount := 0
|
||||
for _, v := range accountsMap {
|
||||
virtualPath := utils.StandardizationPath(v.GetAccount().VirtualPath)
|
||||
bIndex := strings.LastIndex(virtualPath, balance)
|
||||
if bIndex != -1 {
|
||||
virtualPath = virtualPath[:bIndex]
|
||||
}
|
||||
virtualPath := utils.GetActualVirtualPath(v.GetAccount().VirtualPath)
|
||||
if virtualPath == "/" {
|
||||
virtualPath = ""
|
||||
}
|
||||
|
@ -154,7 +150,7 @@ func GetAccountFilesByPath(prefix string) []driver.FileInfo {
|
|||
set := make(map[string]interface{})
|
||||
for _, v := range accounts {
|
||||
// balance account
|
||||
if strings.Contains(v.GetAccount().VirtualPath, balance) {
|
||||
if utils.IsBalance(v.GetAccount().VirtualPath) {
|
||||
continue
|
||||
}
|
||||
full := utils.StandardizationPath(v.GetAccount().VirtualPath)
|
||||
|
@ -178,3 +174,31 @@ func GetAccountFilesByPath(prefix string) []driver.FileInfo {
|
|||
}
|
||||
return files
|
||||
}
|
||||
|
||||
var balanceMap sync.Map
|
||||
|
||||
// GetBalancedAccount get account by path
|
||||
func GetBalancedAccount(path string) driver.Driver {
|
||||
path = utils.StandardizationPath(path)
|
||||
accounts := GetAccountsByPath(path)
|
||||
accountNum := len(accounts)
|
||||
switch accountNum {
|
||||
case 0:
|
||||
return nil
|
||||
case 1:
|
||||
return accounts[0]
|
||||
default:
|
||||
virtualPath := utils.GetActualVirtualPath(accounts[0].GetAccount().VirtualPath)
|
||||
cur, ok := balanceMap.Load(virtualPath)
|
||||
i := 0
|
||||
if ok {
|
||||
i = cur.(int)
|
||||
i = (i + 1) % accountNum
|
||||
balanceMap.Store(virtualPath, i)
|
||||
} else {
|
||||
balanceMap.Store(virtualPath, i)
|
||||
}
|
||||
log.Debugln("use: ", i)
|
||||
return accounts[i]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
var balance = ".balance"
|
||||
|
||||
func IsBalance(str string) bool {
|
||||
return strings.Contains(str, balance)
|
||||
}
|
||||
|
||||
// GetActualVirtualPath remove balance suffix
|
||||
func GetActualVirtualPath(virtualPath string) string {
|
||||
bIndex := strings.LastIndex(virtualPath, ".balance")
|
||||
if bIndex != -1 {
|
||||
virtualPath = virtualPath[:bIndex]
|
||||
}
|
||||
return virtualPath
|
||||
}
|
Loading…
Reference in New Issue