feat: get account files by path

refactor/fs
Noah Hsu 2022-06-09 23:05:52 +08:00
parent 164dab49ac
commit 2481676c46
1 changed files with 46 additions and 0 deletions

View File

@ -132,3 +132,49 @@ func GetAccountsByPath(path string) []driver.Driver {
})
return accounts
}
// GetAccountFilesByPath Obtain the virtual file generated by the account according to the path
// for example, there are: /a/b,/a/c,/a/d/e,/a/b.balance1,/av
// GetAccountFilesByPath(/a) => b,c,d
func GetAccountFilesByPath(prefix string) []driver.FileInfo {
files := make([]driver.FileInfo, 0)
accounts := make([]driver.Driver, len(accountsMap))
i := 0
for _, v := range accountsMap {
accounts[i] = v
i += 1
}
sort.Slice(accounts, func(i, j int) bool {
if accounts[i].GetAccount().Index == accounts[j].GetAccount().Index {
return accounts[i].GetAccount().VirtualPath < accounts[j].GetAccount().VirtualPath
}
return accounts[i].GetAccount().Index < accounts[j].GetAccount().Index
})
prefix = utils.StandardizationPath(prefix)
set := make(map[string]interface{})
for _, v := range accounts {
// balance account
if strings.Contains(v.GetAccount().VirtualPath, balance) {
continue
}
full := utils.StandardizationPath(v.GetAccount().VirtualPath)
if len(full) <= len(prefix) {
continue
}
// not prefixed with `prefix`
if !strings.HasPrefix(full, prefix+"/") && prefix != "/" {
continue
}
name := strings.Split(strings.TrimPrefix(strings.TrimPrefix(full, prefix), "/"), "/")[0]
if _, ok := set[name]; ok {
continue
}
files = append(files, model.File{
Name: name,
Size: 0,
Modified: v.GetAccount().Modified,
})
set[name] = nil
}
return files
}