Skip to content

Commit ced61da

Browse files
committed
feat: virtual path
1 parent a0f4383 commit ced61da

10 files changed

Lines changed: 194 additions & 124 deletions

File tree

bootstrap/account.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bootstrap
33
import (
44
"github.com/Xhofe/alist/conf"
55
"github.com/Xhofe/alist/drivers/base"
6+
"github.com/Xhofe/alist/drivers/operate"
67
"github.com/Xhofe/alist/model"
78
log "github.com/sirupsen/logrus"
89
)
@@ -20,7 +21,8 @@ func InitAccounts() {
2021
log.Errorf("no [%s] driver", account.Type)
2122
} else {
2223
log.Infof("start init account: [%s], type: [%s]", account.Name, account.Type)
23-
err := driver.Save(&accounts[i], nil)
24+
//err := driver.Save(&accounts[i], nil)
25+
err := operate.Save(driver, &accounts[i], nil)
2426
if err != nil {
2527
log.Errorf("init account [%s] error:[%s]", account.Name, err.Error())
2628
} else {

drivers/alidrive/driver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ func (driver AliDrive) Save(account *model.Account, old *model.Account) error {
9494
log.Debugf("user info: %+v", resp)
9595
account.DriveId = resp["default_drive_id"].(string)
9696
cronId, err := conf.Cron.AddFunc("@every 2h", func() {
97-
name := account.Name
98-
log.Debugf("ali account name: %s", name)
99-
newAccount, ok := model.GetAccount(name)
97+
id := account.ID
98+
log.Debugf("ali account id: %d", id)
99+
newAccount, err := model.GetAccountById(id)
100100
log.Debugf("ali account: %+v", newAccount)
101-
if !ok {
101+
if err != nil {
102102
return
103103
}
104-
err = driver.RefreshToken(&newAccount)
105-
_ = model.SaveAccount(&newAccount)
104+
err = driver.RefreshToken(newAccount)
105+
_ = model.SaveAccount(newAccount)
106106
})
107107
if err != nil {
108108
return err

drivers/operate/operate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"runtime/debug"
99
)
1010

11+
func Save(driver base.Driver, account, old *model.Account) error {
12+
return driver.Save(account, old)
13+
}
14+
1115
func Path(driver base.Driver, account *model.Account, path string) (*model.File, []model.File, error) {
1216
return driver.Path(path, account)
1317
}

model/account.go

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package model
22

33
import (
44
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/utils"
56
log "github.com/sirupsen/logrus"
67
"strings"
78
"sync"
@@ -97,6 +98,7 @@ func RegisterAccount(account Account) {
9798
accountsMap[account.Name] = account
9899
}
99100

101+
// GetAccount 根据名称获取账号(不包含负载均衡账号) 用于定时任务更新账号
100102
func GetAccount(name string) (Account, bool) {
101103
if len(accountsMap) == 1 {
102104
for _, v := range accountsMap {
@@ -107,25 +109,28 @@ func GetAccount(name string) (Account, bool) {
107109
return account, ok
108110
}
109111

110-
func GetAccountsByName(name string) []Account {
111-
accounts := make([]Account, 0)
112-
if AccountsCount() == 1 {
113-
account, _ := GetAccount("")
114-
accounts = append(accounts, account)
115-
return accounts
116-
}
117-
for _, v := range accountsMap {
118-
if v.Name == name || strings.HasPrefix(v.Name, name+balance) {
119-
accounts = append(accounts, v)
120-
}
121-
}
122-
return accounts
123-
}
112+
// GetAccountsByName 根据名称获取账号(包含负载均衡账号)
113+
//func GetAccountsByName(name string) []Account {
114+
// accounts := make([]Account, 0)
115+
// if AccountsCount() == 1 {
116+
// for _, v := range accountsMap {
117+
// accounts = append(accounts, v)
118+
// }
119+
// return accounts
120+
// }
121+
// for _, v := range accountsMap {
122+
// if v.Name == name || strings.HasPrefix(v.Name, name+balance) {
123+
// accounts = append(accounts, v)
124+
// }
125+
// }
126+
// return accounts
127+
//}
124128

125129
var balanceMap sync.Map
126130

131+
// GetBalancedAccount 根据名称获取账号,负载均衡之后的
127132
func GetBalancedAccount(name string) (Account, bool) {
128-
accounts := GetAccountsByName(name)
133+
accounts := GetAccountsByPath(name)
129134
accountNum := len(accounts)
130135
switch accountNum {
131136
case 0:
@@ -147,6 +152,7 @@ func GetBalancedAccount(name string) (Account, bool) {
147152
}
148153
}
149154

155+
// GetAccountById 根据id获取账号,用于更新账号
150156
func GetAccountById(id uint) (*Account, error) {
151157
var account Account
152158
account.ID = id
@@ -156,31 +162,100 @@ func GetAccountById(id uint) (*Account, error) {
156162
return &account, nil
157163
}
158164

159-
func GetAccountFiles() ([]File, error) {
165+
// GetAccountFiles 获取账号虚拟文件(去除负载均衡)
166+
//func GetAccountFiles() ([]File, error) {
167+
// files := make([]File, 0)
168+
// var accounts []Account
169+
// if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
170+
// return nil, err
171+
// }
172+
// for _, v := range accounts {
173+
// if strings.Contains(v.Name, balance) {
174+
// continue
175+
// }
176+
// files = append(files, File{
177+
// Name: v.Name,
178+
// Size: 0,
179+
// Driver: v.Type,
180+
// Type: conf.FOLDER,
181+
// UpdatedAt: v.UpdatedAt,
182+
// })
183+
// }
184+
// return files, nil
185+
//}
186+
187+
// GetAccounts 获取所有账号
188+
func GetAccounts() ([]Account, error) {
189+
var accounts []Account
190+
if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
191+
return nil, err
192+
}
193+
return accounts, nil
194+
}
195+
196+
// GetAccountsByPath 根据路径获取账号,最长匹配,未负载均衡
197+
// 如有账号: /a/b,/a/c,/a/d/e,/a/d/e.balance
198+
// GetAccountsByPath(/a/d/e/f) => /a/d/e,/a/d/e.balance
199+
func GetAccountsByPath(path string) []Account {
200+
accounts := make([]Account, 0)
201+
curSlashCount := 0
202+
for _, v := range accountsMap {
203+
name := utils.ParsePath(v.Name)
204+
bIndex := strings.LastIndex(name, balance)
205+
if bIndex != -1 {
206+
name = v.Name[:bIndex]
207+
}
208+
// 不是这个账号
209+
if path != name && !strings.HasPrefix(path, name+"/") {
210+
continue
211+
}
212+
slashCount := strings.Count(name, "/")
213+
// 不是最长匹配
214+
if slashCount < curSlashCount {
215+
continue
216+
}
217+
if slashCount > curSlashCount {
218+
accounts = accounts[:0]
219+
curSlashCount = slashCount
220+
}
221+
accounts = append(accounts, v)
222+
}
223+
return accounts
224+
}
225+
226+
// GetAccountFilesByPath 根据路径获取账号虚拟文件
227+
// 如有账号: /a/b,/a/c,/a/d/e,/a/b.balance1,/av
228+
// GetAccountFilesByPath(/a) => b,c,d
229+
func GetAccountFilesByPath(prefix string) ([]File, error) {
160230
files := make([]File, 0)
161231
var accounts []Account
162232
if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
163233
return nil, err
164234
}
235+
prefix = utils.ParsePath(prefix)
236+
set := make(map[string]interface{})
165237
for _, v := range accounts {
238+
// 负载均衡账号
166239
if strings.Contains(v.Name, balance) {
167240
continue
168241
}
242+
full := utils.ParsePath(v.Name)
243+
// 不是以prefix为前缀
244+
if !strings.HasPrefix(full, prefix+"/") && prefix != "/" {
245+
continue
246+
}
247+
name := strings.Split(strings.TrimPrefix(strings.TrimPrefix(full, prefix), "/"), "/")[0]
248+
if _, ok := set[name]; ok {
249+
continue
250+
}
169251
files = append(files, File{
170-
Name: v.Name,
252+
Name: name,
171253
Size: 0,
172254
Driver: v.Type,
173255
Type: conf.FOLDER,
174256
UpdatedAt: v.UpdatedAt,
175257
})
258+
set[name] = nil
176259
}
177260
return files, nil
178261
}
179-
180-
func GetAccounts() ([]Account, error) {
181-
var accounts []Account
182-
if err := conf.DB.Order(columnName("index")).Find(&accounts).Error; err != nil {
183-
return nil, err
184-
}
185-
return accounts, nil
186-
}

server/common/common.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package common
22

33
import (
4-
"errors"
54
"fmt"
65
"github.com/Xhofe/alist/drivers/base"
76
"github.com/Xhofe/alist/model"
@@ -25,30 +24,16 @@ type PathReq struct {
2524
}
2625

2726
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
28-
var path, name string
29-
switch model.AccountsCount() {
30-
case 0:
31-
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
32-
case 1:
33-
path = rawPath
34-
break
35-
default:
36-
if path == "/" {
37-
return nil, "", nil, errors.New("can't operate root of multiple accounts")
38-
}
39-
paths := strings.Split(rawPath, "/")
40-
path = "/" + strings.Join(paths[2:], "/")
41-
name = paths[1]
42-
}
43-
account, ok := model.GetBalancedAccount(name)
27+
rawPath = utils.ParsePath(rawPath)
28+
account, ok := model.GetBalancedAccount(rawPath)
4429
if !ok {
45-
return nil, "", nil, fmt.Errorf("no [%s] account", name)
30+
return nil, "", nil, fmt.Errorf("path not found")
4631
}
4732
driver, ok := base.GetDriver(account.Type)
4833
if !ok {
4934
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
5035
}
51-
return &account, path, driver, nil
36+
return &account, strings.TrimPrefix(rawPath, utils.ParsePath(account.Name)), driver, nil
5237
}
5338

5439
func ErrorResp(c *gin.Context, err error, code int) {

server/common/files.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package common
2+
3+
import (
4+
"github.com/Xhofe/alist/drivers/base"
5+
"github.com/Xhofe/alist/drivers/operate"
6+
"github.com/Xhofe/alist/model"
7+
)
8+
9+
func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) {
10+
account, path, driver, err := ParsePath(rawPath)
11+
if err != nil {
12+
if err.Error() == "path not found" {
13+
accountFiles, err := model.GetAccountFilesByPath(rawPath)
14+
if err != nil {
15+
return nil, nil, nil, nil, "", err
16+
}
17+
if len(accountFiles) != 0 {
18+
return nil, accountFiles, nil, nil, path, nil
19+
}
20+
}
21+
return nil, nil, nil, nil, "", err
22+
}
23+
file, files, err := operate.Path(driver, account, path)
24+
if err != nil {
25+
return nil, nil, nil, nil, "", err
26+
}
27+
if file != nil {
28+
return file, nil, account, driver, path, nil
29+
} else {
30+
accountFiles, err := model.GetAccountFilesByPath(rawPath)
31+
if err != nil {
32+
return nil, nil, nil, nil, "", err
33+
}
34+
files = append(files, accountFiles...)
35+
return nil, files, account, driver, path, nil
36+
}
37+
}

server/controllers/account.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"fmt"
55
"github.com/Xhofe/alist/drivers/base"
6+
"github.com/Xhofe/alist/drivers/operate"
67
"github.com/Xhofe/alist/model"
78
"github.com/Xhofe/alist/server/common"
89
"github.com/gin-gonic/gin"
@@ -37,7 +38,8 @@ func CreateAccount(c *gin.Context) {
3738
common.ErrorResp(c, err, 500)
3839
} else {
3940
log.Debugf("new account: %+v", req)
40-
err = driver.Save(&req, nil)
41+
//err = driver.Save(&req, nil)
42+
err = operate.Save(driver, &req, nil)
4143
if err != nil {
4244
common.ErrorResp(c, err, 500)
4345
return
@@ -71,7 +73,8 @@ func SaveAccount(c *gin.Context) {
7173
common.ErrorResp(c, err, 500)
7274
} else {
7375
log.Debugf("save account: %+v", req)
74-
err = driver.Save(&req, old)
76+
//err = driver.Save(&req, old)
77+
err = operate.Save(driver, &req, nil)
7578
if err != nil {
7679
common.ErrorResp(c, err, 500)
7780
return
@@ -93,7 +96,8 @@ func DeleteAccount(c *gin.Context) {
9396
} else {
9497
driver, ok := base.GetDriver(account.Type)
9598
if ok {
96-
_ = driver.Save(nil, account)
99+
//_ = driver.Save(nil, account)
100+
_ = operate.Save(driver, nil, account)
97101
} else {
98102
log.Errorf("no driver: %s", account.Type)
99103
}

0 commit comments

Comments
 (0)