Add GET /fs/browse directory-listing endpoint

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 20:15:00 -04:00
parent 1a925b3a05
commit a108964457
3 changed files with 130 additions and 0 deletions
+57
View File
@@ -2,6 +2,9 @@ package web
import (
"net/http"
"os"
"path/filepath"
"strings"
"antidrift/internal/settings"
@@ -61,3 +64,57 @@ func (s *Server) handlePostSettings(c *gin.Context) {
s.broadcast()
c.JSON(http.StatusOK, req)
}
type browseEntry struct {
Name string `json:"name"`
Path string `json:"path"`
IsDir bool `json:"is_dir"`
}
type browseResponse struct {
Dir string `json:"dir"`
Parent string `json:"parent"`
Entries []browseEntry `json:"entries"`
}
// handleBrowse lists subdirectories plus .md files under dir, so the UI can build
// a file picker that returns a real server-side path. Dotfiles are NOT hidden —
// the default profile lives under ~/.antidrift. Localhost-only daemon; no jail
// beyond OS permissions (same trust boundary as the rest of the UI).
func (s *Server) handleBrowse(c *gin.Context) {
dir := strings.TrimSpace(c.Query("dir"))
if dir == "" {
dir = s.browseStart()
}
dir = filepath.Clean(dir)
infos, err := os.ReadDir(dir)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
resp := browseResponse{Dir: dir, Parent: filepath.Dir(dir), Entries: []browseEntry{}}
for _, e := range infos {
name := e.Name()
full := filepath.Join(dir, name)
if e.IsDir() {
resp.Entries = append(resp.Entries, browseEntry{Name: name, Path: full, IsDir: true})
} else if strings.HasSuffix(name, ".md") {
resp.Entries = append(resp.Entries, browseEntry{Name: name, Path: full})
}
}
c.JSON(http.StatusOK, resp)
}
// browseStart picks the directory of the current knowledge path, else $HOME.
func (s *Server) browseStart() string {
s.settingsMu.Lock()
kp := s.settings.KnowledgePath
s.settingsMu.Unlock()
if kp != "" {
return filepath.Dir(kp)
}
if home, err := os.UserHomeDir(); err == nil {
return home
}
return "/"
}