From c4ccbb3f9511da09e59ebaf7dd4c6d206f4955fa Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 7 Nov 2026 00:54:36 +0000 Subject: [PATCH] Add file size and modified time to directory listings --- docs/notes/files-panel.md | 3 +++ internal/protocol/directories.go | 14 +++++++---- internal/server/directory_listing.go | 11 +++++++-- internal/server/directory_listing_test.go | 30 +++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 docs/notes/files-panel.md diff --git a/docs/notes/files-panel.md b/docs/notes/files-panel.md new file mode 100644 index 00000000..df31cda5 --- /dev/null +++ b/docs/notes/files-panel.md @@ -0,0 +1,3 @@ +# Files panel + +- Directory listings now include file `size` and RFC3339 UTC `modified` metadata. diff --git a/internal/protocol/directories.go b/internal/protocol/directories.go index 24e9a43e..71f32393 100644 --- a/internal/protocol/directories.go +++ b/internal/protocol/directories.go @@ -3,8 +3,9 @@ package protocol // Directory browsing for the chat folder picker. A client sends // Message{Type: ListDirectory, ID: , ListDirectory: &DirectoryListRequest{Path: ...}} // and the server echoes the ID on a DirectoryListing reply. Listing is -// restricted to the server user's home directory subtree, returns names and -// kinds only (never file contents) and is bounded by DirectoryListingMaxEntries. +// restricted to the server user's home directory subtree, returns names, kinds, +// and file metadata (never file contents), and is bounded by +// DirectoryListingMaxEntries. const ( ListDirectory = "list_directory" DirectoryListing = "directory_listing" @@ -23,8 +24,8 @@ type DirectoryListRequest struct { // DirectoryListingResult is the directory_listing reply. Path is the absolute, // cleaned directory listed (expressed under Home), Parent is null at the home // root, and Entries are directories first, then files, each group sorted -// case-insensitively. Files are listed by name only so a picker can show -// context; they are not selectable. +// case-insensitively. Entries include file size and modification time when +// available; file contents are never returned, and files are not selectable. type DirectoryListingResult struct { Path string `json:"path"` Home string `json:"home"` @@ -35,11 +36,14 @@ type DirectoryListingResult struct { // DirectoryEntry is one listed name. IsDir is true only for a directory (or a // symlink to one) that stays inside the home subtree; IsGitRepo marks a -// directory containing .git; Hidden marks a dot-name. +// directory containing .git; Hidden marks a dot-name. Size and Modified are +// populated for files; directories have no file metadata. type DirectoryEntry struct { Name string `json:"name"` Path string `json:"path"` IsDir bool `json:"is_dir"` IsGitRepo bool `json:"is_git_repo"` Hidden bool `json:"hidden"` + Size int64 `json:"size,omitempty"` + Modified string `json:"modified,omitempty"` } diff --git a/internal/server/directory_listing.go b/internal/server/directory_listing.go index 6b8af91c..1117305d 100644 --- a/internal/server/directory_listing.go +++ b/internal/server/directory_listing.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sort" "strings" + "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -131,8 +132,8 @@ func describePathError(path string, err error) error { } // readDirectoryEntries reads at most directoryListingScanLimit names, sorts -// directories first then case-insensitively, and caps the result. Only names -// and kinds are read; no file is opened. +// directories first then case-insensitively, and caps the result. No file is +// opened. func readDirectoryEntries(realHome, realDir, shown string, maxEntries int) ([]protocol.DirectoryEntry, bool, error) { dir, err := os.Open(realDir) if err != nil { @@ -191,6 +192,12 @@ func describeEntry(realHome, realDir, shown string, item fs.DirEntry) protocol.D entry.IsDir = true } } + if !entry.IsDir { + if info, err := item.Info(); err == nil { + entry.Size = info.Size() + entry.Modified = info.ModTime().UTC().Format(time.RFC3339) + } + } if entry.IsDir { if _, err := os.Lstat(filepath.Join(full, ".git")); err == nil { entry.IsGitRepo = true diff --git a/internal/server/directory_listing_test.go b/internal/server/directory_listing_test.go index ccaf183b..b0eddf2a 100644 --- a/internal/server/directory_listing_test.go +++ b/internal/server/directory_listing_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" "testing" + "time" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/coder/websocket/wsjson" @@ -95,6 +96,35 @@ func TestListDirectorySubdirectoryHasParent(t *testing.T) { } } +func TestListDirectoryFileMetadata(t *testing.T) { + home, _ := newListingHome(t) + file := filepath.Join(home, "Notes.txt") + modified := time.Date(2024, time.March, 14, 15, 9, 26, 0, time.UTC) + if err := os.Chtimes(file, modified, modified); err != nil { + t.Fatal(err) + } + + listing, err := listHomeDirectory(home, "~", protocol.DirectoryListingMaxEntries) + if err != nil { + t.Fatal(err) + } + var gotFile, gotDir protocol.DirectoryEntry + for _, entry := range listing.Entries { + switch entry.Name { + case "Notes.txt": + gotFile = entry + case "repo": + gotDir = entry + } + } + if gotFile.Size != int64(len("secret contents")) || gotFile.Modified != modified.Format(time.RFC3339) { + t.Fatalf("file metadata = %+v", gotFile) + } + if gotDir.Size != 0 || gotDir.Modified != "" { + t.Fatalf("directory metadata = %+v", gotDir) + } +} + func TestListDirectoryRefusesPathsOutsideHome(t *testing.T) { home, outside := newListingHome(t) if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {