alist/server/middlewares/down.go

64 lines
1.4 KiB
Go
Raw Normal View History

2022-06-28 13:58:46 +00:00
package middlewares
import (
2022-09-11 11:39:24 +00:00
"strings"
2022-08-03 06:26:59 +00:00
2022-11-30 14:10:07 +00:00
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
2022-06-28 13:58:46 +00:00
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-28 13:58:46 +00:00
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
func Down(c *gin.Context) {
rawPath := parsePath(c.Param("path"))
c.Set("path", rawPath)
meta, err := op.GetNearestMeta(rawPath)
2022-06-28 13:58:46 +00:00
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
c.Set("meta", meta)
// verify sign
if needSign(meta, rawPath) {
2022-09-04 10:29:41 +00:00
s := c.Query("sign")
err = sign.Verify(rawPath, strings.TrimSuffix(s, "/"))
2022-06-28 13:58:46 +00:00
if err != nil {
common.ErrorResp(c, err, 401)
c.Abort()
return
}
}
c.Next()
}
// TODO: implement
// path maybe contains # ? etc.
func parsePath(path string) string {
return utils.FixAndCleanPath(path)
2022-06-28 13:58:46 +00:00
}
func needSign(meta *model.Meta, path string) bool {
2022-11-30 14:10:07 +00:00
if setting.GetBool(conf.SignAll) {
return true
}
if common.IsStorageSignEnabled(path) {
return true
}
2022-06-28 13:58:46 +00:00
if meta == nil || meta.Password == "" {
return false
}
2022-06-30 07:41:58 +00:00
if !meta.PSub && path != meta.Path {
2022-06-28 13:58:46 +00:00
return false
}
return true
}