alist/internal/fs/list.go

96 lines
2.2 KiB
Go
Raw Normal View History

package fs
import (
"context"
2022-08-03 06:26:59 +00:00
"regexp"
"strings"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
2022-06-27 11:10:02 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// List files
func list(ctx context.Context, path string) ([]model.Obj, error) {
2022-06-27 11:51:23 +00:00
meta := ctx.Value("meta").(*model.Meta)
2022-06-27 11:10:02 +00:00
user := ctx.Value("user").(*model.User)
2022-07-10 06:45:39 +00:00
storage, actualPath, err := operations.GetStorageAndActualPath(path)
virtualFiles := operations.GetStorageVirtualFilesByPath(path)
if err != nil {
if len(virtualFiles) != 0 {
return virtualFiles, nil
}
2022-07-10 06:45:39 +00:00
return nil, errors.WithMessage(err, "failed get storage")
}
2022-08-11 12:32:17 +00:00
objs, err := operations.List(ctx, storage, actualPath, model.ListArgs{
ReqPath: path,
})
if err != nil {
log.Errorf("%+v", err)
if len(virtualFiles) != 0 {
return virtualFiles, nil
}
2022-06-27 11:10:02 +00:00
return nil, errors.WithMessage(err, "failed get objs")
}
2022-07-10 06:45:39 +00:00
for _, storageFile := range virtualFiles {
if !containsByName(objs, storageFile) {
objs = append(objs, storageFile)
}
}
2022-06-27 11:10:02 +00:00
if whetherHide(user, meta, path) {
2022-06-30 08:09:06 +00:00
objs = hide(objs, meta)
2022-06-27 11:10:02 +00:00
}
// sort objs
2022-07-10 06:45:39 +00:00
if storage.Config().LocalSort {
model.SortFiles(objs, storage.GetStorage().OrderBy, storage.GetStorage().OrderDirection)
2022-06-27 11:10:02 +00:00
}
2022-07-10 06:45:39 +00:00
model.ExtractFolder(objs, storage.GetStorage().ExtractFolder)
2022-06-27 11:10:02 +00:00
return objs, nil
}
func whetherHide(user *model.User, meta *model.Meta, path string) bool {
// if is admin, don't hide
2022-06-30 08:09:06 +00:00
if user.CanSeeHides() {
2022-06-27 11:10:02 +00:00
return false
}
// if meta is nil, don't hide
if meta == nil {
return false
}
// if meta.Hide is empty, don't hide
if meta.Hide == "" {
return false
}
// if meta doesn't apply to sub_folder, don't hide
2022-06-30 07:41:58 +00:00
if !utils.PathEqual(meta.Path, path) && !meta.HSub {
2022-06-27 11:10:02 +00:00
return false
}
// if is guest, hide
2022-06-30 08:09:06 +00:00
return true
2022-06-27 11:10:02 +00:00
}
2022-06-30 08:09:06 +00:00
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.Compile(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
}