feat: expose accessible storage locations
This commit is contained in:
@@ -76,6 +76,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("/chfs/exist", s.exist)
|
||||
mux.HandleFunc("/chfs/session", s.session)
|
||||
mux.HandleFunc("/chfs/me", s.me)
|
||||
mux.HandleFunc("/chfs/roots", s.roots)
|
||||
mux.HandleFunc("/chfs/users", s.users)
|
||||
mux.HandleFunc("/chfs/password", s.password)
|
||||
mux.HandleFunc("/chfs/newtext", s.newtext)
|
||||
@@ -468,6 +469,45 @@ func (s *Server) me(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]any{"username": a.Username, "home": a.Home, "admin": a.Admin, "mask": permString(a.MaskFor(a.Home)), "managed": a.PasswordHash != "", "enabled": a.Enabled})
|
||||
}
|
||||
|
||||
type accessibleRoot struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Mask string `json:"mask"`
|
||||
Home bool `json:"home"`
|
||||
}
|
||||
|
||||
func (s *Server) roots(w http.ResponseWriter, r *http.Request) {
|
||||
a := account(r, s)
|
||||
if a == nil || a.Username == "" {
|
||||
writeJSONError(w, http.StatusUnauthorized, "not authenticated")
|
||||
return
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
out := make([]accessibleRoot, 0)
|
||||
if a.Admin && a.MaskFor("").HasRead() {
|
||||
seen[""] = true
|
||||
out = append(out, accessibleRoot{Path: "", Name: "全部文件", Mask: permString(a.MaskFor("")), Home: false})
|
||||
}
|
||||
if a.Home != "" && a.MaskFor(a.Home).HasRead() {
|
||||
home := strings.Trim(a.Home, "/")
|
||||
seen[home] = true
|
||||
out = append(out, accessibleRoot{Path: home, Name: "我的空间", Mask: permString(a.MaskFor(home)), Home: true})
|
||||
}
|
||||
for _, rule := range a.Rules {
|
||||
p := strings.Trim(rule.Dir, "/")
|
||||
if p == "" || seen[p] || rule.Deny || strings.ContainsAny(p, "*?") {
|
||||
continue
|
||||
}
|
||||
m := a.MaskFor(p)
|
||||
if !m.HasRead() {
|
||||
continue
|
||||
}
|
||||
seen[p] = true
|
||||
out = append(out, accessibleRoot{Path: p, Name: path.Base(p), Mask: permString(m), Home: false})
|
||||
}
|
||||
writeJSON(w, map[string]any{"roots": out})
|
||||
}
|
||||
|
||||
func (s *Server) password(w http.ResponseWriter, r *http.Request) {
|
||||
a := account(r, s)
|
||||
if a == nil || a.Username == "" {
|
||||
|
||||
Reference in New Issue
Block a user