mirror of https://github.com/Xhofe/alist
refactor:remove user JoinPath error
parent
fd45fd431d
commit
4e20daaf9e
|
@ -91,6 +91,6 @@ func (u User) CanWebdavManage() bool {
|
||||||
return u.IsAdmin() || (u.Permission>>9)&1 == 1
|
return u.IsAdmin() || (u.Permission>>9)&1 == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u User) JoinPath(reqPath string) (string, error) {
|
func (u User) JoinPath(reqPath string) string {
|
||||||
return utils.JoinBasePath(u.BasePath, reqPath)
|
return utils.JoinBasePath(u.BasePath, reqPath)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,6 @@ func EncodePath(path string, all ...bool) string {
|
||||||
return strings.Join(seg, "/")
|
return strings.Join(seg, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func JoinBasePath(basePath, reqPath string) (string, error) {
|
func JoinBasePath(basePath, reqPath string) string {
|
||||||
return stdpath.Join(FixAndCleanPath(basePath), FixAndCleanPath(reqPath)), nil
|
return stdpath.Join(FixAndCleanPath(basePath), FixAndCleanPath(reqPath))
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,11 +56,7 @@ func AddAria2(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath := user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, url := range req.Urls {
|
for _, url := range req.Urls {
|
||||||
err := aria2.AddURI(c, url, reqPath)
|
err := aria2.AddURI(c, url, reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -26,11 +26,7 @@ func FsMkdir(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath := user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !user.CanWrite() {
|
if !user.CanWrite() {
|
||||||
meta, err := op.GetNearestMeta(stdpath.Dir(reqPath))
|
meta, err := op.GetNearestMeta(stdpath.Dir(reqPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -73,16 +69,9 @@ func FsMove(c *gin.Context) {
|
||||||
common.ErrorResp(c, errs.PermissionDenied, 403)
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
srcDir, err := user.JoinPath(req.SrcDir)
|
srcDir := user.JoinPath(req.SrcDir)
|
||||||
if err != nil {
|
dstDir := user.JoinPath(req.DstDir)
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
dstDir, err := user.JoinPath(req.DstDir)
|
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, name := range req.Names {
|
for _, name := range req.Names {
|
||||||
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir)
|
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -110,16 +99,9 @@ func FsCopy(c *gin.Context) {
|
||||||
common.ErrorResp(c, errs.PermissionDenied, 403)
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
srcDir, err := user.JoinPath(req.SrcDir)
|
srcDir := user.JoinPath(req.SrcDir)
|
||||||
if err != nil {
|
dstDir := user.JoinPath(req.DstDir)
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
dstDir, err := user.JoinPath(req.DstDir)
|
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var addedTask []string
|
var addedTask []string
|
||||||
for _, name := range req.Names {
|
for _, name := range req.Names {
|
||||||
ok, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir)
|
ok, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir)
|
||||||
|
@ -157,11 +139,8 @@ func FsRename(c *gin.Context) {
|
||||||
common.ErrorResp(c, errs.PermissionDenied, 403)
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath := user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := fs.Rename(c, reqPath, req.Name); err != nil {
|
if err := fs.Rename(c, reqPath, req.Name); err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
|
@ -190,11 +169,8 @@ func FsRemove(c *gin.Context) {
|
||||||
common.ErrorResp(c, errs.PermissionDenied, 403)
|
common.ErrorResp(c, errs.PermissionDenied, 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqDir, err := user.JoinPath(req.Dir)
|
reqDir := user.JoinPath(req.Dir)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, name := range req.Names {
|
for _, name := range req.Names {
|
||||||
err := fs.Remove(c, stdpath.Join(reqDir, name))
|
err := fs.Remove(c, stdpath.Join(reqDir, name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -56,11 +56,8 @@ func FsList(c *gin.Context) {
|
||||||
}
|
}
|
||||||
req.Validate()
|
req.Validate()
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath := user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
meta, err := op.GetNearestMeta(reqPath)
|
meta, err := op.GetNearestMeta(reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
||||||
|
@ -111,12 +108,7 @@ func FsDirs(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tmp, err := user.JoinPath(req.Path)
|
reqPath = user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reqPath = tmp
|
|
||||||
}
|
}
|
||||||
meta, err := op.GetNearestMeta(reqPath)
|
meta, err := op.GetNearestMeta(reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -228,11 +220,8 @@ func FsGet(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath := user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
meta, err := op.GetNearestMeta(reqPath)
|
meta, err := op.GetNearestMeta(reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
||||||
|
@ -339,11 +328,8 @@ func FsOther(c *gin.Context) {
|
||||||
}
|
}
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
var err error
|
var err error
|
||||||
req.Path, err = user.JoinPath(req.Path)
|
req.Path = user.JoinPath(req.Path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
meta, err := op.GetNearestMeta(req.Path)
|
meta, err := op.GetNearestMeta(req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
||||||
|
|
|
@ -21,11 +21,8 @@ func FsStream(c *gin.Context) {
|
||||||
}
|
}
|
||||||
asTask := c.GetHeader("As-Task") == "true"
|
asTask := c.GetHeader("As-Task") == "true"
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
path, err = user.JoinPath(path)
|
path = user.JoinPath(path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
dir, name := stdpath.Split(path)
|
dir, name := stdpath.Split(path)
|
||||||
sizeStr := c.GetHeader("Content-Length")
|
sizeStr := c.GetHeader("Content-Length")
|
||||||
size, err := strconv.ParseInt(sizeStr, 10, 64)
|
size, err := strconv.ParseInt(sizeStr, 10, 64)
|
||||||
|
@ -64,11 +61,8 @@ func FsForm(c *gin.Context) {
|
||||||
}
|
}
|
||||||
asTask := c.GetHeader("As-Task") == "true"
|
asTask := c.GetHeader("As-Task") == "true"
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
path, err = user.JoinPath(path)
|
path = user.JoinPath(path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
storage, err := fs.GetStorage(path)
|
storage, err := fs.GetStorage(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
|
|
|
@ -34,11 +34,8 @@ func Search(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
req.Parent, err = user.JoinPath(req.Parent)
|
req.Parent = user.JoinPath(req.Parent)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 400)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := req.Validate(); err != nil {
|
if err := req.Validate(); err != nil {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
|
|
|
@ -22,11 +22,8 @@ func FsUp(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.MustGet("user").(*model.User)
|
user := c.MustGet("user").(*model.User)
|
||||||
path, err = user.JoinPath(path)
|
path = user.JoinPath(path)
|
||||||
if err != nil {
|
|
||||||
common.ErrorResp(c, err, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
meta, err := op.GetNearestMeta(stdpath.Dir(path))
|
meta, err := op.GetNearestMeta(stdpath.Dir(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
||||||
|
|
|
@ -183,10 +183,8 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) (status
|
||||||
}
|
}
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
allow := "OPTIONS, LOCK, PUT, MKCOL"
|
allow := "OPTIONS, LOCK, PUT, MKCOL"
|
||||||
if fi, err := fs.Get(ctx, reqPath); err == nil {
|
if fi, err := fs.Get(ctx, reqPath); err == nil {
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
|
@ -211,10 +209,8 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta
|
||||||
// TODO: check locks for read-only access??
|
// TODO: check locks for read-only access??
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
fi, err := fs.Get(ctx, reqPath)
|
fi, err := fs.Get(ctx, reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusNotFound, err
|
return http.StatusNotFound, err
|
||||||
|
@ -269,10 +265,8 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) (status i
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
// TODO: return MultiStatus where appropriate.
|
// TODO: return MultiStatus where appropriate.
|
||||||
|
|
||||||
// "godoc os RemoveAll" says that "If the path does not exist, RemoveAll
|
// "godoc os RemoveAll" says that "If the path does not exist, RemoveAll
|
||||||
|
@ -305,10 +299,8 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
|
||||||
// comments in http.checkEtag.
|
// comments in http.checkEtag.
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
obj := model.Object{
|
obj := model.Object{
|
||||||
Name: path.Base(reqPath),
|
Name: path.Base(reqPath),
|
||||||
Size: r.ContentLength,
|
Size: r.ContentLength,
|
||||||
|
@ -354,10 +346,7 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request) (status in
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.ContentLength > 0 {
|
if r.ContentLength > 0 {
|
||||||
return http.StatusUnsupportedMediaType, nil
|
return http.StatusUnsupportedMediaType, nil
|
||||||
|
@ -404,14 +393,9 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request) (status
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
src, err = user.JoinPath(src)
|
src = user.JoinPath(src)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
dst = user.JoinPath(dst)
|
||||||
}
|
|
||||||
dst, err = user.JoinPath(dst)
|
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method == "COPY" {
|
if r.Method == "COPY" {
|
||||||
// Section 7.5.1 says that a COPY only needs to lock the destination,
|
// Section 7.5.1 says that a COPY only needs to lock the destination,
|
||||||
|
@ -502,10 +486,8 @@ func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request) (retStatus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reqPath, status, err := h.stripPrefix(r.URL.Path)
|
reqPath, status, err := h.stripPrefix(r.URL.Path)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return status, err
|
return status, err
|
||||||
}
|
}
|
||||||
|
@ -586,10 +568,8 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) (status
|
||||||
}
|
}
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
fi, err := fs.Get(ctx, reqPath)
|
fi, err := fs.Get(ctx, reqPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errs.IsObjectNotFound(err) {
|
if errs.IsObjectNotFound(err) {
|
||||||
|
@ -665,10 +645,8 @@ func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request) (statu
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
user := ctx.Value("user").(*model.User)
|
user := ctx.Value("user").(*model.User)
|
||||||
reqPath, err = user.JoinPath(reqPath)
|
reqPath = user.JoinPath(reqPath)
|
||||||
if err != nil {
|
|
||||||
return 403, err
|
|
||||||
}
|
|
||||||
if _, err := fs.Get(ctx, reqPath); err != nil {
|
if _, err := fs.Get(ctx, reqPath); err != nil {
|
||||||
if errs.IsObjectNotFound(err) {
|
if errs.IsObjectNotFound(err) {
|
||||||
return http.StatusNotFound, err
|
return http.StatusNotFound, err
|
||||||
|
|
Loading…
Reference in New Issue