mirror of
https://github.com/runbear-io/beardrive.git
synced 2026-08-25 08:08:08 +02:00
feat: web viewer, project-local config, selective sync, Claude Code plugin
- sfs-web (cmd/sfs-web, internal/webapp): read-only Obsidian-style web UI serving a local folder (default) or an sfs remote; markdown rendering with [[wikilinks]], task lists and tables, file downloads with ETags, per-file provenance from the journals; added to goreleaser builds - .sfs project file (internal/config): per-folder volume/remote/include settings that travel with the folder, win over the global registry, and never sync; daemon picks up edits live - .sfsignore + include lists (internal/syncer): gitignore-style selective sync with ! re-includes, applied symmetrically in scan and materialize; newly ignored files stop syncing without being deleted anywhere - Claude Code plugin (plugin/, .claude-plugin/): sfs skill, /sfs:mount and /sfs:status commands, turn-boundary sync hooks (blocking pull on prompt, async push on stop); installable via the repo's marketplace manifest - CLAUDE.md and .claude project settings for Claude Code development Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HHEUaYfFHhmDvqLYw74Ehz
This commit is contained in:
co-authored by
Claude Fable 5
parent
d22fe71d1c
commit
2d29ff8aac
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ProjectFile is the name of the per-folder settings file at the mount root.
|
||||
// It travels with the project (copy the folder, `sfs mnt .`, and the same
|
||||
// volume/remote apply) but is never synced — remotes and credentials setups
|
||||
// are often device-specific, and syncing it would let one device silently
|
||||
// repoint another.
|
||||
const ProjectFile = ".sfs"
|
||||
|
||||
// Project holds the settings stored in <folder>/.sfs.
|
||||
type Project struct {
|
||||
Volume string `json:"volume,omitempty"`
|
||||
Remote string `json:"remote,omitempty"`
|
||||
// Include optionally narrows what syncs: when non-empty, only paths
|
||||
// matching one of these patterns (gitignore-style, same syntax as
|
||||
// .sfsignore) are scanned and materialized.
|
||||
Include []string `json:"include,omitempty"`
|
||||
}
|
||||
|
||||
// LoadProject reads <folder>/.sfs; ok is false if the file does not exist.
|
||||
func LoadProject(folder string) (Project, bool, error) {
|
||||
var p Project
|
||||
data, err := os.ReadFile(filepath.Join(folder, ProjectFile))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return p, false, nil
|
||||
}
|
||||
return p, false, err
|
||||
}
|
||||
if err := json.Unmarshal(data, &p); err != nil {
|
||||
return p, false, fmt.Errorf("parse %s: %w", ProjectFile, err)
|
||||
}
|
||||
return p, true, nil
|
||||
}
|
||||
|
||||
// SaveProject writes <folder>/.sfs.
|
||||
func SaveProject(folder string, p Project) error {
|
||||
return writeJSON(filepath.Join(folder, ProjectFile), p)
|
||||
}
|
||||
|
||||
// EffectiveMount resolves a folder's mount settings: the project file wins
|
||||
// over the global registry, so hand-edits to .sfs (or a folder copied with
|
||||
// its .sfs) take effect without re-registering. Found reports whether the
|
||||
// folder is known at all (registered or carrying a project file).
|
||||
func EffectiveMount(folder string) (mi MountInfo, proj Project, found bool, err error) {
|
||||
mounts, err := LoadMounts()
|
||||
if err != nil {
|
||||
return mi, proj, false, err
|
||||
}
|
||||
mi, registered := mounts[folder]
|
||||
proj, hasProj, err := LoadProject(folder)
|
||||
if err != nil {
|
||||
return mi, proj, false, err
|
||||
}
|
||||
if hasProj {
|
||||
if proj.Volume != "" {
|
||||
mi.Volume = proj.Volume
|
||||
}
|
||||
if proj.Remote != "" {
|
||||
mi.Remote = proj.Remote
|
||||
}
|
||||
}
|
||||
return mi, proj, registered || hasProj, nil
|
||||
}
|
||||
@@ -106,6 +106,22 @@ func Stop(volDir, mountID string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// overlayProject applies the folder's .sfs settings on top of the registry
|
||||
// entry; the project file wins so hand-edits take effect on the next tick.
|
||||
func overlayProject(folder string, mi config.MountInfo) config.MountInfo {
|
||||
proj, ok, err := config.LoadProject(folder)
|
||||
if err != nil || !ok {
|
||||
return mi
|
||||
}
|
||||
if proj.Volume != "" {
|
||||
mi.Volume = proj.Volume
|
||||
}
|
||||
if proj.Remote != "" {
|
||||
mi.Remote = proj.Remote
|
||||
}
|
||||
return mi
|
||||
}
|
||||
|
||||
// Run is the daemon main loop, executed in the foreground of the (usually
|
||||
// detached) `sfs daemon run` process.
|
||||
func Run(folder string, scanInterval, remoteInterval time.Duration) error {
|
||||
@@ -120,6 +136,7 @@ func Run(folder string, scanInterval, remoteInterval time.Duration) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("%s is not an sfs mount", folder)
|
||||
}
|
||||
mi = overlayProject(folder, mi)
|
||||
volDir, err := config.VolumeDir(mi.Volume)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -150,13 +167,15 @@ func Run(folder string, scanInterval, remoteInterval time.Duration) error {
|
||||
var lastRemote time.Time
|
||||
|
||||
for {
|
||||
// Pick up `sfs remote set` / `sfs umnt --forget` without restarting.
|
||||
// Pick up `sfs remote set`, .sfs edits, and `sfs umnt --forget`
|
||||
// without restarting.
|
||||
if m, err := config.LoadMounts(); err == nil {
|
||||
cur, ok := m[folder]
|
||||
if !ok {
|
||||
log.Printf("mount unregistered; exiting")
|
||||
return nil
|
||||
}
|
||||
cur = overlayProject(folder, cur)
|
||||
if cur.Remote != mi.Remote {
|
||||
log.Printf("remote changed: %q -> %q", mi.Remote, cur.Remote)
|
||||
if be != nil {
|
||||
|
||||
@@ -31,8 +31,8 @@ type Op struct {
|
||||
Device string `json:"device"`
|
||||
DeviceName string `json:"device_name,omitempty"`
|
||||
Author string `json:"author,omitempty"`
|
||||
Kind string `json:"kind"` // "put" or "delete"
|
||||
Path string `json:"path"` // slash-separated, relative to volume root
|
||||
Kind string `json:"kind"` // "put" or "delete"
|
||||
Path string `json:"path"` // slash-separated, relative to volume root
|
||||
Blob string `json:"blob,omitempty"` // sha256 hex of content (put only)
|
||||
Size int64 `json:"size,omitempty"`
|
||||
Mode uint32 `json:"mode,omitempty"` // permission bits
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package syncer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Multi-device behavior of .sfsignore and the .sfs include list.
|
||||
|
||||
func TestIgnoredFilesDoNotSync(t *testing.T) {
|
||||
be := sharedRemote(t)
|
||||
a := newDevice(t, "deva", be)
|
||||
b := newDevice(t, "devb", be)
|
||||
|
||||
write(t, a.Folder, IgnoreFile, "*.secret\n")
|
||||
write(t, a.Folder, "notes.md", "hello")
|
||||
write(t, a.Folder, "key.secret", "hunter2")
|
||||
cycle(t, a)
|
||||
cycle(t, b)
|
||||
|
||||
if got := read(t, b.Folder, "notes.md"); got != "hello" {
|
||||
t.Fatalf("notes.md = %q", got)
|
||||
}
|
||||
if got := read(t, b.Folder, IgnoreFile); got != "*.secret\n" {
|
||||
t.Fatalf(".sfsignore should sync like a normal file, got %q", got)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(b.Folder, "key.secret")); !os.IsNotExist(err) {
|
||||
t.Fatal("ignored file must not reach other devices")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewlyIgnoredFileIsNotDeletedRemotely(t *testing.T) {
|
||||
be := sharedRemote(t)
|
||||
a := newDevice(t, "deva", be)
|
||||
b := newDevice(t, "devb", be)
|
||||
|
||||
write(t, a.Folder, "debug.log", "lines")
|
||||
cycle(t, a)
|
||||
cycle(t, b)
|
||||
if read(t, b.Folder, "debug.log") != "lines" {
|
||||
t.Fatal("setup: file should have synced")
|
||||
}
|
||||
|
||||
// A opts out afterwards; the file must stop syncing without a delete
|
||||
// op, so it stays on disk on every device.
|
||||
write(t, a.Folder, IgnoreFile, "*.log\n")
|
||||
res := cycle(t, a)
|
||||
if res.LocalOps != 1 { // only the .sfsignore put, no delete for debug.log
|
||||
t.Fatalf("LocalOps = %d, want 1 (the .sfsignore itself)", res.LocalOps)
|
||||
}
|
||||
cycle(t, b)
|
||||
cycle(t, b) // second cycle: filter from the pulled .sfsignore is active
|
||||
if read(t, a.Folder, "debug.log") != "lines" || read(t, b.Folder, "debug.log") != "lines" {
|
||||
t.Fatal("newly ignored file must remain on disk everywhere")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIncludeListLimitsSync(t *testing.T) {
|
||||
be := sharedRemote(t)
|
||||
a := newDevice(t, "deva", be)
|
||||
b := newDevice(t, "devb", be)
|
||||
|
||||
write(t, a.Folder, ".sfs", `{"include": ["docs/"]}`)
|
||||
write(t, a.Folder, "docs/guide.md", "included")
|
||||
write(t, a.Folder, "src/main.go", "excluded")
|
||||
cycle(t, a)
|
||||
cycle(t, b)
|
||||
|
||||
if got := read(t, b.Folder, "docs/guide.md"); got != "included" {
|
||||
t.Fatalf("docs/guide.md = %q", got)
|
||||
}
|
||||
for _, absent := range []string{"src/main.go", ".sfs"} {
|
||||
if _, err := os.Stat(filepath.Join(b.Folder, absent)); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s must not sync", absent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package syncer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IgnoreFile is the per-folder opt-out list at the mount root. It uses a
|
||||
// gitignore-style syntax and, unlike the .sfs settings file, syncs like any
|
||||
// other file so every device shares the same rules.
|
||||
const IgnoreFile = ".sfsignore"
|
||||
|
||||
// Filter decides which paths sync. A path syncs when it is not ignored and,
|
||||
// if an include list is set, matches at least one include pattern.
|
||||
//
|
||||
// Pattern syntax (a practical gitignore subset): one pattern per line,
|
||||
// blank lines and #-comments skipped, `!` re-includes, a trailing `/`
|
||||
// matches directories only, a `/` anywhere else anchors the pattern to the
|
||||
// mount root (otherwise it matches at any depth), `*` matches within a path
|
||||
// segment, `**` across segments, `?` a single character.
|
||||
type Filter struct {
|
||||
ignore []pattern
|
||||
include []pattern
|
||||
negated bool // any `!` rules → directory pruning is unsafe
|
||||
}
|
||||
|
||||
type pattern struct {
|
||||
re *regexp.Regexp
|
||||
negate bool
|
||||
}
|
||||
|
||||
// loadFilter builds the filter for a folder from its .sfsignore (if any)
|
||||
// plus the include list from the .sfs settings file.
|
||||
func loadFilter(folder string, include []string) (*Filter, error) {
|
||||
f := &Filter{}
|
||||
for _, line := range include {
|
||||
if p, ok := compile(line); ok {
|
||||
f.include = append(f.include, p)
|
||||
}
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(folder, IgnoreFile))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return f, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
p, ok := compile(line)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
f.ignore = append(f.ignore, p)
|
||||
if p.negate {
|
||||
f.negated = true
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// compile turns one pattern line into a regexp over slash-separated paths.
|
||||
// The regexp also matches everything under a matched directory. Returns
|
||||
// ok=false for blanks, comments, and invalid patterns.
|
||||
func compile(line string) (pattern, bool) {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
return pattern{}, false
|
||||
}
|
||||
var p pattern
|
||||
if strings.HasPrefix(line, "!") {
|
||||
p.negate = true
|
||||
line = strings.TrimSpace(line[1:])
|
||||
}
|
||||
anchored := strings.HasPrefix(line, "/")
|
||||
dirOnly := strings.HasSuffix(line, "/")
|
||||
line = strings.Trim(line, "/")
|
||||
if line == "" {
|
||||
return pattern{}, false
|
||||
}
|
||||
anchored = anchored || strings.Contains(line, "/")
|
||||
|
||||
var b strings.Builder
|
||||
if anchored {
|
||||
b.WriteString("^")
|
||||
} else {
|
||||
b.WriteString("(^|.*/)")
|
||||
}
|
||||
for i := 0; i < len(line); i++ {
|
||||
switch line[i] {
|
||||
case '*':
|
||||
if i+1 < len(line) && line[i+1] == '*' {
|
||||
b.WriteString(".*")
|
||||
i++
|
||||
} else {
|
||||
b.WriteString("[^/]*")
|
||||
}
|
||||
case '?':
|
||||
b.WriteString("[^/]")
|
||||
default:
|
||||
b.WriteString(regexp.QuoteMeta(line[i : i+1]))
|
||||
}
|
||||
}
|
||||
if dirOnly {
|
||||
b.WriteString("/.*$") // must match something *inside* the directory
|
||||
} else {
|
||||
b.WriteString("(/.*)?$")
|
||||
}
|
||||
re, err := regexp.Compile(b.String())
|
||||
if err != nil {
|
||||
return pattern{}, false
|
||||
}
|
||||
p.re = re
|
||||
return p, true
|
||||
}
|
||||
|
||||
// Skip reports whether a file path should not sync.
|
||||
func (f *Filter) Skip(rel string) bool {
|
||||
if f.ignoredFile(rel) {
|
||||
return true
|
||||
}
|
||||
if len(f.include) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, p := range f.include {
|
||||
if p.re.MatchString(rel) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// PruneDir reports whether a whole directory can be skipped during the
|
||||
// scan walk. Pruning is conservative: never with `!` rules (a child could
|
||||
// be re-included) or an include list (a deep child could match).
|
||||
func (f *Filter) PruneDir(rel string) bool {
|
||||
if f.negated || len(f.include) > 0 {
|
||||
return false
|
||||
}
|
||||
return f.ignoredFile(rel + "/")
|
||||
}
|
||||
|
||||
// ignoredFile applies the ignore rules in order; the last match wins, so
|
||||
// `!` patterns can re-include what an earlier pattern excluded.
|
||||
func (f *Filter) ignoredFile(rel string) bool {
|
||||
ignored := false
|
||||
for _, p := range f.ignore {
|
||||
if p.re.MatchString(rel) {
|
||||
ignored = !p.negate
|
||||
}
|
||||
}
|
||||
return ignored
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package syncer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func filterFrom(t *testing.T, ignore string, include []string) *Filter {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
if ignore != "" {
|
||||
if err := os.WriteFile(filepath.Join(dir, IgnoreFile), []byte(ignore), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
f, err := loadFilter(dir, include)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func TestIgnorePatterns(t *testing.T) {
|
||||
cases := []struct {
|
||||
ignore string
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"*.log", "a.log", true},
|
||||
{"*.log", "sub/dir/a.log", true},
|
||||
{"*.log", "a.log.txt", false},
|
||||
{"# comment\n\n*.tmp", "x.tmp", true},
|
||||
{"build/", "build/out.bin", true},
|
||||
{"build/", "build", false}, // dir-only pattern must not match a file
|
||||
{"build/", "sub/build/x", true},
|
||||
{"/docs", "docs/a.md", true},
|
||||
{"/docs", "sub/docs/a.md", false}, // anchored
|
||||
{"docs/*.md", "docs/a.md", true},
|
||||
{"docs/*.md", "docs/sub/a.md", false}, // * stays within a segment
|
||||
{"docs/**/a.md", "docs/x/y/a.md", true},
|
||||
{"secret?.txt", "secret1.txt", true},
|
||||
{"secret?.txt", "secret10.txt", false},
|
||||
{"*.log\n!keep.log", "keep.log", false},
|
||||
{"*.log\n!keep.log", "other.log", true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
f := filterFrom(t, c.ignore, nil)
|
||||
if got := f.Skip(c.path); got != c.want {
|
||||
t.Errorf("ignore %q: Skip(%q) = %v, want %v", c.ignore, c.path, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIncludePatterns(t *testing.T) {
|
||||
f := filterFrom(t, "", []string{"docs/", "*.md"})
|
||||
for path, want := range map[string]bool{
|
||||
"docs/deep/x.bin": false, // under an included dir
|
||||
"README.md": false, // matches *.md anywhere
|
||||
"notes/plan.md": false,
|
||||
"src/main.go": true, // matches nothing → skipped
|
||||
} {
|
||||
if got := f.Skip(path); got != want {
|
||||
t.Errorf("Skip(%q) = %v, want %v", path, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoreBeatsInclude(t *testing.T) {
|
||||
f := filterFrom(t, "docs/private.md", []string{"docs/"})
|
||||
if !f.Skip("docs/private.md") {
|
||||
t.Error("ignored file inside an included dir should be skipped")
|
||||
}
|
||||
if f.Skip("docs/public.md") {
|
||||
t.Error("non-ignored file inside an included dir should sync")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneDir(t *testing.T) {
|
||||
f := filterFrom(t, "node_modules/", nil)
|
||||
if !f.PruneDir("node_modules") || !f.PruneDir("sub/node_modules") {
|
||||
t.Error("plain dir ignore should prune the walk")
|
||||
}
|
||||
if f.PruneDir("src") {
|
||||
t.Error("unmatched dir must not be pruned")
|
||||
}
|
||||
// negations and includes make pruning unsafe
|
||||
if filterFrom(t, "node_modules/\n!node_modules/keep.js", nil).PruneDir("node_modules") {
|
||||
t.Error("must not prune when ! rules exist")
|
||||
}
|
||||
if filterFrom(t, "node_modules/", []string{"docs/"}).PruneDir("node_modules") {
|
||||
t.Error("must not prune when an include list exists")
|
||||
}
|
||||
}
|
||||
+34
-11
@@ -60,7 +60,9 @@ func (r *Result) Activity() bool {
|
||||
return r.LocalOps > 0 || r.PulledOps > 0 || r.Conflicts > 0 || r.Materialized > 0
|
||||
}
|
||||
|
||||
var ignoreNames = map[string]bool{".DS_Store": true}
|
||||
// config.ProjectFile (.sfs) never syncs: remotes are device-specific and
|
||||
// syncing it would let one device silently repoint another.
|
||||
var ignoreNames = map[string]bool{".DS_Store": true, config.ProjectFile: true}
|
||||
var ignoreDirs = map[string]bool{".git": true, ".sfs": true}
|
||||
|
||||
func ignoredFile(name string) bool {
|
||||
@@ -88,9 +90,17 @@ func (s *Session) Cycle(ctx context.Context) (*Result, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read own journal: %w", err)
|
||||
}
|
||||
proj, _, err := config.LoadProject(s.Folder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filter, err := loadFilter(s.Folder, proj.Include)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load %s: %w", IgnoreFile, err)
|
||||
}
|
||||
|
||||
// 1. Scan the working folder and journal any local changes.
|
||||
localOps, err := s.scan(cache, &st, int64(len(myOps)))
|
||||
localOps, err := s.scan(cache, &st, int64(len(myOps)), filter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan: %w", err)
|
||||
}
|
||||
@@ -139,7 +149,7 @@ func (s *Session) Cycle(ctx context.Context) (*Result, error) {
|
||||
return nil, fmt.Errorf("read journals: %w", err)
|
||||
}
|
||||
target := journal.Replay(all)
|
||||
n, err := s.materialize(target, cache)
|
||||
n, err := s.materialize(target, cache, filter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("materialize: %w", err)
|
||||
}
|
||||
@@ -165,8 +175,11 @@ func (s *Session) Cycle(ctx context.Context) (*Result, error) {
|
||||
}
|
||||
|
||||
// scan diffs the working folder against the state cache and returns ops for
|
||||
// every local change, storing new content in the blob store.
|
||||
func (s *Session) scan(cache map[string]store.CachedFile, st *store.SyncState, seqBase int64) ([]journal.Op, error) {
|
||||
// every local change, storing new content in the blob store. Filtered paths
|
||||
// are neither journaled nor deleted: a path that becomes ignored is dropped
|
||||
// from the cache without a delete op, so opting out locally never removes
|
||||
// the file from other devices.
|
||||
func (s *Session) scan(cache map[string]store.CachedFile, st *store.SyncState, seqBase int64, filter *Filter) ([]journal.Op, error) {
|
||||
seen := make(map[string]bool, len(cache))
|
||||
var ops []journal.Op
|
||||
nextOp := func(kind, rel string) journal.Op {
|
||||
@@ -189,12 +202,12 @@ func (s *Session) scan(cache map[string]store.CachedFile, st *store.SyncState, s
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if d.IsDir() {
|
||||
if ignoreDirs[d.Name()] {
|
||||
if ignoreDirs[d.Name()] || filter.PruneDir(rel) {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !d.Type().IsRegular() || ignoredFile(d.Name()) {
|
||||
if !d.Type().IsRegular() || ignoredFile(d.Name()) || filter.Skip(rel) {
|
||||
return nil
|
||||
}
|
||||
info, err := d.Info()
|
||||
@@ -229,10 +242,15 @@ func (s *Session) scan(cache map[string]store.CachedFile, st *store.SyncState, s
|
||||
}
|
||||
|
||||
for rel := range cache {
|
||||
if !seen[rel] {
|
||||
ops = append(ops, nextOp(journal.KindDelete, rel))
|
||||
delete(cache, rel)
|
||||
if seen[rel] {
|
||||
continue
|
||||
}
|
||||
if filter.Skip(rel) {
|
||||
delete(cache, rel) // newly filtered, not deleted: stop tracking silently
|
||||
continue
|
||||
}
|
||||
ops = append(ops, nextOp(journal.KindDelete, rel))
|
||||
delete(cache, rel)
|
||||
}
|
||||
return ops, nil
|
||||
}
|
||||
@@ -390,9 +408,14 @@ func sanitize(s string) string {
|
||||
|
||||
// materialize applies the merged state to the working folder, never
|
||||
// clobbering files that changed since the scan earlier in this cycle.
|
||||
func (s *Session) materialize(target map[string]journal.FileState, cache map[string]store.CachedFile) (int, error) {
|
||||
// Filtered paths are not written: other devices' files that match the local
|
||||
// ignore/include rules simply don't appear here.
|
||||
func (s *Session) materialize(target map[string]journal.FileState, cache map[string]store.CachedFile, filter *Filter) (int, error) {
|
||||
changed := 0
|
||||
for rel, want := range target {
|
||||
if filter.Skip(rel) {
|
||||
continue
|
||||
}
|
||||
c, ok := cache[rel]
|
||||
if ok && c.Blob == want.Blob && c.Mode == want.Mode {
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DirSource serves a plain local folder straight from disk — no sfs remote
|
||||
// or volume needed. Meant for debugging the webapp (and as a quick local
|
||||
// markdown browser): the tree reflects the folder live, provenance is just
|
||||
// file mtimes, and content streams from the filesystem.
|
||||
type DirSource struct {
|
||||
Root string
|
||||
}
|
||||
|
||||
var skipNames = map[string]bool{".DS_Store": true, ".sfs": true}
|
||||
var skipDirs = map[string]bool{".git": true, ".sfs": true}
|
||||
|
||||
func (d *DirSource) Files(_ context.Context) (map[string]FileInfo, error) {
|
||||
files := make(map[string]FileInfo)
|
||||
err := filepath.WalkDir(d.Root, func(p string, e fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return nil // skip unreadable entries
|
||||
}
|
||||
rel, err := filepath.Rel(d.Root, p)
|
||||
if err != nil || rel == "." {
|
||||
return nil
|
||||
}
|
||||
if e.IsDir() {
|
||||
if skipDirs[e.Name()] {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !e.Type().IsRegular() || skipNames[e.Name()] || strings.HasPrefix(e.Name(), ".sfs-tmp-") {
|
||||
return nil
|
||||
}
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
files[filepath.ToSlash(rel)] = FileInfo{
|
||||
// Synthetic content identity for the ETag; changes when the
|
||||
// file does, which is all revalidation needs.
|
||||
Blob: fmt.Sprintf("dir-%d-%d", info.ModTime().UnixNano(), info.Size()),
|
||||
Size: info.Size(),
|
||||
Time: info.ModTime().UTC(),
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// Open streams a file from disk. Paths are only ever snapshot map keys
|
||||
// (produced by Files above), so they cannot escape Root.
|
||||
func (d *DirSource) Open(_ context.Context, path string, _ FileInfo) (io.ReadCloser, error) {
|
||||
return os.Open(filepath.Join(d.Root, filepath.FromSlash(path)))
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func dirServer(t *testing.T, files map[string]string) http.Handler {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
for rel, content := range files {
|
||||
abs := filepath.Join(root, filepath.FromSlash(rel))
|
||||
if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(abs, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
s := &Server{Source: &DirSource{Root: root}, Remote: root, Volume: "local", Refresh: 0}
|
||||
return s.Handler()
|
||||
}
|
||||
|
||||
func TestDirSourceServesFolder(t *testing.T) {
|
||||
h := dirServer(t, map[string]string{
|
||||
"README.md": "# Local",
|
||||
"notes/plan.md": "content",
|
||||
".sfs": `{"volume":"x"}`, // settings file must be hidden
|
||||
".git/config": "noise", // .git must be skipped
|
||||
})
|
||||
|
||||
var root Node
|
||||
if err := json.Unmarshal(get(t, h, "/api/tree").Body.Bytes(), &root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
names := []string{}
|
||||
for _, n := range root.Children {
|
||||
names = append(names, n.Name)
|
||||
}
|
||||
if len(root.Children) != 2 || root.Children[0].Name != "notes" || root.Children[1].Name != "README.md" {
|
||||
t.Fatalf("tree children = %v, want [notes README.md]", names)
|
||||
}
|
||||
|
||||
rec := get(t, h, "/api/file?path=notes/plan.md")
|
||||
if rec.Code != 200 || rec.Body.String() != "content" {
|
||||
t.Fatalf("file: %d %q", rec.Code, rec.Body)
|
||||
}
|
||||
if rec.Header().Get("ETag") == "" {
|
||||
t.Fatal("dir source should still produce ETags")
|
||||
}
|
||||
|
||||
rec = get(t, h, "/api/render?path=README.md")
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Local") {
|
||||
t.Fatalf("render: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
|
||||
if rec := get(t, h, "/api/file?path=.git/config"); rec.Code != 404 {
|
||||
t.Fatalf(".git content must be hidden, got %d", rec.Code)
|
||||
}
|
||||
if rec := get(t, h, "/api/file?path=../escape"); rec.Code != 404 {
|
||||
t.Fatalf("path traversal must 404, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
)
|
||||
|
||||
var md = goldmark.New(
|
||||
goldmark.WithExtensions(extension.GFM),
|
||||
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
|
||||
)
|
||||
|
||||
// wikiRe matches Obsidian-style [[target]] and [[target|label]] links.
|
||||
var wikiRe = regexp.MustCompile(`\[\[([^\]|]+)(?:\|([^\]]+))?\]\]`)
|
||||
|
||||
// expandWikilinks rewrites [[target]] to a markdown link with a wiki: URL;
|
||||
// the frontend resolves the target against the file tree by basename.
|
||||
func expandWikilinks(src []byte) []byte {
|
||||
return wikiRe.ReplaceAllFunc(src, func(m []byte) []byte {
|
||||
g := wikiRe.FindSubmatch(m)
|
||||
target, label := g[1], g[2]
|
||||
if len(label) == 0 {
|
||||
label = target
|
||||
}
|
||||
return []byte("[" + string(label) + "](wiki:" + url.PathEscape(string(target)) + ")")
|
||||
})
|
||||
}
|
||||
|
||||
// RenderMarkdown converts markdown to HTML (GFM + wikilinks). Raw HTML in
|
||||
// the source is escaped by goldmark's safe default.
|
||||
func RenderMarkdown(src []byte) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := md.Convert(expandWikilinks(src), &buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
// Package webapp serves a read-only web view of an sfs remote: the volume's
|
||||
// file tree reconstructed from the journals, rendered markdown, and file
|
||||
// downloads. It talks straight to the object store — no local volume state,
|
||||
// mount, or daemon is needed.
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"maps"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/runbear-io/sfs/internal/journal"
|
||||
"github.com/runbear-io/sfs/internal/remote"
|
||||
)
|
||||
|
||||
//go:embed static
|
||||
var staticFiles embed.FS
|
||||
|
||||
// Source supplies the file set and content the server renders. Two
|
||||
// implementations: RemoteSource (an sfs remote, the normal mode) and
|
||||
// DirSource (a plain local folder, for debugging without any remote).
|
||||
type Source interface {
|
||||
Files(ctx context.Context) (map[string]FileInfo, error)
|
||||
Open(ctx context.Context, path string, fi FileInfo) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// Server renders one source as a website. File listings are cached for
|
||||
// Refresh between fetches; if the source becomes unreachable, the last good
|
||||
// snapshot keeps being served.
|
||||
type Server struct {
|
||||
Source Source
|
||||
Remote string // display only
|
||||
Volume string // display only
|
||||
Refresh time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
snap *snapshot
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// FileInfo is the resolved state of one path: content identity (Blob doubles
|
||||
// as the ETag), plus provenance where the source knows it.
|
||||
type FileInfo struct {
|
||||
Blob string
|
||||
Size int64
|
||||
Time time.Time
|
||||
Author string
|
||||
Device string
|
||||
}
|
||||
|
||||
type snapshot struct {
|
||||
files map[string]FileInfo
|
||||
}
|
||||
|
||||
func (s *Server) snapshot(ctx context.Context) (*snapshot, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.snap != nil && time.Since(s.at) < s.Refresh {
|
||||
return s.snap, nil
|
||||
}
|
||||
files, err := s.Source.Files(ctx)
|
||||
if err != nil {
|
||||
if s.snap != nil {
|
||||
return s.snap, nil // serve stale rather than fail
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
s.snap, s.at = &snapshot{files: files}, time.Now()
|
||||
return s.snap, nil
|
||||
}
|
||||
|
||||
// RemoteSource reads an sfs remote: it fetches every journal and folds the
|
||||
// ops into the current volume state (same total order as journal.Replay,
|
||||
// but keeping author/device/time of the winning op per path).
|
||||
type RemoteSource struct {
|
||||
Backend remote.Backend
|
||||
}
|
||||
|
||||
func (r *RemoteSource) Files(ctx context.Context) (map[string]FileInfo, error) {
|
||||
objs, err := r.Backend.List(ctx, "journal/")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list journals: %w", err)
|
||||
}
|
||||
var all []journal.Op
|
||||
for _, o := range objs {
|
||||
if !strings.HasSuffix(o.Key, ".jsonl") {
|
||||
continue
|
||||
}
|
||||
rc, err := r.Backend.Get(ctx, o.Key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch %s: %w", o.Key, err)
|
||||
}
|
||||
data, err := io.ReadAll(rc)
|
||||
rc.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ops, err := journal.Parse(data)
|
||||
if err != nil {
|
||||
continue // corrupt journal; ignore rather than break the view
|
||||
}
|
||||
all = append(all, ops...)
|
||||
}
|
||||
journal.Sort(all)
|
||||
files := make(map[string]FileInfo)
|
||||
for _, op := range all {
|
||||
switch op.Kind {
|
||||
case journal.KindPut:
|
||||
files[op.Path] = FileInfo{
|
||||
Blob: op.Blob, Size: op.Size, Time: op.Time,
|
||||
Author: op.Author, Device: op.DeviceName,
|
||||
}
|
||||
case journal.KindDelete:
|
||||
delete(files, op.Path)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (r *RemoteSource) Open(ctx context.Context, _ string, fi FileInfo) (io.ReadCloser, error) {
|
||||
return r.Backend.Get(ctx, "blobs/"+fi.Blob)
|
||||
}
|
||||
|
||||
// Handler returns the HTTP handler: /api/* plus the embedded frontend.
|
||||
func (s *Server) Handler() http.Handler {
|
||||
static, err := fs.Sub(staticFiles, "static")
|
||||
if err != nil {
|
||||
panic(err) // embedded FS; cannot fail at runtime
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /api/volume", s.handleVolume)
|
||||
mux.HandleFunc("GET /api/tree", s.handleTree)
|
||||
mux.HandleFunc("GET /api/file", s.handleFile)
|
||||
mux.HandleFunc("GET /api/download", s.handleDownload)
|
||||
mux.HandleFunc("GET /api/render", s.handleRender)
|
||||
mux.Handle("GET /", http.FileServerFS(static))
|
||||
return mux
|
||||
}
|
||||
|
||||
func (s *Server) handleVolume(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]string{"volume": s.Volume, "remote": s.Remote})
|
||||
}
|
||||
|
||||
// Node is one entry of the file tree returned by /api/tree.
|
||||
type Node struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Dir bool `json:"dir"`
|
||||
Size int64 `json:"size,omitempty"`
|
||||
Time time.Time `json:"time,omitzero"`
|
||||
Author string `json:"author,omitempty"`
|
||||
Device string `json:"device,omitempty"`
|
||||
Children []*Node `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleTree(w http.ResponseWriter, r *http.Request) {
|
||||
snap, err := s.snapshot(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
writeJSON(w, buildTree(snap.files))
|
||||
}
|
||||
|
||||
func buildTree(files map[string]FileInfo) *Node {
|
||||
root := &Node{Name: "/", Dir: true}
|
||||
dirs := map[string]*Node{"": root}
|
||||
for _, p := range slices.Sorted(maps.Keys(files)) {
|
||||
fi := files[p]
|
||||
parent := root
|
||||
segs := strings.Split(p, "/")
|
||||
for i := 0; i < len(segs)-1; i++ {
|
||||
dp := strings.Join(segs[:i+1], "/")
|
||||
n, ok := dirs[dp]
|
||||
if !ok {
|
||||
n = &Node{Name: segs[i], Path: dp, Dir: true}
|
||||
dirs[dp] = n
|
||||
parent.Children = append(parent.Children, n)
|
||||
}
|
||||
parent = n
|
||||
}
|
||||
parent.Children = append(parent.Children, &Node{
|
||||
Name: segs[len(segs)-1], Path: p,
|
||||
Size: fi.Size, Time: fi.Time, Author: fi.Author, Device: fi.Device,
|
||||
})
|
||||
}
|
||||
sortTree(root)
|
||||
return root
|
||||
}
|
||||
|
||||
func sortTree(n *Node) {
|
||||
sort.SliceStable(n.Children, func(i, j int) bool {
|
||||
a, b := n.Children[i], n.Children[j]
|
||||
if a.Dir != b.Dir {
|
||||
return a.Dir // folders first, like Obsidian
|
||||
}
|
||||
return strings.ToLower(a.Name) < strings.ToLower(b.Name)
|
||||
})
|
||||
for _, c := range n.Children {
|
||||
if c.Dir {
|
||||
sortTree(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// lookup resolves ?path= against the current snapshot.
|
||||
func (s *Server) lookup(r *http.Request) (string, FileInfo, int, error) {
|
||||
p := r.URL.Query().Get("path")
|
||||
if p == "" {
|
||||
return "", FileInfo{}, http.StatusBadRequest, fmt.Errorf("missing ?path=")
|
||||
}
|
||||
snap, err := s.snapshot(r.Context())
|
||||
if err != nil {
|
||||
return "", FileInfo{}, http.StatusBadGateway, err
|
||||
}
|
||||
fi, ok := snap.files[p]
|
||||
if !ok {
|
||||
return "", FileInfo{}, http.StatusNotFound, fmt.Errorf("no such file: %s", p)
|
||||
}
|
||||
return p, fi, 0, nil
|
||||
}
|
||||
|
||||
func (s *Server) serveBlob(w http.ResponseWriter, r *http.Request, attach bool) {
|
||||
p, fi, code, err := s.lookup(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), code)
|
||||
return
|
||||
}
|
||||
etag := `"` + fi.Blob + `"`
|
||||
if r.Header.Get("If-None-Match") == etag {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
rc, err := s.Source.Open(r.Context(), p, fi)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("fetch content: %v", err), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
w.Header().Set("ETag", etag)
|
||||
w.Header().Set("Content-Type", contentType(p))
|
||||
w.Header().Set("Content-Length", fmt.Sprint(fi.Size))
|
||||
if attach {
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", path.Base(p)))
|
||||
}
|
||||
io.Copy(w, rc)
|
||||
}
|
||||
|
||||
func (s *Server) handleFile(w http.ResponseWriter, r *http.Request) {
|
||||
s.serveBlob(w, r, false)
|
||||
}
|
||||
|
||||
func (s *Server) handleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
s.serveBlob(w, r, true)
|
||||
}
|
||||
|
||||
func (s *Server) handleRender(w http.ResponseWriter, r *http.Request) {
|
||||
p, fi, code, err := s.lookup(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), code)
|
||||
return
|
||||
}
|
||||
rc, err := s.Source.Open(r.Context(), p, fi)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("fetch content: %v", err), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
src, err := io.ReadAll(rc)
|
||||
rc.Close()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
html, err := RenderMarkdown(src)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("render: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{
|
||||
"path": p, "html": html,
|
||||
"size": fi.Size, "time": fi.Time, "author": fi.Author, "device": fi.Device,
|
||||
})
|
||||
}
|
||||
|
||||
func contentType(p string) string {
|
||||
switch strings.ToLower(path.Ext(p)) {
|
||||
case ".md", ".markdown":
|
||||
return "text/markdown; charset=utf-8"
|
||||
case ".txt", ".log", ".go", ".py", ".js", ".ts", ".sh", ".yaml", ".yml", ".toml", ".csv":
|
||||
return "text/plain; charset=utf-8"
|
||||
case ".json":
|
||||
return "application/json"
|
||||
}
|
||||
if t := mime.TypeByExtension(path.Ext(p)); t != "" {
|
||||
return t
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/runbear-io/sfs/internal/journal"
|
||||
"github.com/runbear-io/sfs/internal/remote"
|
||||
)
|
||||
|
||||
// fakeRemote builds an sfs remote layout (journal/<dev>.jsonl + blobs/<sha>)
|
||||
// in a temp dir and returns a Server over it.
|
||||
type fakeRemote struct {
|
||||
t *testing.T
|
||||
dir string
|
||||
seq map[string]int64
|
||||
lam int64
|
||||
}
|
||||
|
||||
func newFakeRemote(t *testing.T) *fakeRemote {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
for _, d := range []string{"journal", "blobs"} {
|
||||
if err := os.MkdirAll(filepath.Join(dir, d), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return &fakeRemote{t: t, dir: dir, seq: map[string]int64{}}
|
||||
}
|
||||
|
||||
func (f *fakeRemote) put(dev, path, content string) {
|
||||
f.t.Helper()
|
||||
sum := sha256.Sum256([]byte(content))
|
||||
blob := hex.EncodeToString(sum[:])
|
||||
if err := os.WriteFile(filepath.Join(f.dir, "blobs", blob), []byte(content), 0o644); err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
f.append(dev, journal.Op{
|
||||
Kind: journal.KindPut, Path: path,
|
||||
Blob: blob, Size: int64(len(content)), Mode: 0o644,
|
||||
})
|
||||
}
|
||||
|
||||
func (f *fakeRemote) del(dev, path string) {
|
||||
f.t.Helper()
|
||||
f.append(dev, journal.Op{Kind: journal.KindDelete, Path: path})
|
||||
}
|
||||
|
||||
func (f *fakeRemote) append(dev string, op journal.Op) {
|
||||
f.t.Helper()
|
||||
f.lam++
|
||||
f.seq[dev]++
|
||||
op.Seq, op.Lamport = f.seq[dev], f.lam
|
||||
op.Time = time.Now().UTC()
|
||||
op.Device, op.DeviceName, op.Author = dev, dev, dev+"@test"
|
||||
if err := journal.Append(filepath.Join(f.dir, "journal", dev+".jsonl"), []journal.Op{op}); err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fakeRemote) server() *Server {
|
||||
f.t.Helper()
|
||||
be, err := remote.Open(context.Background(), "file://"+f.dir)
|
||||
if err != nil {
|
||||
f.t.Fatal(err)
|
||||
}
|
||||
f.t.Cleanup(func() { be.Close() })
|
||||
return &Server{Source: &RemoteSource{Backend: be}, Remote: "file://" + f.dir, Volume: "testvol", Refresh: 0}
|
||||
}
|
||||
|
||||
func get(t *testing.T, h http.Handler, url string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest("GET", url, nil))
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestTreeAndFile(t *testing.T) {
|
||||
f := newFakeRemote(t)
|
||||
f.put("deva", "readme.md", "# Hello")
|
||||
f.put("deva", "notes/plan.md", "- step one")
|
||||
f.put("devb", "notes/img.png", "not-really-a-png")
|
||||
h := f.server().Handler()
|
||||
|
||||
rec := get(t, h, "/api/tree")
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("tree: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
var root Node
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(root.Children) != 2 {
|
||||
t.Fatalf("root children = %d, want 2 (notes/, readme.md)", len(root.Children))
|
||||
}
|
||||
if !root.Children[0].Dir || root.Children[0].Name != "notes" {
|
||||
t.Fatalf("first child = %+v, want dir notes (folders first)", root.Children[0])
|
||||
}
|
||||
if got := len(root.Children[0].Children); got != 2 {
|
||||
t.Fatalf("notes/ children = %d, want 2", got)
|
||||
}
|
||||
|
||||
rec = get(t, h, "/api/file?path=readme.md")
|
||||
if rec.Code != 200 || rec.Body.String() != "# Hello" {
|
||||
t.Fatalf("file: %d %q", rec.Code, rec.Body)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/markdown") {
|
||||
t.Fatalf("content-type = %q", ct)
|
||||
}
|
||||
etag := rec.Header().Get("ETag")
|
||||
if etag == "" {
|
||||
t.Fatal("no ETag")
|
||||
}
|
||||
req := httptest.NewRequest("GET", "/api/file?path=readme.md", nil)
|
||||
req.Header.Set("If-None-Match", etag)
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNotModified {
|
||||
t.Fatalf("etag revalidate: %d, want 304", rec.Code)
|
||||
}
|
||||
|
||||
if rec := get(t, h, "/api/file?path=nope.md"); rec.Code != 404 {
|
||||
t.Fatalf("missing file: %d, want 404", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteHidesFile(t *testing.T) {
|
||||
f := newFakeRemote(t)
|
||||
f.put("deva", "a.md", "a")
|
||||
f.put("deva", "b.md", "b")
|
||||
f.del("devb", "a.md")
|
||||
h := f.server().Handler()
|
||||
|
||||
var root Node
|
||||
if err := json.Unmarshal(get(t, h, "/api/tree").Body.Bytes(), &root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(root.Children) != 1 || root.Children[0].Name != "b.md" {
|
||||
t.Fatalf("tree after delete = %+v, want only b.md", root.Children)
|
||||
}
|
||||
if rec := get(t, h, "/api/file?path=a.md"); rec.Code != 404 {
|
||||
t.Fatalf("deleted file: %d, want 404", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown(t *testing.T) {
|
||||
f := newFakeRemote(t)
|
||||
f.put("deva", "doc.md", "# Title\n\nsee [[plan]] and [[plan|the plan]]\n\n<script>x</script>")
|
||||
h := f.server().Handler()
|
||||
|
||||
rec := get(t, h, "/api/render?path=doc.md")
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("render: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
var doc struct {
|
||||
HTML string `json:"html"`
|
||||
Author string `json:"author"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"<h1", "Title", `href="wiki:plan"`, ">the plan</a>"} {
|
||||
if !strings.Contains(doc.HTML, want) {
|
||||
t.Errorf("html missing %q:\n%s", want, doc.HTML)
|
||||
}
|
||||
}
|
||||
if strings.Contains(doc.HTML, "<script>") {
|
||||
t.Errorf("raw HTML not escaped:\n%s", doc.HTML)
|
||||
}
|
||||
if doc.Author != "deva@test" {
|
||||
t.Errorf("author = %q", doc.Author)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownload(t *testing.T) {
|
||||
f := newFakeRemote(t)
|
||||
f.put("deva", "notes/plan.md", "content")
|
||||
h := f.server().Handler()
|
||||
|
||||
rec := get(t, h, "/api/download?path=notes/plan.md")
|
||||
if rec.Code != 200 || rec.Body.String() != "content" {
|
||||
t.Fatalf("download: %d %q", rec.Code, rec.Body)
|
||||
}
|
||||
if cd := rec.Header().Get("Content-Disposition"); cd != `attachment; filename="plan.md"` {
|
||||
t.Fatalf("content-disposition = %q", cd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrontendServed(t *testing.T) {
|
||||
f := newFakeRemote(t)
|
||||
h := f.server().Handler()
|
||||
rec := get(t, h, "/")
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "<title>sfs</title>") {
|
||||
t.Fatalf("index: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandWikilinks(t *testing.T) {
|
||||
got := string(expandWikilinks([]byte("a [[x y]] b [[u|v]] c [[no")))
|
||||
want := "a [x y](wiki:x%20y) b [v](wiki:u) c [[no"
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/* sfs web viewer: file tree + obsidian-like markdown pane. No dependencies. */
|
||||
"use strict";
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
let flatFiles = []; // [{path, name}] for wikilink resolution
|
||||
let currentPath = null;
|
||||
let collapsed = new Set(); // dir paths the user collapsed
|
||||
|
||||
const MD_EXT = /\.(md|markdown)$/i;
|
||||
const IMG_EXT = /\.(png|jpe?g|gif|svg|webp|ico|bmp|avif)$/i;
|
||||
const TEXT_EXT = /\.(txt|log|json|ya?ml|toml|csv|go|py|js|ts|jsx|tsx|sh|bash|zsh|rb|rs|c|h|cpp|java|kt|swift|sql|html|css|xml|ini|conf|env|mod|sum|jsonl)$/i;
|
||||
|
||||
const fileURL = (p) => "api/file?path=" + encodeURIComponent(p);
|
||||
|
||||
async function getJSON(url) {
|
||||
const r = await fetch(url);
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
return r.json();
|
||||
}
|
||||
|
||||
/* ---- boot ---- */
|
||||
async function boot() {
|
||||
try {
|
||||
const v = await getJSON("api/volume");
|
||||
$("vault-name").textContent = v.volume || "sfs";
|
||||
$("vault-remote").textContent = v.remote || "";
|
||||
document.title = (v.volume || "sfs") + " — sfs";
|
||||
} catch { /* non-fatal */ }
|
||||
await refreshTree();
|
||||
const p = decodeURIComponent(location.hash.slice(1));
|
||||
if (p) openFile(p);
|
||||
setInterval(refreshTree, 15000); // pick up synced changes
|
||||
}
|
||||
|
||||
/* ---- tree ---- */
|
||||
async function refreshTree() {
|
||||
let root;
|
||||
try {
|
||||
root = await getJSON("api/tree");
|
||||
} catch { return; } // keep the last good tree
|
||||
flatFiles = [];
|
||||
const nav = $("tree");
|
||||
nav.innerHTML = "";
|
||||
nav.appendChild(renderChildren(root.children || []));
|
||||
markActive();
|
||||
}
|
||||
|
||||
function renderChildren(children) {
|
||||
const ul = document.createElement("ul");
|
||||
for (const n of children) ul.appendChild(renderNode(n));
|
||||
return ul;
|
||||
}
|
||||
|
||||
function renderNode(n) {
|
||||
const li = document.createElement("li");
|
||||
li.className = n.dir ? "dir" : "file";
|
||||
const row = document.createElement("div");
|
||||
row.className = "row";
|
||||
row.dataset.path = n.path;
|
||||
const chev = document.createElement("span");
|
||||
chev.className = "chev";
|
||||
chev.textContent = "▾"; // ▾
|
||||
const label = document.createElement("span");
|
||||
label.textContent = n.name;
|
||||
row.append(chev, label);
|
||||
li.appendChild(row);
|
||||
if (n.dir) {
|
||||
li.appendChild(renderChildren(n.children || []));
|
||||
if (collapsed.has(n.path)) li.classList.add("collapsed");
|
||||
row.onclick = () => {
|
||||
li.classList.toggle("collapsed");
|
||||
li.classList.contains("collapsed") ? collapsed.add(n.path) : collapsed.delete(n.path);
|
||||
};
|
||||
} else {
|
||||
flatFiles.push({ path: n.path, name: n.name });
|
||||
row.onclick = () => openFile(n.path);
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
function markActive() {
|
||||
for (const el of document.querySelectorAll("#tree .row.active")) el.classList.remove("active");
|
||||
if (!currentPath) return;
|
||||
const row = document.querySelector(`#tree .row[data-path="${CSS.escape(currentPath)}"]`);
|
||||
if (row) row.classList.add("active");
|
||||
}
|
||||
|
||||
/* ---- file pane ---- */
|
||||
async function openFile(p) {
|
||||
currentPath = p;
|
||||
location.hash = encodeURIComponent(p);
|
||||
markActive();
|
||||
$("crumb").textContent = p.split("/").join(" / ");
|
||||
const dl = $("download");
|
||||
dl.href = "api/download?path=" + encodeURIComponent(p);
|
||||
dl.hidden = false;
|
||||
const content = $("content");
|
||||
content.innerHTML = "";
|
||||
$("meta").textContent = "";
|
||||
try {
|
||||
if (MD_EXT.test(p)) {
|
||||
const doc = await getJSON("api/render?path=" + encodeURIComponent(p));
|
||||
content.innerHTML = doc.html;
|
||||
fixLinks(content, p);
|
||||
showMeta(doc);
|
||||
} else if (IMG_EXT.test(p)) {
|
||||
const img = document.createElement("img");
|
||||
img.src = fileURL(p);
|
||||
img.alt = p;
|
||||
content.appendChild(img);
|
||||
} else if (TEXT_EXT.test(p)) {
|
||||
const r = await fetch(fileURL(p));
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
const pre = document.createElement("pre");
|
||||
pre.className = "plain";
|
||||
pre.textContent = await r.text();
|
||||
content.appendChild(pre);
|
||||
} else {
|
||||
content.innerHTML =
|
||||
`<div class="filecard"><div class="name"></div>` +
|
||||
`<p>No preview for this file type.</p>` +
|
||||
`<a class="btn" download href="${dl.href}">Download</a></div>`;
|
||||
content.querySelector(".name").textContent = p.split("/").pop();
|
||||
}
|
||||
} catch (err) {
|
||||
content.innerHTML = `<div class="empty"></div>`;
|
||||
content.querySelector(".empty").textContent = "Could not load file: " + err.message;
|
||||
}
|
||||
}
|
||||
|
||||
function showMeta(doc) {
|
||||
const parts = [];
|
||||
if (doc.author) parts.push(doc.author + (doc.device ? " on " + doc.device : ""));
|
||||
if (doc.time) parts.push(new Date(doc.time).toLocaleString());
|
||||
$("meta").textContent = parts.join(" · ");
|
||||
}
|
||||
|
||||
/* Rewrite rendered-markdown links: wiki: targets resolve by basename, and
|
||||
relative links/images resolve against the current file's folder. */
|
||||
function fixLinks(scope, p) {
|
||||
const dir = p.includes("/") ? p.slice(0, p.lastIndexOf("/")) : "";
|
||||
for (const img of scope.querySelectorAll("img")) {
|
||||
const src = img.getAttribute("src") || "";
|
||||
if (!/^([a-z]+:|\/)/i.test(src)) img.src = fileURL(join(dir, src));
|
||||
}
|
||||
for (const a of scope.querySelectorAll("a")) {
|
||||
const href = a.getAttribute("href") || "";
|
||||
if (href.startsWith("wiki:")) {
|
||||
const target = decodeURIComponent(href.slice(5));
|
||||
a.onclick = (e) => { e.preventDefault(); openWikilink(target); };
|
||||
} else if (!/^([a-z]+:|\/|#)/i.test(href)) {
|
||||
const target = join(dir, decodeURIComponent(href));
|
||||
a.onclick = (e) => { e.preventDefault(); openFile(target); };
|
||||
} else if (/^https?:/i.test(href)) {
|
||||
a.target = "_blank";
|
||||
a.rel = "noopener";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function openWikilink(target) {
|
||||
const want = target.toLowerCase();
|
||||
const hit =
|
||||
flatFiles.find((f) => f.path.toLowerCase() === want || f.path.toLowerCase() === want + ".md") ||
|
||||
flatFiles.find((f) => {
|
||||
const n = f.name.toLowerCase();
|
||||
return n === want || n === want + ".md";
|
||||
});
|
||||
if (hit) openFile(hit.path);
|
||||
}
|
||||
|
||||
function join(dir, rel) {
|
||||
const parts = (dir ? dir.split("/") : []).concat(rel.split("/"));
|
||||
const out = [];
|
||||
for (const s of parts) {
|
||||
if (s === "" || s === ".") continue;
|
||||
if (s === "..") out.pop();
|
||||
else out.push(s);
|
||||
}
|
||||
return out.join("/");
|
||||
}
|
||||
|
||||
window.addEventListener("hashchange", () => {
|
||||
const p = decodeURIComponent(location.hash.slice(1));
|
||||
if (p && p !== currentPath) openFile(p);
|
||||
});
|
||||
|
||||
boot();
|
||||
@@ -0,0 +1,30 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>sfs</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>📁</text></svg>">
|
||||
</head>
|
||||
<body>
|
||||
<aside id="sidebar">
|
||||
<header id="vault">
|
||||
<span id="vault-name">…</span>
|
||||
<span id="vault-remote"></span>
|
||||
</header>
|
||||
<nav id="tree" aria-label="Files"></nav>
|
||||
</aside>
|
||||
<main id="main">
|
||||
<header id="topbar">
|
||||
<span id="crumb"></span>
|
||||
<span id="meta"></span>
|
||||
<a id="download" class="btn" hidden download>Download</a>
|
||||
</header>
|
||||
<article id="content" class="markdown">
|
||||
<div class="empty">Select a file from the sidebar</div>
|
||||
</article>
|
||||
</main>
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,149 @@
|
||||
/* Obsidian-inspired dark theme */
|
||||
:root {
|
||||
--bg: #1e1e1e;
|
||||
--bg-side: #262626;
|
||||
--bg-hover: #333333;
|
||||
--bg-active: #3f3550;
|
||||
--border: #363636;
|
||||
--text: #dadada;
|
||||
--text-dim: #9a9a9a;
|
||||
--text-faint: #6e6e6e;
|
||||
--accent: #a882ff;
|
||||
--accent-dim: #7c5cd6;
|
||||
--code-bg: #2a2a2a;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body { height: 100%; margin: 0; }
|
||||
body {
|
||||
display: flex;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
/* ---- sidebar ---- */
|
||||
#sidebar {
|
||||
width: 280px;
|
||||
min-width: 200px;
|
||||
background: var(--bg-side);
|
||||
border-right: 1px solid var(--border);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
#vault {
|
||||
padding: 14px 16px 10px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
#vault-name { display: block; font-weight: 600; font-size: 14px; }
|
||||
#vault-remote {
|
||||
display: block;
|
||||
font-size: 11px;
|
||||
color: var(--text-faint);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
#tree { flex: 1; overflow-y: auto; padding: 8px 6px 24px; font-size: 13.5px; }
|
||||
#tree ul { list-style: none; margin: 0; padding-left: 14px; }
|
||||
#tree > ul { padding-left: 4px; }
|
||||
#tree li > .row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
padding: 2px 8px;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
color: var(--text-dim);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
#tree li > .row:hover { background: var(--bg-hover); color: var(--text); }
|
||||
#tree li > .row.active { background: var(--bg-active); color: var(--accent); }
|
||||
#tree .chev { width: 12px; flex: none; font-size: 10px; color: var(--text-faint); transition: transform .12s; }
|
||||
#tree li.collapsed > ul { display: none; }
|
||||
#tree li.collapsed > .row .chev { transform: rotate(-90deg); }
|
||||
#tree .file .chev { visibility: hidden; }
|
||||
|
||||
/* ---- main pane ---- */
|
||||
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
|
||||
#topbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 10px 24px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
min-height: 46px;
|
||||
}
|
||||
#crumb { font-size: 13px; color: var(--text-dim); }
|
||||
#meta { flex: 1; font-size: 12px; color: var(--text-faint); text-align: right; }
|
||||
.btn {
|
||||
flex: none;
|
||||
padding: 4px 12px;
|
||||
border-radius: 6px;
|
||||
background: var(--accent-dim);
|
||||
color: #fff;
|
||||
font-size: 12.5px;
|
||||
text-decoration: none;
|
||||
}
|
||||
.btn:hover { background: var(--accent); }
|
||||
#content { flex: 1; overflow-y: auto; padding: 28px 48px 80px; }
|
||||
.empty { color: var(--text-faint); text-align: center; margin-top: 20vh; }
|
||||
|
||||
/* ---- markdown ---- */
|
||||
.markdown { max-width: 820px; }
|
||||
.markdown h1, .markdown h2, .markdown h3, .markdown h4 {
|
||||
color: #f0f0f0;
|
||||
line-height: 1.3;
|
||||
margin: 1.4em 0 .5em;
|
||||
}
|
||||
.markdown h1:first-child { margin-top: 0; }
|
||||
.markdown h1 { font-size: 1.7em; }
|
||||
.markdown h2 { font-size: 1.35em; border-bottom: 1px solid var(--border); padding-bottom: .25em; }
|
||||
.markdown a { color: var(--accent); text-decoration: none; }
|
||||
.markdown a:hover { text-decoration: underline; }
|
||||
.markdown code {
|
||||
background: var(--code-bg);
|
||||
padding: .15em .4em;
|
||||
border-radius: 4px;
|
||||
font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
}
|
||||
.markdown pre {
|
||||
background: var(--code-bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 14px 16px;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.markdown pre code { background: none; padding: 0; }
|
||||
.markdown blockquote {
|
||||
margin: 1em 0;
|
||||
padding: .1em 1em;
|
||||
border-left: 3px solid var(--accent-dim);
|
||||
color: var(--text-dim);
|
||||
}
|
||||
.markdown table { border-collapse: collapse; margin: 1em 0; }
|
||||
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 12px; }
|
||||
.markdown th { background: var(--bg-side); }
|
||||
.markdown img { max-width: 100%; border-radius: 6px; }
|
||||
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
|
||||
.markdown input[type="checkbox"] { accent-color: var(--accent); }
|
||||
|
||||
/* plain file / binary views */
|
||||
pre.plain {
|
||||
background: var(--code-bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 14px 16px;
|
||||
overflow-x: auto;
|
||||
font: 12.5px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
.filecard {
|
||||
margin-top: 15vh;
|
||||
text-align: center;
|
||||
color: var(--text-dim);
|
||||
}
|
||||
.filecard .name { font-size: 1.2em; color: var(--text); margin-bottom: .3em; }
|
||||
Reference in New Issue
Block a user