feat: dirs api
This commit is contained in:
parent
e28c1e436d
commit
efa20cc7bd
@ -23,6 +23,11 @@ type ListReq struct {
|
|||||||
Password string `json:"password" form:"password"`
|
Password string `json:"password" form:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DirReq struct {
|
||||||
|
Path string `json:"path" form:"path"`
|
||||||
|
Password string `json:"password" form:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
type ObjResp struct {
|
type ObjResp struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
@ -56,7 +61,7 @@ func FsList(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.Set("meta", meta)
|
c.Set("meta", meta)
|
||||||
if !canAccess(user, meta, req.Path, req.Password) {
|
if !canAccess(user, meta, req.Path, req.Password) {
|
||||||
common.ErrorStrResp(c, "password is incorrect", 401)
|
common.ErrorStrResp(c, "password is incorrect", 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
objs, err := fs.List(c, req.Path)
|
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 {
|
func getReadme(meta *model.Meta, path string) string {
|
||||||
if meta != nil && (utils.PathEqual(meta.Path, path) || meta.RSub) {
|
if meta != nil && (utils.PathEqual(meta.Path, path) || meta.RSub) {
|
||||||
return meta.Readme
|
return meta.Readme
|
||||||
@ -152,7 +204,7 @@ func FsGet(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.Set("meta", meta)
|
c.Set("meta", meta)
|
||||||
if !canAccess(user, meta, req.Path, req.Password) {
|
if !canAccess(user, meta, req.Path, req.Password) {
|
||||||
common.ErrorStrResp(c, "password is incorrect", 401)
|
common.ErrorStrResp(c, "password is incorrect", 403)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj, err := fs.Get(c, req.Path)
|
obj, err := fs.Get(c, req.Path)
|
||||||
|
@ -79,6 +79,7 @@ func Init(r *gin.Engine) {
|
|||||||
public.GET("/settings", controllers.PublicSettings)
|
public.GET("/settings", controllers.PublicSettings)
|
||||||
public.Any("/list", controllers.FsList)
|
public.Any("/list", controllers.FsList)
|
||||||
public.Any("/get", controllers.FsGet)
|
public.Any("/get", controllers.FsGet)
|
||||||
|
public.Any("/dirs", controllers.FsDirs)
|
||||||
|
|
||||||
// gust can't
|
// gust can't
|
||||||
fs := api.Group("/fs")
|
fs := api.Group("/fs")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user