From efa20cc7bdf78014ca7dc2009a9389d13968bb30 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 10 Jul 2022 14:09:31 +0800 Subject: [PATCH] feat: dirs api --- server/controllers/fsread.go | 56 ++++++++++++++++++++++++++++++++++-- server/router.go | 1 + 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/server/controllers/fsread.go b/server/controllers/fsread.go index c2a0f0833b6..51baf9818ff 100644 --- a/server/controllers/fsread.go +++ b/server/controllers/fsread.go @@ -23,6 +23,11 @@ type ListReq struct { Password string `json:"password" form:"password"` } +type DirReq struct { + Path string `json:"path" form:"path"` + Password string `json:"password" form:"password"` +} + type ObjResp struct { Name string `json:"name"` Size int64 `json:"size"` @@ -56,7 +61,7 @@ func FsList(c *gin.Context) { } c.Set("meta", meta) if !canAccess(user, meta, req.Path, req.Password) { - common.ErrorStrResp(c, "password is incorrect", 401) + common.ErrorStrResp(c, "password is incorrect", 403) return } objs, err := fs.List(c, req.Path) @@ -73,6 +78,53 @@ func FsList(c *gin.Context) { }) } +func FsDirs(c *gin.Context) { + var req DirReq + if err := c.ShouldBind(&req); err != nil { + common.ErrorResp(c, err, 400) + return + } + user := c.MustGet("user").(*model.User) + req.Path = stdpath.Join(user.BasePath, req.Path) + meta, err := db.GetNearestMeta(req.Path) + if err != nil { + if !errors.Is(errors.Cause(err), errs.MetaNotFound) { + common.ErrorResp(c, err, 500, true) + return + } + } + c.Set("meta", meta) + if !canAccess(user, meta, req.Path, req.Password) { + common.ErrorStrResp(c, "password is incorrect", 403) + return + } + objs, err := fs.List(c, req.Path) + if err != nil { + common.ErrorResp(c, err, 500) + return + } + dirs := filterDirs(objs) + common.SuccessResp(c, dirs) +} + +type DirResp struct { + Name string `json:"name"` + Modified time.Time `json:"modified"` +} + +func filterDirs(objs []model.Obj) []DirResp { + var dirs []DirResp + for _, obj := range objs { + if obj.IsDir() { + dirs = append(dirs, DirResp{ + Name: obj.GetName(), + Modified: obj.ModTime(), + }) + } + } + return dirs +} + func getReadme(meta *model.Meta, path string) string { if meta != nil && (utils.PathEqual(meta.Path, path) || meta.RSub) { return meta.Readme @@ -152,7 +204,7 @@ func FsGet(c *gin.Context) { } c.Set("meta", meta) if !canAccess(user, meta, req.Path, req.Password) { - common.ErrorStrResp(c, "password is incorrect", 401) + common.ErrorStrResp(c, "password is incorrect", 403) return } obj, err := fs.Get(c, req.Path) diff --git a/server/router.go b/server/router.go index f9c7f790eab..6afe26c7ebb 100644 --- a/server/router.go +++ b/server/router.go @@ -79,6 +79,7 @@ func Init(r *gin.Engine) { public.GET("/settings", controllers.PublicSettings) public.Any("/list", controllers.FsList) public.Any("/get", controllers.FsGet) + public.Any("/dirs", controllers.FsDirs) // gust can't fs := api.Group("/fs")