feat: implement legacy operation logging

This commit is contained in:
OpenClaw Agent
2026-09-21 16:05:06 +08:00
parent add2a22416
commit 4cf4e92932
3 changed files with 72 additions and 1 deletions
+31
View File
@@ -15,6 +15,7 @@ import (
"time"
"chfs-ng/internal/auth"
"chfs-ng/internal/opslog"
"chfs-ng/internal/storage"
"github.com/emersion/go-webdav"
)
@@ -27,6 +28,7 @@ type Server struct {
Allow string
MultiRoot bool
RemoveMode int
Log *opslog.Logger
}
type fileInfo struct {
Name string `json:"name"`
@@ -68,6 +70,26 @@ func account(r *http.Request, s *Server) *auth.Account {
}
return s.Auth.Guest
}
func (s *Server) logOp(r *http.Request, action string) {
if s.Log == nil {
return
}
a := account(r, s)
user := ""
if a != nil {
user = a.Username
}
_ = s.Log.Write(clientIPString(r), user, action)
}
func clientIPString(r *http.Request) string {
ip := clientIP(r)
if ip == nil {
return "-"
}
return ip.String()
}
func writeJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(v)
@@ -140,6 +162,7 @@ func (s *Server) newdir(w http.ResponseWriter, r *http.Request) {
http.Error(w, e.Error(), 500)
return
}
s.logOp(r, fmt.Sprintf("created directory:'%s'", p))
writeJSON(w, map[string]any{"ok": true})
}
func (s *Server) rename(w http.ResponseWriter, r *http.Request) {
@@ -154,6 +177,7 @@ func (s *Server) rename(w http.ResponseWriter, r *http.Request) {
http.Error(w, e.Error(), 500)
return
}
s.logOp(r, fmt.Sprintf("renamed '%s' to '%s'", old, neu))
writeJSON(w, map[string]any{"ok": true})
}
func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
@@ -173,6 +197,7 @@ func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
http.Error(w, e.Error(), 500)
return
}
s.logOp(r, fmt.Sprintf("removed '%s'", strings.Trim(p, "/")))
}
writeJSON(w, map[string]any{"ok": true})
}
@@ -256,6 +281,7 @@ func (s *Server) newtext(w http.ResponseWriter, r *http.Request) {
}
defer f.Close()
io.WriteString(f, r.FormValue("text"))
s.logOp(r, fmt.Sprintf("create a text file:'%s'", p))
writeJSON(w, map[string]any{"ok": true})
}
func (s *Server) updatetext(w http.ResponseWriter, r *http.Request) {
@@ -277,6 +303,7 @@ func (s *Server) updatetext(w http.ResponseWriter, r *http.Request) {
http.Error(w, e.Error(), 500)
return
}
s.logOp(r, fmt.Sprintf("update text file:'%s'", p))
writeJSON(w, map[string]any{"ok": true})
}
func (s *Server) upload(w http.ResponseWriter, r *http.Request) {
@@ -305,6 +332,9 @@ func (s *Server) upload(w http.ResponseWriter, r *http.Request) {
saved = append(saved, h.Filename)
}
}
for _, name := range saved {
s.logOp(r, fmt.Sprintf("upload '%s'", path.Join(dir, name)))
}
w.WriteHeader(http.StatusCreated)
writeJSON(w, map[string]any{"ok": true, "files": saved})
}
@@ -515,6 +545,7 @@ func (s *Server) downloadDir(w http.ResponseWriter, r *http.Request) {
}
return nil
}
s.logOp(r, fmt.Sprintf("download directory '%s'", p))
if err := walk(p, ""); err != nil {
return
}