feat: hide objects
This commit is contained in:
parent
fba96d024f
commit
3934d9029e
@ -7,6 +7,8 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// List files
|
// List files
|
||||||
@ -35,7 +37,7 @@ func list(ctx context.Context, path string) ([]model.Obj, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if whetherHide(user, meta, path) {
|
if whetherHide(user, meta, path) {
|
||||||
hide(objs, meta)
|
objs = hide(objs, meta)
|
||||||
}
|
}
|
||||||
// sort objs
|
// sort objs
|
||||||
if account.Config().LocalSort {
|
if account.Config().LocalSort {
|
||||||
@ -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 {
|
func whetherHide(user *model.User, meta *model.Meta, path string) bool {
|
||||||
// if is admin, don't hide
|
// if is admin, don't hide
|
||||||
if user.IsAdmin() {
|
if user.CanSeeHides() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// if meta is nil, don't hide
|
// if meta is nil, don't hide
|
||||||
@ -63,12 +65,28 @@ func whetherHide(user *model.User, meta *model.Meta, path string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// if is guest, hide
|
// if is guest, hide
|
||||||
if user.IsGuest() {
|
return true
|
||||||
return true
|
|
||||||
}
|
|
||||||
return !user.CanSeeHides()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hide(objs []model.Obj, meta *model.Meta) {
|
func hide(objs []model.Obj, meta *model.Meta) []model.Obj {
|
||||||
// TODO: hide
|
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
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/db"
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
@ -35,6 +38,11 @@ func CreateMeta(c *gin.Context) {
|
|||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
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)
|
req.Path = utils.StandardizePath(req.Path)
|
||||||
if err := db.CreateMeta(&req); err != nil {
|
if err := db.CreateMeta(&req); err != nil {
|
||||||
common.ErrorResp(c, err, 500, true)
|
common.ErrorResp(c, err, 500, true)
|
||||||
@ -49,6 +57,11 @@ func UpdateMeta(c *gin.Context) {
|
|||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
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)
|
req.Path = utils.StandardizePath(req.Path)
|
||||||
if err := db.UpdateMeta(&req); err != nil {
|
if err := db.UpdateMeta(&req); err != nil {
|
||||||
common.ErrorResp(c, err, 500, true)
|
common.ErrorResp(c, err, 500, true)
|
||||||
@ -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) {
|
func DeleteMeta(c *gin.Context) {
|
||||||
idStr := c.Query("id")
|
idStr := c.Query("id")
|
||||||
id, err := strconv.Atoi(idStr)
|
id, err := strconv.Atoi(idStr)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user