fs: just some renames.

This commit is contained in:
fiatjaf
2026-01-16 12:18:32 -03:00
parent 0e283368ed
commit 6dfbed4413
2 changed files with 147 additions and 152 deletions

View File

@@ -1,16 +0,0 @@
package nostrfs
import (
"fiatjaf.com/nostr"
)
func kindToExtension(kind nostr.Kind) string {
switch kind {
case 30023:
return "md"
case 30818:
return "adoc"
default:
return "txt"
}
}

View File

@@ -17,27 +17,27 @@ import (
"github.com/winfsp/cgofuse/fuse" "github.com/winfsp/cgofuse/fuse"
) )
type Options struct { type FSOptions struct {
AutoPublishNotesTimeout time.Duration AutoPublishNotesTimeout time.Duration
AutoPublishArticlesTimeout time.Duration AutoPublishArticlesTimeout time.Duration
} }
type NostrRoot struct { type FSRoot struct {
fuse.FileSystemBase fuse.FileSystemBase
ctx context.Context ctx context.Context
sys *sdk.System sys *sdk.System
rootPubKey nostr.PubKey rootPubKey nostr.PubKey
signer nostr.Signer signer nostr.Signer
opts Options opts FSOptions
mountpoint string mountpoint string
mu sync.RWMutex mu sync.RWMutex
nodes map[string]*Node // path -> node nodes map[string]*FSNode // path -> node
nextIno uint64 nextIno uint64
pendingNotes map[string]*time.Timer // path -> auto-publish timer pendingNotes map[string]*time.Timer // path -> auto-publish timer
} }
type Node struct { type FSNode struct {
ino uint64 ino uint64
path string path string
name string name string
@@ -46,14 +46,14 @@ type Node struct {
mode uint32 mode uint32
mtime time.Time mtime time.Time
data []byte data []byte
children map[string]*Node children map[string]*FSNode
loadFunc func() ([]byte, error) // for lazy loading loadFunc func() ([]byte, error) // for lazy loading
loaded bool loaded bool
} }
var _ fuse.FileSystemInterface = (*NostrRoot)(nil) var _ fuse.FileSystemInterface = (*FSRoot)(nil)
func NewNostrRoot(ctx context.Context, sys interface{}, user interface{}, mountpoint string, o Options) *NostrRoot { func NewFSRoot(ctx context.Context, sys interface{}, user interface{}, mountpoint string, o FSOptions) *FSRoot {
var system *sdk.System var system *sdk.System
if sys != nil { if sys != nil {
system = sys.(*sdk.System) system = sys.(*sdk.System)
@@ -71,37 +71,37 @@ func NewNostrRoot(ctx context.Context, sys interface{}, user interface{}, mountp
abs, _ := filepath.Abs(mountpoint) abs, _ := filepath.Abs(mountpoint)
root := &NostrRoot{ root := &FSRoot{
ctx: ctx, ctx: ctx,
sys: system, sys: system,
rootPubKey: pubkey, rootPubKey: pubkey,
signer: signer, signer: signer,
opts: o, opts: o,
mountpoint: abs, mountpoint: abs,
nodes: make(map[string]*Node), nodes: make(map[string]*FSNode),
nextIno: 2, // 1 is reserved for root nextIno: 2, // 1 is reserved for root
pendingNotes: make(map[string]*time.Timer), pendingNotes: make(map[string]*time.Timer),
} }
// Initialize root directory // initialize root directory
rootNode := &Node{ rootNode := &FSNode{
ino: 1, ino: 1,
path: "/", path: "/",
name: "", name: "",
isDir: true, isDir: true,
mode: fuse.S_IFDIR | 0755, mode: fuse.S_IFDIR | 0755,
mtime: time.Now(), mtime: time.Now(),
children: make(map[string]*Node), children: make(map[string]*FSNode),
} }
root.nodes["/"] = rootNode root.nodes["/"] = rootNode
// Start async initialization // start async initialization
go root.initialize() go root.initialize()
return root return root
} }
func (r *NostrRoot) initialize() { func (r *FSRoot) initialize() {
if r.rootPubKey == nostr.ZeroPK { if r.rootPubKey == nostr.ZeroPK {
return return
} }
@@ -109,14 +109,14 @@ func (r *NostrRoot) initialize() {
log := r.getLog() log := r.getLog()
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
// Fetch follow list // fetch follow list
fl := r.sys.FetchFollowList(r.ctx, r.rootPubKey) fl := r.sys.FetchFollowList(r.ctx, r.rootPubKey)
log("- fetched %d contacts\n", len(fl.Items)) log("- fetched %d contacts\n", len(fl.Items))
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Add our contacts // add our contacts
for _, f := range fl.Items { for _, f := range fl.Items {
npub := nip19.EncodeNpub(f.Pubkey) npub := nip19.EncodeNpub(f.Pubkey)
if _, exists := r.nodes["/"+npub]; !exists { if _, exists := r.nodes["/"+npub]; !exists {
@@ -124,14 +124,14 @@ func (r *NostrRoot) initialize() {
} }
} }
// Add ourselves // add ourselves
npub := nip19.EncodeNpub(r.rootPubKey) npub := nip19.EncodeNpub(r.rootPubKey)
if _, exists := r.nodes["/"+npub]; !exists { if _, exists := r.nodes["/"+npub]; !exists {
r.createNpubDirLocked(npub, r.rootPubKey, r.signer) r.createNpubDirLocked(npub, r.rootPubKey, r.signer)
} }
// Add @me symlink (for now, just create a text file pointing to our npub) // add @me symlink (for now, just create a text file pointing to our npub)
meNode := &Node{ meNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: "/@me", path: "/@me",
name: "@me", name: "@me",
@@ -146,19 +146,19 @@ func (r *NostrRoot) initialize() {
r.nodes["/"].children["@me"] = meNode r.nodes["/"].children["@me"] = meNode
} }
func (r *NostrRoot) fetchMetadata(dirPath string, pubkey nostr.PubKey) { func (r *FSRoot) fetchMetadata(dirPath string, pubkey nostr.PubKey) {
pm := r.sys.FetchProfileMetadata(r.ctx, pubkey) pm := r.sys.FetchProfileMetadata(r.ctx, pubkey)
if pm.Event == nil { if pm.Event == nil {
return return
} }
// Use the content field which contains the actual profile JSON // use the content field which contains the actual profile JSON
metadataJ := []byte(pm.Event.Content) metadataJ := []byte(pm.Event.Content)
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
metadataNode := &Node{ metadataNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath + "/metadata.json", path: dirPath + "/metadata.json",
name: "metadata.json", name: "metadata.json",
@@ -175,13 +175,13 @@ func (r *NostrRoot) fetchMetadata(dirPath string, pubkey nostr.PubKey) {
} }
} }
func (r *NostrRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) { func (r *FSRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) {
pm := r.sys.FetchProfileMetadata(r.ctx, pubkey) pm := r.sys.FetchProfileMetadata(r.ctx, pubkey)
if pm.Event == nil || pm.Picture == "" { if pm.Event == nil || pm.Picture == "" {
return return
} }
// Download picture // download picture
ctx, cancel := context.WithTimeout(r.ctx, time.Second*20) ctx, cancel := context.WithTimeout(r.ctx, time.Second*20)
defer cancel() defer cancel()
@@ -200,7 +200,7 @@ func (r *NostrRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) {
return return
} }
// Read image data // read image data
imageData := make([]byte, 0, 1024*1024) // 1MB initial capacity imageData := make([]byte, 0, 1024*1024) // 1MB initial capacity
buf := make([]byte, 32*1024) buf := make([]byte, 32*1024)
for { for {
@@ -220,7 +220,7 @@ func (r *NostrRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) {
return return
} }
// Detect file extension from content-type or URL // detect file extension from content-type or URL
ext := "png" ext := "png"
if ct := resp.Header.Get("Content-Type"); ct != "" { if ct := resp.Header.Get("Content-Type"); ct != "" {
switch ct { switch ct {
@@ -239,7 +239,7 @@ func (r *NostrRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) {
defer r.mu.Unlock() defer r.mu.Unlock()
picturePath := dirPath + "/picture." + ext picturePath := dirPath + "/picture." + ext
pictureNode := &Node{ pictureNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: picturePath, path: picturePath,
name: "picture." + ext, name: "picture." + ext,
@@ -256,11 +256,11 @@ func (r *NostrRoot) fetchProfilePicture(dirPath string, pubkey nostr.PubKey) {
} }
} }
func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) { func (r *FSRoot) fetchEvents(dirPath string, filter nostr.Filter) {
ctx, cancel := context.WithTimeout(r.ctx, time.Second*10) ctx, cancel := context.WithTimeout(r.ctx, time.Second*10)
defer cancel() defer cancel()
// Get relays for authors // get relays for authors
var relays []string var relays []string
if len(filter.Authors) > 0 { if len(filter.Authors) > 0 {
relays = r.sys.FetchOutboxRelays(ctx, filter.Authors[0], 3) relays = r.sys.FetchOutboxRelays(ctx, filter.Authors[0], 3)
@@ -272,12 +272,12 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
log := r.getLog() log := r.getLog()
log("- fetching events for %s from %v\n", dirPath, relays) log("- fetching events for %s from %v\n", dirPath, relays)
// Fetch events // fetch events
events := make([]*nostr.Event, 0) events := make([]*nostr.Event, 0)
for ie := range r.sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{ for ie := range r.sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{
Label: "nak-fs", Label: "nak-fs",
}) { }) {
// Make a copy to avoid pointer issues with loop variable // make a copy to avoid pointer issues with loop variable
evt := ie.Event evt := ie.Event
events = append(events, &evt) events = append(events, &evt)
if len(events) >= int(filter.Limit) { if len(events) >= int(filter.Limit) {
@@ -295,14 +295,14 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
return return
} }
// Track oldest timestamp for pagination // track oldest timestamp for pagination
var oldestTimestamp nostr.Timestamp var oldestTimestamp nostr.Timestamp
if len(events) > 0 { if len(events) > 0 {
oldestTimestamp = events[len(events)-1].CreatedAt oldestTimestamp = events[len(events)-1].CreatedAt
} }
for _, evt := range events { for _, evt := range events {
// Create filename based on event // create filename based on event
filename := r.eventToFilename(evt) filename := r.eventToFilename(evt)
filePath := dirPath + "/" + filename filePath := dirPath + "/" + filename
@@ -315,7 +315,7 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
content = "(empty)" content = "(empty)"
} }
fileNode := &Node{ fileNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: filePath, path: filePath,
name: filename, name: filename,
@@ -330,9 +330,9 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
dir.children[filename] = fileNode dir.children[filename] = fileNode
} }
// Add "more" file for pagination if we got a full page // add "more" file for pagination if we got a full page
if len(events) >= int(filter.Limit) { if len(events) >= int(filter.Limit) {
moreFile := &Node{ moreFile := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath + "/.more", path: dirPath + "/.more",
name: ".more", name: ".more",
@@ -342,7 +342,7 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
data: []byte(fmt.Sprintf("Read this file to load more events (until: %d)\n", oldestTimestamp)), data: []byte(fmt.Sprintf("Read this file to load more events (until: %d)\n", oldestTimestamp)),
size: int64(len(fmt.Sprintf("Read this file to load more events (until: %d)\n", oldestTimestamp))), size: int64(len(fmt.Sprintf("Read this file to load more events (until: %d)\n", oldestTimestamp))),
loadFunc: func() ([]byte, error) { loadFunc: func() ([]byte, error) {
// When .more is read, fetch next page // when .more is read, fetch next page
newFilter := filter newFilter := filter
newFilter.Until = oldestTimestamp newFilter.Until = oldestTimestamp
go r.fetchEvents(dirPath, newFilter) go r.fetchEvents(dirPath, newFilter)
@@ -355,24 +355,24 @@ func (r *NostrRoot) fetchEvents(dirPath string, filter nostr.Filter) {
} }
} }
func (r *NostrRoot) eventToFilename(evt *nostr.Event) string { func (r *FSRoot) eventToFilename(evt *nostr.Event) string {
// Use event ID first 8 chars + extension based on kind // use event ID first 8 chars + extension based on kind
ext := kindToExtension(evt.Kind) ext := kindToExtension(evt.Kind)
// Get hex representation of event ID // get hex representation of event ID
// evt.ID.String() may return format like ":1234abcd" so use Hex() or remove colons // evt.ID.String() may return format like ":1234abcd" so use Hex() or remove colons
idHex := evt.ID.Hex() idHex := evt.ID.Hex()
if len(idHex) > 8 { if len(idHex) > 8 {
idHex = idHex[:8] idHex = idHex[:8]
} }
// For articles, try to use title // for articles, try to use title
if evt.Kind == 30023 || evt.Kind == 30818 { if evt.Kind == 30023 || evt.Kind == 30818 {
for _, tag := range evt.Tags { for _, tag := range evt.Tags {
if len(tag) >= 2 && tag[0] == "title" { if len(tag) >= 2 && tag[0] == "title" {
titleStr := tag[1] titleStr := tag[1]
if titleStr != "" { if titleStr != "" {
// Sanitize title for filename // sanitize title for filename
name := strings.Map(func(r rune) rune { name := strings.Map(func(r rune) rune {
if r == '/' || r == '\\' || r == ':' || r == '*' || r == '?' || r == '"' || r == '<' || r == '>' || r == '|' { if r == '/' || r == '\\' || r == ':' || r == '*' || r == '?' || r == '"' || r == '<' || r == '>' || r == '|' {
return '-' return '-'
@@ -391,35 +391,35 @@ func (r *NostrRoot) eventToFilename(evt *nostr.Event) string {
return fmt.Sprintf("%s.%s", idHex, ext) return fmt.Sprintf("%s.%s", idHex, ext)
} }
func (r *NostrRoot) getLog() func(string, ...interface{}) { func (r *FSRoot) getLog() func(string, ...interface{}) {
if log := r.ctx.Value("log"); log != nil { if log := r.ctx.Value("log"); log != nil {
return log.(func(string, ...interface{})) return log.(func(string, ...interface{}))
} }
return func(string, ...interface{}) {} return func(string, ...interface{}) {}
} }
func (r *NostrRoot) getNode(path string) *Node { func (r *FSRoot) getNode(path string) *FSNode {
originalPath := path originalPath := path
// Normalize path // normalize path
if path == "" { if path == "" {
path = "/" path = "/"
} }
// Convert Windows backslashes to forward slashes // convert Windows backslashes to forward slashes
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
// Ensure path starts with / // ensure path starts with /
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
} }
// Remove trailing slash except for root // remove trailing slash except for root
if path != "/" && strings.HasSuffix(path, "/") { if path != "/" && strings.HasSuffix(path, "/") {
path = strings.TrimSuffix(path, "/") path = strings.TrimSuffix(path, "/")
} }
// Debug logging // debug logging
if r.ctx.Value("logverbose") != nil { if r.ctx.Value("logverbose") != nil {
logv := r.ctx.Value("logverbose").(func(string, ...interface{})) logv := r.ctx.Value("logverbose").(func(string, ...interface{}))
logv("getNode: original='%s' normalized='%s'\n", originalPath, path) logv("getNode: original='%s' normalized='%s'\n", originalPath, path)
@@ -430,7 +430,7 @@ func (r *NostrRoot) getNode(path string) *Node {
node := r.nodes[path] node := r.nodes[path]
// Debug: if not found, show similar paths // debug: if not found, show similar paths
if node == nil && r.ctx.Value("logverbose") != nil { if node == nil && r.ctx.Value("logverbose") != nil {
logv := r.ctx.Value("logverbose").(func(string, ...interface{})) logv := r.ctx.Value("logverbose").(func(string, ...interface{}))
logv("getNode: NOT FOUND '%s'\n", path) logv("getNode: NOT FOUND '%s'\n", path)
@@ -451,11 +451,11 @@ func (r *NostrRoot) getNode(path string) *Node {
return node return node
} }
func (r *NostrRoot) Getattr(path string, stat *fuse.Stat_t, fh uint64) int { func (r *FSRoot) Getattr(path string, stat *fuse.Stat_t, fh uint64) int {
node := r.getNode(path) node := r.getNode(path)
// If node doesn't exist, try dynamic lookup // if node doesn't exist, try dynamic lookup
// But skip for special files starting with @ or . // but skip for special files starting with @ or .
if node == nil { if node == nil {
basename := filepath.Base(path) basename := filepath.Base(path)
if !strings.HasPrefix(basename, "@") && !strings.HasPrefix(basename, ".") { if !strings.HasPrefix(basename, "@") && !strings.HasPrefix(basename, ".") {
@@ -480,14 +480,14 @@ func (r *NostrRoot) Getattr(path string, stat *fuse.Stat_t, fh uint64) int {
} }
// dynamicLookup tries to create nodes on-demand for npub/note/nevent paths // dynamicLookup tries to create nodes on-demand for npub/note/nevent paths
func (r *NostrRoot) dynamicLookup(path string) bool { func (r *FSRoot) dynamicLookup(path string) bool {
// Normalize path // normalize path
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
} }
// Get the first component after root // get the first component after root
parts := strings.Split(strings.TrimPrefix(path, "/"), "/") parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
if len(parts) == 0 { if len(parts) == 0 {
return false return false
@@ -495,10 +495,10 @@ func (r *NostrRoot) dynamicLookup(path string) bool {
name := parts[0] name := parts[0]
// Try to decode as nostr pointer // try to decode as nostr pointer
pointer, err := nip19.ToPointer(name) pointer, err := nip19.ToPointer(name)
if err != nil { if err != nil {
// Try NIP-05 // try NIP-05
if strings.Contains(name, "@") && !strings.HasPrefix(name, "@") { if strings.Contains(name, "@") && !strings.HasPrefix(name, "@") {
ctx, cancel := context.WithTimeout(r.ctx, time.Second*5) ctx, cancel := context.WithTimeout(r.ctx, time.Second*5)
defer cancel() defer cancel()
@@ -515,19 +515,19 @@ func (r *NostrRoot) dynamicLookup(path string) bool {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Check if already exists // check if already exists
if _, exists := r.nodes["/"+name]; exists { if _, exists := r.nodes["/"+name]; exists {
return true return true
} }
switch p := pointer.(type) { switch p := pointer.(type) {
case nostr.ProfilePointer: case nostr.ProfilePointer:
// Create npub directory dynamically // create npub directory dynamically
r.createNpubDirLocked(name, p.PublicKey, nil) r.createNpubDirLocked(name, p.PublicKey, nil)
return true return true
case nostr.EventPointer: case nostr.EventPointer:
// Create event directory dynamically // create event directory dynamically
return r.createEventDirLocked(name, p) return r.createEventDirLocked(name, p)
default: default:
@@ -535,30 +535,30 @@ func (r *NostrRoot) dynamicLookup(path string) bool {
} }
} }
func (r *NostrRoot) createNpubDirLocked(npub string, pubkey nostr.PubKey, signer nostr.Signer) { func (r *FSRoot) createNpubDirLocked(npub string, pubkey nostr.PubKey, signer nostr.Signer) {
dirPath := "/" + npub dirPath := "/" + npub
// Check if already exists // check if already exists
if _, exists := r.nodes[dirPath]; exists { if _, exists := r.nodes[dirPath]; exists {
return return
} }
dirNode := &Node{ dirNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath, path: dirPath,
name: npub, name: npub,
isDir: true, isDir: true,
mode: fuse.S_IFDIR | 0755, mode: fuse.S_IFDIR | 0755,
mtime: time.Now(), mtime: time.Now(),
children: make(map[string]*Node), children: make(map[string]*FSNode),
} }
r.nextIno++ r.nextIno++
r.nodes[dirPath] = dirNode r.nodes[dirPath] = dirNode
r.nodes["/"].children[npub] = dirNode r.nodes["/"].children[npub] = dirNode
// Add pubkey file // add pubkey file
pubkeyData := []byte(pubkey.Hex() + "\n") pubkeyData := []byte(pubkey.Hex() + "\n")
pubkeyNode := &Node{ pubkeyNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath + "/pubkey", path: dirPath + "/pubkey",
name: "pubkey", name: "pubkey",
@@ -572,78 +572,78 @@ func (r *NostrRoot) createNpubDirLocked(npub string, pubkey nostr.PubKey, signer
r.nodes[dirPath+"/pubkey"] = pubkeyNode r.nodes[dirPath+"/pubkey"] = pubkeyNode
dirNode.children["pubkey"] = pubkeyNode dirNode.children["pubkey"] = pubkeyNode
// Fetch metadata asynchronously // fetch metadata asynchronously
go r.fetchMetadata(dirPath, pubkey) go r.fetchMetadata(dirPath, pubkey)
// Add notes directory // add notes directory
r.createViewDirLocked(dirPath, "notes", nostr.Filter{ r.createViewDirLocked(dirPath, "notes", nostr.Filter{
Kinds: []nostr.Kind{1}, Kinds: []nostr.Kind{1},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add articles directory // add articles directory
r.createViewDirLocked(dirPath, "articles", nostr.Filter{ r.createViewDirLocked(dirPath, "articles", nostr.Filter{
Kinds: []nostr.Kind{30023}, Kinds: []nostr.Kind{30023},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add comments directory // add comments directory
r.createViewDirLocked(dirPath, "comments", nostr.Filter{ r.createViewDirLocked(dirPath, "comments", nostr.Filter{
Kinds: []nostr.Kind{1111}, Kinds: []nostr.Kind{1111},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add highlights directory // add highlights directory
r.createViewDirLocked(dirPath, "highlights", nostr.Filter{ r.createViewDirLocked(dirPath, "highlights", nostr.Filter{
Kinds: []nostr.Kind{9802}, Kinds: []nostr.Kind{9802},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add photos directory // add photos directory
r.createViewDirLocked(dirPath, "photos", nostr.Filter{ r.createViewDirLocked(dirPath, "photos", nostr.Filter{
Kinds: []nostr.Kind{20}, Kinds: []nostr.Kind{20},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add videos directory // add videos directory
r.createViewDirLocked(dirPath, "videos", nostr.Filter{ r.createViewDirLocked(dirPath, "videos", nostr.Filter{
Kinds: []nostr.Kind{21, 22}, Kinds: []nostr.Kind{21, 22},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Add wikis directory // add wikis directory
r.createViewDirLocked(dirPath, "wikis", nostr.Filter{ r.createViewDirLocked(dirPath, "wikis", nostr.Filter{
Kinds: []nostr.Kind{30818}, Kinds: []nostr.Kind{30818},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
Limit: 50, Limit: 50,
}) })
// Fetch profile picture asynchronously // fetch profile picture asynchronously
go r.fetchProfilePicture(dirPath, pubkey) go r.fetchProfilePicture(dirPath, pubkey)
} }
func (r *NostrRoot) createViewDirLocked(parentPath, name string, filter nostr.Filter) { func (r *FSRoot) createViewDirLocked(parentPath, name string, filter nostr.Filter) {
dirPath := parentPath + "/" + name dirPath := parentPath + "/" + name
// Check if already exists // check if already exists
if _, exists := r.nodes[dirPath]; exists { if _, exists := r.nodes[dirPath]; exists {
return return
} }
dirNode := &Node{ dirNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath, path: dirPath,
name: name, name: name,
isDir: true, isDir: true,
mode: fuse.S_IFDIR | 0755, mode: fuse.S_IFDIR | 0755,
mtime: time.Now(), mtime: time.Now(),
children: make(map[string]*Node), children: make(map[string]*FSNode),
} }
r.nextIno++ r.nextIno++
@@ -652,14 +652,14 @@ func (r *NostrRoot) createViewDirLocked(parentPath, name string, filter nostr.Fi
parent.children[name] = dirNode parent.children[name] = dirNode
} }
// Fetch events asynchronously // fetch events asynchronously
go r.fetchEvents(dirPath, filter) go r.fetchEvents(dirPath, filter)
} }
func (r *NostrRoot) createEventDirLocked(name string, pointer nostr.EventPointer) bool { func (r *FSRoot) createEventDirLocked(name string, pointer nostr.EventPointer) bool {
dirPath := "/" + name dirPath := "/" + name
// Fetch the event // fetch the event
ctx, cancel := context.WithTimeout(r.ctx, time.Second*10) ctx, cancel := context.WithTimeout(r.ctx, time.Second*10)
defer cancel() defer cancel()
@@ -676,7 +676,7 @@ func (r *NostrRoot) createEventDirLocked(name string, pointer nostr.EventPointer
for ie := range r.sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{ for ie := range r.sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{
Label: "nak-fs-event", Label: "nak-fs-event",
}) { }) {
// Make a copy to avoid pointer issues // make a copy to avoid pointer issues
evtCopy := ie.Event evtCopy := ie.Event
evt = &evtCopy evt = &evtCopy
break break
@@ -686,24 +686,24 @@ func (r *NostrRoot) createEventDirLocked(name string, pointer nostr.EventPointer
return false return false
} }
// Create event directory // create event directory
dirNode := &Node{ dirNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: dirPath, path: dirPath,
name: name, name: name,
isDir: true, isDir: true,
mode: fuse.S_IFDIR | 0755, mode: fuse.S_IFDIR | 0755,
mtime: time.Unix(int64(evt.CreatedAt), 0), mtime: time.Unix(int64(evt.CreatedAt), 0),
children: make(map[string]*Node), children: make(map[string]*FSNode),
} }
r.nextIno++ r.nextIno++
r.nodes[dirPath] = dirNode r.nodes[dirPath] = dirNode
r.nodes["/"].children[name] = dirNode r.nodes["/"].children[name] = dirNode
// Add content file // add content file
ext := kindToExtension(evt.Kind) ext := kindToExtension(evt.Kind)
contentPath := dirPath + "/content." + ext contentPath := dirPath + "/content." + ext
contentNode := &Node{ contentNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: contentPath, path: contentPath,
name: "content." + ext, name: "content." + ext,
@@ -717,10 +717,10 @@ func (r *NostrRoot) createEventDirLocked(name string, pointer nostr.EventPointer
r.nodes[contentPath] = contentNode r.nodes[contentPath] = contentNode
dirNode.children["content."+ext] = contentNode dirNode.children["content."+ext] = contentNode
// Add event.json // add event.json
eventJSON, _ := json.MarshalIndent(evt, "", " ") eventJSON, _ := json.MarshalIndent(evt, "", " ")
eventJSONPath := dirPath + "/event.json" eventJSONPath := dirPath + "/event.json"
eventJSONNode := &Node{ eventJSONNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: eventJSONPath, path: eventJSONPath,
name: "event.json", name: "event.json",
@@ -737,11 +737,11 @@ func (r *NostrRoot) createEventDirLocked(name string, pointer nostr.EventPointer
return true return true
} }
func (r *NostrRoot) Readdir(path string, func (r *FSRoot) Readdir(path string,
fill func(name string, stat *fuse.Stat_t, ofst int64) bool, fill func(name string, stat *fuse.Stat_t, ofst int64) bool,
ofst int64, ofst int64,
fh uint64) int { fh uint64,
) int {
node := r.getNode(path) node := r.getNode(path)
if node == nil || !node.isDir { if node == nil || !node.isDir {
return -fuse.ENOENT return -fuse.ENOENT
@@ -768,8 +768,8 @@ func (r *NostrRoot) Readdir(path string,
return 0 return 0
} }
func (r *NostrRoot) Open(path string, flags int) (int, uint64) { func (r *FSRoot) Open(path string, flags int) (int, uint64) {
// Log the open attempt // log the open attempt
if r.ctx.Value("logverbose") != nil { if r.ctx.Value("logverbose") != nil {
logv := r.ctx.Value("logverbose").(func(string, ...interface{})) logv := r.ctx.Value("logverbose").(func(string, ...interface{}))
logv("Open: path='%s' flags=%d\n", path, flags) logv("Open: path='%s' flags=%d\n", path, flags)
@@ -783,7 +783,7 @@ func (r *NostrRoot) Open(path string, flags int) (int, uint64) {
return -fuse.EISDIR, ^uint64(0) return -fuse.EISDIR, ^uint64(0)
} }
// Load data if needed // load data if needed
if node.loadFunc != nil && !node.loaded { if node.loadFunc != nil && !node.loaded {
r.mu.Lock() r.mu.Lock()
if !node.loaded { if !node.loaded {
@@ -799,7 +799,7 @@ func (r *NostrRoot) Open(path string, flags int) (int, uint64) {
return 0, node.ino return 0, node.ino
} }
func (r *NostrRoot) Read(path string, buff []byte, ofst int64, fh uint64) int { func (r *FSRoot) Read(path string, buff []byte, ofst int64, fh uint64) int {
node := r.getNode(path) node := r.getNode(path)
if node == nil || node.isDir { if node == nil || node.isDir {
return -fuse.ENOENT return -fuse.ENOENT
@@ -818,7 +818,7 @@ func (r *NostrRoot) Read(path string, buff []byte, ofst int64, fh uint64) int {
return n return n
} }
func (r *NostrRoot) Opendir(path string) (int, uint64) { func (r *FSRoot) Opendir(path string) (int, uint64) {
node := r.getNode(path) node := r.getNode(path)
if node == nil { if node == nil {
return -fuse.ENOENT, ^uint64(0) return -fuse.ENOENT, ^uint64(0)
@@ -829,17 +829,17 @@ func (r *NostrRoot) Opendir(path string) (int, uint64) {
return 0, node.ino return 0, node.ino
} }
func (r *NostrRoot) Release(path string, fh uint64) int { func (r *FSRoot) Release(path string, fh uint64) int {
return 0 return 0
} }
func (r *NostrRoot) Releasedir(path string, fh uint64) int { func (r *FSRoot) Releasedir(path string, fh uint64) int {
return 0 return 0
} }
// Create creates a new file // Create creates a new file
func (r *NostrRoot) Create(path string, flags int, mode uint32) (int, uint64) { func (r *FSRoot) Create(path string, flags int, mode uint32) (int, uint64) {
// Parse path // parse path
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
@@ -851,19 +851,19 @@ func (r *NostrRoot) Create(path string, flags int, mode uint32) (int, uint64) {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Check if parent directory exists // check if parent directory exists
parent, ok := r.nodes[dir] parent, ok := r.nodes[dir]
if !ok || !parent.isDir { if !ok || !parent.isDir {
return -fuse.ENOENT, ^uint64(0) return -fuse.ENOENT, ^uint64(0)
} }
// Check if file already exists // check if file already exists
if _, exists := r.nodes[path]; exists { if _, exists := r.nodes[path]; exists {
return -fuse.EEXIST, ^uint64(0) return -fuse.EEXIST, ^uint64(0)
} }
// Create new file node // create new file node
fileNode := &Node{ fileNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: path, path: path,
name: name, name: name,
@@ -882,7 +882,7 @@ func (r *NostrRoot) Create(path string, flags int, mode uint32) (int, uint64) {
} }
// Truncate truncates a file // Truncate truncates a file
func (r *NostrRoot) Truncate(path string, size int64, fh uint64) int { func (r *FSRoot) Truncate(path string, size int64, fh uint64) int {
node := r.getNode(path) node := r.getNode(path)
if node == nil { if node == nil {
return -fuse.ENOENT return -fuse.ENOENT
@@ -899,7 +899,7 @@ func (r *NostrRoot) Truncate(path string, size int64, fh uint64) int {
} else if size < int64(len(node.data)) { } else if size < int64(len(node.data)) {
node.data = node.data[:size] node.data = node.data[:size]
} else { } else {
// Extend with zeros // extend with zeros
newData := make([]byte, size) newData := make([]byte, size)
copy(newData, node.data) copy(newData, node.data)
node.data = newData node.data = newData
@@ -911,7 +911,7 @@ func (r *NostrRoot) Truncate(path string, size int64, fh uint64) int {
} }
// Write writes data to a file // Write writes data to a file
func (r *NostrRoot) Write(path string, buff []byte, ofst int64, fh uint64) int { func (r *FSRoot) Write(path string, buff []byte, ofst int64, fh uint64) int {
node := r.getNode(path) node := r.getNode(path)
if node == nil { if node == nil {
return -fuse.ENOENT return -fuse.ENOENT
@@ -925,7 +925,7 @@ func (r *NostrRoot) Write(path string, buff []byte, ofst int64, fh uint64) int {
endofst := ofst + int64(len(buff)) endofst := ofst + int64(len(buff))
// Extend data if necessary // extend data if necessary
if endofst > int64(len(node.data)) { if endofst > int64(len(node.data)) {
newData := make([]byte, endofst) newData := make([]byte, endofst)
copy(newData, node.data) copy(newData, node.data)
@@ -936,14 +936,14 @@ func (r *NostrRoot) Write(path string, buff []byte, ofst int64, fh uint64) int {
node.size = int64(len(node.data)) node.size = int64(len(node.data))
node.mtime = time.Now() node.mtime = time.Now()
// Check if this is a note that should be auto-published // check if this is a note that should be auto-published
if r.signer != nil && strings.Contains(path, "/notes/") && !strings.HasPrefix(filepath.Base(path), ".") { if r.signer != nil && strings.Contains(path, "/notes/") && !strings.HasPrefix(filepath.Base(path), ".") {
// Cancel existing timer if any // cancel existing timer if any
if timer, exists := r.pendingNotes[path]; exists { if timer, exists := r.pendingNotes[path]; exists {
timer.Stop() timer.Stop()
} }
// Schedule auto-publish // schedule auto-publish
timeout := r.opts.AutoPublishNotesTimeout timeout := r.opts.AutoPublishNotesTimeout
if timeout > 0 && timeout < time.Hour*24*365 { if timeout > 0 && timeout < time.Hour*24*365 {
r.pendingNotes[path] = time.AfterFunc(timeout, func() { r.pendingNotes[path] = time.AfterFunc(timeout, func() {
@@ -955,7 +955,7 @@ func (r *NostrRoot) Write(path string, buff []byte, ofst int64, fh uint64) int {
return n return n
} }
func (r *NostrRoot) publishNote(path string) { func (r *FSRoot) publishNote(path string) {
r.mu.Lock() r.mu.Lock()
node, ok := r.nodes[path] node, ok := r.nodes[path]
if !ok { if !ok {
@@ -973,7 +973,7 @@ func (r *NostrRoot) publishNote(path string) {
log := r.getLog() log := r.getLog()
log("- auto-publishing note from %s\n", path) log("- auto-publishing note from %s\n", path)
// Create and sign event // create and sign event
evt := &nostr.Event{ evt := &nostr.Event{
CreatedAt: nostr.Now(), CreatedAt: nostr.Now(),
Kind: 1, Kind: 1,
@@ -986,7 +986,7 @@ func (r *NostrRoot) publishNote(path string) {
return return
} }
// Publish to relays // publish to relays
ctx, cancel := context.WithTimeout(r.ctx, time.Second*10) ctx, cancel := context.WithTimeout(r.ctx, time.Second*10)
defer cancel() defer cancel()
@@ -1005,7 +1005,7 @@ func (r *NostrRoot) publishNote(path string) {
log("- published note %s to %d relays\n", evt.ID.Hex()[:8], len(relays)) log("- published note %s to %d relays\n", evt.ID.Hex()[:8], len(relays))
// Update filename to include event ID // update filename to include event ID
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
@@ -1015,7 +1015,7 @@ func (r *NostrRoot) publishNote(path string) {
newName := evt.ID.Hex()[:8] + ext newName := evt.ID.Hex()[:8] + ext
newPath := dir + "/" + newName newPath := dir + "/" + newName
// Rename node // rename node
if _, exists := r.nodes[newPath]; !exists { if _, exists := r.nodes[newPath]; !exists {
node.path = newPath node.path = newPath
node.name = newName node.name = newName
@@ -1032,7 +1032,7 @@ func (r *NostrRoot) publishNote(path string) {
} }
// Unlink deletes a file // Unlink deletes a file
func (r *NostrRoot) Unlink(path string) int { func (r *FSRoot) Unlink(path string) int {
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
@@ -1044,7 +1044,7 @@ func (r *NostrRoot) Unlink(path string) int {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Check if file exists // check if file exists
node, ok := r.nodes[path] node, ok := r.nodes[path]
if !ok { if !ok {
return -fuse.ENOENT return -fuse.ENOENT
@@ -1053,19 +1053,19 @@ func (r *NostrRoot) Unlink(path string) int {
return -fuse.EISDIR return -fuse.EISDIR
} }
// Remove from parent // remove from parent
if parent, ok := r.nodes[dir]; ok { if parent, ok := r.nodes[dir]; ok {
delete(parent.children, name) delete(parent.children, name)
} }
// Remove from nodes map // remove from nodes map
delete(r.nodes, path) delete(r.nodes, path)
return 0 return 0
} }
// Mkdir creates a new directory // Mkdir creates a new directory
func (r *NostrRoot) Mkdir(path string, mode uint32) int { func (r *FSRoot) Mkdir(path string, mode uint32) int {
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
@@ -1077,26 +1077,26 @@ func (r *NostrRoot) Mkdir(path string, mode uint32) int {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Check if parent directory exists // check if parent directory exists
parent, ok := r.nodes[dir] parent, ok := r.nodes[dir]
if !ok || !parent.isDir { if !ok || !parent.isDir {
return -fuse.ENOENT return -fuse.ENOENT
} }
// Check if directory already exists // check if directory already exists
if _, exists := r.nodes[path]; exists { if _, exists := r.nodes[path]; exists {
return -fuse.EEXIST return -fuse.EEXIST
} }
// Create new directory node // create new directory node
dirNode := &Node{ dirNode := &FSNode{
ino: r.nextIno, ino: r.nextIno,
path: path, path: path,
name: name, name: name,
isDir: true, isDir: true,
mode: fuse.S_IFDIR | 0755, mode: fuse.S_IFDIR | 0755,
mtime: time.Now(), mtime: time.Now(),
children: make(map[string]*Node), children: make(map[string]*FSNode),
} }
r.nextIno++ r.nextIno++
@@ -1107,7 +1107,7 @@ func (r *NostrRoot) Mkdir(path string, mode uint32) int {
} }
// Rmdir removes a directory // Rmdir removes a directory
func (r *NostrRoot) Rmdir(path string) int { func (r *FSRoot) Rmdir(path string) int {
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
@@ -1123,7 +1123,7 @@ func (r *NostrRoot) Rmdir(path string) int {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
// Check if directory exists // check if directory exists
node, ok := r.nodes[path] node, ok := r.nodes[path]
if !ok { if !ok {
return -fuse.ENOENT return -fuse.ENOENT
@@ -1132,24 +1132,24 @@ func (r *NostrRoot) Rmdir(path string) int {
return -fuse.ENOTDIR return -fuse.ENOTDIR
} }
// Check if directory is empty // check if directory is empty
if len(node.children) > 0 { if len(node.children) > 0 {
return -fuse.ENOTEMPTY return -fuse.ENOTEMPTY
} }
// Remove from parent // remove from parent
if parent, ok := r.nodes[dir]; ok { if parent, ok := r.nodes[dir]; ok {
delete(parent.children, name) delete(parent.children, name)
} }
// Remove from nodes map // remove from nodes map
delete(r.nodes, path) delete(r.nodes, path)
return 0 return 0
} }
// Utimens updates file timestamps // Utimens updates file timestamps
func (r *NostrRoot) Utimens(path string, tmsp []fuse.Timespec) int { func (r *FSRoot) Utimens(path string, tmsp []fuse.Timespec) int {
node := r.getNode(path) node := r.getNode(path)
if node == nil { if node == nil {
return -fuse.ENOENT return -fuse.ENOENT
@@ -1164,3 +1164,14 @@ func (r *NostrRoot) Utimens(path string, tmsp []fuse.Timespec) int {
return 0 return 0
} }
func kindToExtension(kind nostr.Kind) string {
switch kind {
case 30023:
return "md"
case 30818:
return "djot"
default:
return "txt"
}
}