Add file size and modified time to directory listings
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user