refactor: split the db package hook and cache to the op package (#2747)

* refactor:separate the setting method from the db package to the op package and add the cache

* refactor:separate the meta method from the db package to the op package

* fix:setting not load database data

* refactor:separate the user method from the db package to the op package

* refactor:remove user JoinPath error

* fix:op package user cache

* refactor:fs package list method

* fix:tile virtual paths (close #2743)

* Revert "refactor:remove user JoinPath error"

This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848.

* clean path directly may lead to unknown behavior

* fix: The path of the meta passed in must be prefix of reqPath

* chore: rename all virtualPath to mountPath

* fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath`

is_sub_path:

/a/b isn't subpath of /a/bc

* fix: don't save setting if hook error

Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
foxxorcat
2022-12-18 19:51:20 +08:00
committed by GitHub
parent f38f4f401b
commit 6024e8d832
39 changed files with 741 additions and 543 deletions

View File

@ -2,8 +2,6 @@ package fs
import (
"context"
"regexp"
"strings"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
@ -16,15 +14,15 @@ import (
func list(ctx context.Context, path string, refresh ...bool) ([]model.Obj, error) {
meta := ctx.Value("meta").(*model.Meta)
user := ctx.Value("user").(*model.User)
var objs []model.Obj
storage, actualPath, err := op.GetStorageAndActualPath(path)
virtualFiles := op.GetStorageVirtualFilesByPath(path)
if err != nil {
if len(virtualFiles) == 0 {
return nil, errors.WithMessage(err, "failed get storage")
}
} else {
_objs, err := op.List(ctx, storage, actualPath, model.ListArgs{
storage, actualPath, err := op.GetStorageAndActualPath(path)
if err != nil && len(virtualFiles) == 0 {
return nil, errors.WithMessage(err, "failed get storage")
}
var _objs []model.Obj
if storage != nil {
_objs, err = op.List(ctx, storage, actualPath, model.ListArgs{
ReqPath: path,
}, refresh...)
if err != nil {
@ -33,21 +31,13 @@ func list(ctx context.Context, path string, refresh ...bool) ([]model.Obj, error
return nil, errors.WithMessage(err, "failed get objs")
}
}
objs = make([]model.Obj, len(_objs))
copy(objs, _objs)
}
if objs == nil {
objs = virtualFiles
} else {
for _, storageFile := range virtualFiles {
if !containsByName(objs, storageFile) {
objs = append(objs, storageFile)
}
}
}
om := model.NewObjMerge()
if whetherHide(user, meta, path) {
objs = hide(objs, meta)
om.InitHideReg(meta.Hide)
}
objs := om.Merge(virtualFiles, _objs...)
// sort objs
if storage != nil {
if storage.Config().LocalSort {
@ -78,26 +68,3 @@ func whetherHide(user *model.User, meta *model.Meta, path string) bool {
// if is guest, hide
return true
}
func hide(objs []model.Obj, meta *model.Meta) []model.Obj {
var res []model.Obj
deleted := make([]bool, len(objs))
rs := strings.Split(meta.Hide, "\n")
for _, r := range rs {
re := regexp.MustCompile(r)
for i, obj := range objs {
if deleted[i] {
continue
}
if re.MatchString(obj.GetName()) {
deleted[i] = true
}
}
}
for i, obj := range objs {
if !deleted[i] {
res = append(res, obj)
}
}
return res
}