Skip to content

Commit

Permalink
feat: hide objects
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 30, 2022
1 parent fba96d0 commit 3934d90
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
34 changes: 26 additions & 8 deletions internal/fs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"regexp"
"strings"
)

// List files
Expand Down Expand Up @@ -35,7 +37,7 @@ func list(ctx context.Context, path string) ([]model.Obj, error) {
}
}
if whetherHide(user, meta, path) {
hide(objs, meta)
objs = hide(objs, meta)
}
// sort objs
if account.Config().LocalSort {
Expand All @@ -47,7 +49,7 @@ func list(ctx context.Context, path string) ([]model.Obj, error) {

func whetherHide(user *model.User, meta *model.Meta, path string) bool {
// if is admin, don't hide
if user.IsAdmin() {
if user.CanSeeHides() {
return false
}
// if meta is nil, don't hide
Expand All @@ -63,12 +65,28 @@ func whetherHide(user *model.User, meta *model.Meta, path string) bool {
return false
}
// if is guest, hide
if user.IsGuest() {
return true
}
return !user.CanSeeHides()
return true
}

func hide(objs []model.Obj, meta *model.Meta) {
// TODO: hide
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
}
24 changes: 24 additions & 0 deletions server/controllers/meta.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package controllers

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
Expand Down Expand Up @@ -35,6 +38,11 @@ func CreateMeta(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
r, err := validHide(req.Hide)
if err != nil {
common.ErrorStrResp(c, fmt.Sprintf("%s is illegal: %s", r, err.Error()), 400)
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil {
common.ErrorResp(c, err, 500, true)
Expand All @@ -49,6 +57,11 @@ func UpdateMeta(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
r, err := validHide(req.Hide)
if err != nil {
common.ErrorStrResp(c, fmt.Sprintf("%s is illegal: %s", r, err.Error()), 400)
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil {
common.ErrorResp(c, err, 500, true)
Expand All @@ -57,6 +70,17 @@ func UpdateMeta(c *gin.Context) {
}
}

func validHide(hide string) (string, error) {
rs := strings.Split(hide, "\n")
for _, r := range rs {
_, err := regexp.Compile(r)
if err != nil {
return r, err
}
}
return "", nil
}

func DeleteMeta(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
Expand Down

0 comments on commit 3934d90

Please sign in to comment.