mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 20:26:26 +08:00

* Enable blank issue * chore(README.md): update docs (temporally) * Update FUNDING.yml * chore: purge README.md * chore: change module name to OpenListTeam/OpenList * fix: fix link errors * chore: remove v3 in module name * fix: resolve some conficts * fix: resolve conficts * docs: update with latest file --------- Co-authored-by: ShenLin <773933146@qq.com> Co-authored-by: Hantong Chen <cxwdyx620@gmail.com> Co-authored-by: joshua <i@joshua.su> Co-authored-by: Hantong Chen <70561268+cxw620@users.noreply.github.com>
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/OpenListTeam/OpenList/internal/conf"
|
|
"github.com/OpenListTeam/OpenList/internal/setting"
|
|
|
|
"github.com/OpenListTeam/OpenList/internal/errs"
|
|
"github.com/OpenListTeam/OpenList/internal/model"
|
|
"github.com/OpenListTeam/OpenList/internal/op"
|
|
"github.com/OpenListTeam/OpenList/pkg/utils"
|
|
"github.com/OpenListTeam/OpenList/server/common"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
|
|
return func(c *gin.Context) {
|
|
rawPath := parsePath(c.Param("path"))
|
|
c.Set("path", rawPath)
|
|
meta, err := op.GetNearestMeta(rawPath)
|
|
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) {
|
|
s := c.Query("sign")
|
|
err = verifyFunc(rawPath, strings.TrimSuffix(s, "/"))
|
|
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)
|
|
}
|
|
|
|
func needSign(meta *model.Meta, path string) bool {
|
|
if setting.GetBool(conf.SignAll) {
|
|
return true
|
|
}
|
|
if common.IsStorageSignEnabled(path) {
|
|
return true
|
|
}
|
|
if meta == nil || meta.Password == "" {
|
|
return false
|
|
}
|
|
if !meta.PSub && path != meta.Path {
|
|
return false
|
|
}
|
|
return true
|
|
}
|