feat: file proxy handle
This commit is contained in:
54
server/middlewares/down.go
Normal file
54
server/middlewares/down.go
Normal file
@ -0,0 +1,54 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"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"
|
||||
stdpath "path"
|
||||
)
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
rawPath := parsePath(c.Param("path"))
|
||||
c.Set("path", rawPath)
|
||||
filename := stdpath.Base(rawPath)
|
||||
meta, err := db.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.Param("sign")
|
||||
err = sign.Verify(filename, 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.StandardizePath(path)
|
||||
}
|
||||
|
||||
func needSign(meta *model.Meta, path string) bool {
|
||||
if meta == nil || meta.Password == "" {
|
||||
return false
|
||||
}
|
||||
if !meta.SubFolder && path != meta.Path {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
Reference in New Issue
Block a user