Skip to content

Commit

Permalink
feat: get account by path
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 10, 2022
1 parent 2481676 commit 7b6f11f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
8 changes: 4 additions & 4 deletions internal/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 32 additions & 8 deletions internal/operations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -94,20 +96,14 @@ 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
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 = ""
}
Expand Down Expand Up @@ -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)
Expand All @@ -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]
}
}
18 changes: 18 additions & 0 deletions pkg/utils/balance.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7b6f11f

Please sign in to comment.