Skip to content

Commit

Permalink
feat: dirs api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 10, 2022
1 parent e28c1e4 commit efa20cc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
56 changes: 54 additions & 2 deletions server/controllers/fsread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit efa20cc

Please sign in to comment.