alist/server/middlewares/path.go

41 lines
908 B
Go
Raw Normal View History

2021-12-07 07:56:43 +00:00
package middlewares
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func PathCheck(c *gin.Context) {
var req common.PathReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
2022-01-04 13:21:27 +00:00
c.Set("req", req)
2021-12-07 12:16:34 +00:00
token := c.GetHeader("Authorization")
if token == conf.Token {
c.Set("admin", true)
2021-12-07 12:16:34 +00:00
c.Next()
return
}
2021-12-07 07:56:43 +00:00
meta, err := model.GetMetaByPath(req.Path)
if err == nil {
if meta.Password != "" && meta.Password != req.Password {
2022-01-13 13:23:27 +00:00
common.ErrorStrResp(c, "Wrong password", 401)
2021-12-07 07:56:43 +00:00
c.Abort()
return
}
2022-01-04 13:21:27 +00:00
} else if conf.GetBool("check parent folder") {
2021-12-07 07:56:43 +00:00
if !common.CheckParent(utils.Dir(req.Path), req.Password) {
2022-01-13 13:23:27 +00:00
common.ErrorStrResp(c, "Wrong password", 401)
2021-12-07 07:56:43 +00:00
c.Abort()
return
}
}
c.Next()
2022-01-04 13:21:27 +00:00
}