🎇 hide files while webdav visitor

pull/548/head
微凉 2022-01-18 18:48:08 +08:00
parent 0a901a2eb0
commit 6d34e88360
4 changed files with 37 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings" "strings"
@ -85,3 +86,18 @@ func SuccessResp(c *gin.Context, data ...interface{}) {
Data: data[0], Data: data[0],
}) })
} }
func Hide(meta *model.Meta, files []model.File) []model.File {
//meta, _ := model.GetMetaByPath(path)
if meta != nil && meta.Hide != "" {
tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return files
}

View File

@ -10,24 +10,8 @@ import (
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings"
) )
func Hide(meta *model.Meta, files []model.File) []model.File {
//meta, _ := model.GetMetaByPath(path)
if meta != nil && meta.Hide != "" {
tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return files
}
func Pagination(files []model.File, req *common.PathReq) (int, []model.File) { func Pagination(files []model.File, req *common.PathReq) (int, []model.File) {
pageNum, pageSize := req.PageNum, req.PageSize pageNum, pageSize := req.PageNum, req.PageSize
total := len(files) total := len(files)
@ -100,7 +84,7 @@ func Path(c *gin.Context) {
return return
} }
if !ok { if !ok {
files = Hide(meta, files) files = common.Hide(meta, files)
} }
c.JSON(200, common.Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,
@ -159,7 +143,7 @@ func Path(c *gin.Context) {
}) })
} else { } else {
if !ok { if !ok {
files = Hide(meta, files) files = common.Hide(meta, files)
} }
if driver.Config().LocalSort { if driver.Config().LocalSort {
model.SortFiles(files, account) model.SortFiles(files, account)

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/webdav" "github.com/Xhofe/alist/server/webdav"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
@ -58,6 +59,8 @@ func WebDAVAuth(c *gin.Context) {
(conf.GetStr("Visitor WebDAV username") == "" && (conf.GetStr("Visitor WebDAV username") == "" &&
conf.GetStr("Visitor WebDAV password") == "") { conf.GetStr("Visitor WebDAV password") == "") {
if !utils.IsContain([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) { if !utils.IsContain([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) {
ctx := context.WithValue(c.Request.Context(), "visitor", true)
c.Request = c.Request.WithContext(ctx)
c.Next() c.Next()
return return
} }

View File

@ -43,7 +43,7 @@ func (fs *FileSystem) File(rawPath string) (*model.File, error) {
return driver.File(path_, account) return driver.File(path_, account)
} }
func (fs *FileSystem) Files(rawPath string) ([]model.File, error) { func (fs *FileSystem) Files(ctx context.Context, rawPath string) ([]model.File, error) {
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
if model.AccountsCount() > 1 && rawPath == "/" { if model.AccountsCount() > 1 && rawPath == "/" {
files, err := model.GetAccountFiles() files, err := model.GetAccountFiles()
@ -56,7 +56,20 @@ func (fs *FileSystem) Files(rawPath string) ([]model.File, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return driver.Files(path_, account) files, err := driver.Files(path_, account)
if err != nil {
return nil, err
}
meta, _ := model.GetMetaByPath(rawPath)
if visitor := ctx.Value("visitor"); visitor != nil {
if visitor.(bool) {
log.Debug("visitor")
files = common.Hide(meta, files)
}
} else {
log.Debug("admin")
}
return files, nil
} }
func ClientIP(r *http.Request) string { func ClientIP(r *http.Request) string {
@ -260,7 +273,7 @@ func walkFS(
depth = 0 depth = 0
} }
files, err := fs.Files(name) files, err := fs.Files(ctx, name)
if err != nil { if err != nil {
return err return err
} }