From b9f1f935616bbe1bd59e2f5aba10628bd1dbaab4 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 13 Jan 2026 00:03:49 -0300 Subject: [PATCH] khatru/blossom: get rid of custom BlobDescriptor with useless Owner field. --- khatru/blossom/blob.go | 17 ++++------------- khatru/blossom/eventstorewrapper.go | 17 ++++++++++------- khatru/blossom/handlers.go | 10 +++------- khatru/blossom/memoryindex.go | 14 +++++++------- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/khatru/blossom/blob.go b/khatru/blossom/blob.go index 5985598..c7232c1 100644 --- a/khatru/blossom/blob.go +++ b/khatru/blossom/blob.go @@ -5,22 +5,13 @@ import ( "iter" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/nipb0/blossom" ) -type BlobDescriptor struct { - URL string `json:"url"` - SHA256 string `json:"sha256"` - Size int `json:"size"` - Type string `json:"type"` - Uploaded nostr.Timestamp `json:"uploaded"` - - Owner nostr.PubKey `json:"-"` -} - type BlobIndex interface { - Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error - List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor] - Get(ctx context.Context, sha256 string) (*BlobDescriptor, error) + Keep(ctx context.Context, blob blossom.BlobDescriptor, pubkey nostr.PubKey) error + List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] + Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) Delete(ctx context.Context, sha256 string, pubkey nostr.PubKey) error } diff --git a/khatru/blossom/eventstorewrapper.go b/khatru/blossom/eventstorewrapper.go index 72a2836..c272407 100644 --- a/khatru/blossom/eventstorewrapper.go +++ b/khatru/blossom/eventstorewrapper.go @@ -17,7 +17,11 @@ type EventStoreBlobIndexWrapper struct { ServiceURL string } -func (es EventStoreBlobIndexWrapper) Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error { +func (es EventStoreBlobIndexWrapper) Keep( + ctx context.Context, + blob blossom.BlobDescriptor, + pubkey nostr.PubKey, +) error { next, stop := iter.Pull( es.Store.QueryEvents(nostr.Filter{Authors: []nostr.PubKey{pubkey}, Kinds: []nostr.Kind{24242}, Tags: nostr.TagMap{"x": []string{blob.SHA256}}}, 1), ) @@ -42,8 +46,8 @@ func (es EventStoreBlobIndexWrapper) Keep(ctx context.Context, blob BlobDescript return nil } -func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor] { - return func(yield func(BlobDescriptor) bool) { +func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] { + return func(yield func(blossom.BlobDescriptor) bool) { for evt := range es.Store.QueryEvents(nostr.Filter{ Authors: []nostr.PubKey{pubkey}, Kinds: []nostr.Kind{24242}, @@ -53,7 +57,7 @@ func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubK } } -func (es EventStoreBlobIndexWrapper) Get(ctx context.Context, sha256 string) (*BlobDescriptor, error) { +func (es EventStoreBlobIndexWrapper) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) { next, stop := iter.Pull( es.Store.QueryEvents(nostr.Filter{Tags: nostr.TagMap{"x": []string{sha256}}, Kinds: []nostr.Kind{24242}, Limit: 1}, 1), ) @@ -87,14 +91,13 @@ func (es EventStoreBlobIndexWrapper) Delete(ctx context.Context, sha256 string, return nil } -func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) BlobDescriptor { +func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) blossom.BlobDescriptor { hhash := evt.Tags[0][1] mimetype := evt.Tags[1][1] ext := blossom.GetExtension(mimetype) size, _ := strconv.Atoi(evt.Tags[2][1]) - return BlobDescriptor{ - Owner: evt.PubKey, + return blossom.BlobDescriptor{ Uploaded: evt.CreatedAt, URL: es.ServiceURL + "/" + hhash + ext, SHA256: hhash, diff --git a/khatru/blossom/handlers.go b/khatru/blossom/handlers.go index 8ec0b8c..095a596 100644 --- a/khatru/blossom/handlers.go +++ b/khatru/blossom/handlers.go @@ -36,11 +36,7 @@ func (bs BlossomServer) handleUploadCheck(w http.ResponseWriter, r *http.Request } mimetype := r.Header.Get("X-Content-Type") - exts, _ := mime.ExtensionsByType(mimetype) - var ext string - if len(exts) > 0 { - ext = exts[0] - } + ext := blossom.GetExtension(mimetype) // get the file size from the incoming header size, _ := strconv.Atoi(r.Header.Get("X-Content-Length")) @@ -135,7 +131,7 @@ func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) { } // keep track of the blob descriptor - bd := BlobDescriptor{ + bd := blossom.BlobDescriptor{ URL: bs.ServiceURL + "/" + hhash + ext, SHA256: hhash, Size: len(b), @@ -472,7 +468,7 @@ func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) { } // keep track of the blob descriptor - bd := BlobDescriptor{ + bd := blossom.BlobDescriptor{ URL: bs.ServiceURL + "/" + hhash + ext, SHA256: hhash, Size: len(body), diff --git a/khatru/blossom/memoryindex.go b/khatru/blossom/memoryindex.go index 82f09f1..ab0d7aa 100644 --- a/khatru/blossom/memoryindex.go +++ b/khatru/blossom/memoryindex.go @@ -7,11 +7,12 @@ import ( "slices" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/nipb0/blossom" "github.com/puzpuzpuz/xsync/v3" ) type ownedBlob struct { - blob BlobDescriptor + blob blossom.BlobDescriptor owners []nostr.PubKey } @@ -25,7 +26,7 @@ func NewMemoryBlobIndex() MemoryBlobIndex { } } -func (x MemoryBlobIndex) Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error { +func (x MemoryBlobIndex) Keep(ctx context.Context, blob blossom.BlobDescriptor, pubkey nostr.PubKey) error { x.m.Compute(blob.SHA256, func(oldValue ownedBlob, loaded bool) (newValue ownedBlob, delete bool) { if loaded { newValue = oldValue @@ -45,10 +46,10 @@ func (x MemoryBlobIndex) Keep(ctx context.Context, blob BlobDescriptor, pubkey n return nil } -func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor] { - return func(yield func(BlobDescriptor) bool) { +func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] { + return func(yield func(blossom.BlobDescriptor) bool) { x.m.Range(func(key string, value ownedBlob) bool { - if value.blob.Owner == value.owners[0] { + if slices.Contains(value.owners, pubkey) { if !yield(value.blob) { return false } @@ -58,9 +59,8 @@ func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq } } -func (x MemoryBlobIndex) Get(ctx context.Context, sha256 string) (*BlobDescriptor, error) { +func (x MemoryBlobIndex) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) { if val, ok := x.m.Load(sha256); ok { - val.blob.Owner = val.owners[0] return &val.blob, nil } return nil, errors.New("not found")