Compare commits

...

4 Commits

Author SHA1 Message Date
fiatjaf
f47282c745 get rid of base64x temporarily since it doesn't work on arm64. 2025-03-19 15:02:56 -03:00
fiatjaf
f72dea346f rename menu item on docs to say "blossom". 2025-03-17 13:38:35 -03:00
fiatjaf
51632dcc9f update blossom example to use a different database.
closes https://github.com/fiatjaf/khatru/issues/36
2025-03-17 13:36:41 -03:00
andrewheadricke
6cc2477e89 fix blossom upload < 50bytes 2025-03-15 01:58:07 -03:00
6 changed files with 14 additions and 10 deletions

View File

@@ -1,12 +1,12 @@
package blossom package blossom
import ( import (
"encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"github.com/cloudwego/base64x"
"github.com/mailru/easyjson" "github.com/mailru/easyjson"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
) )
@@ -17,7 +17,7 @@ func readAuthorization(r *http.Request) (*nostr.Event, error) {
return nil, nil return nil, nil
} }
eventj, err := base64x.StdEncoding.DecodeString(token[6:]) eventj, err := base64.StdEncoding.DecodeString(token[6:])
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid base64 token") return nil, fmt.Errorf("invalid base64 token")
} }

View File

@@ -73,7 +73,7 @@ func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) {
// read first bytes of upload so we can find out the filetype // read first bytes of upload so we can find out the filetype
b := make([]byte, min(50, size), size) b := make([]byte, min(50, size), size)
if _, err = r.Body.Read(b); err != nil { if n, err := r.Body.Read(b); err != nil && n != size {
blossomError(w, "failed to read initial bytes of upload body: "+err.Error(), 400) blossomError(w, "failed to read initial bytes of upload body: "+err.Error(), 400)
return return
} }

View File

@@ -19,7 +19,7 @@ export default {
{ text: 'HTTP Integration', link: '/core/embed' }, { text: 'HTTP Integration', link: '/core/embed' },
{ text: 'Request Routing', link: '/core/routing' }, { text: 'Request Routing', link: '/core/routing' },
{ text: 'Management API', link: '/core/management' }, { text: 'Management API', link: '/core/management' },
{ text: 'Media Storage', link: '/core/blossom' }, { text: 'Media Storage (Blossom)', link: '/core/blossom' },
] ]
}, },
{ {

View File

@@ -18,7 +18,8 @@ func main() {
bl := blossom.New(relay, "http://localhost:3334") bl := blossom.New(relay, "http://localhost:3334")
// create a database for keeping track of blob metadata // create a database for keeping track of blob metadata
bl.Store = blossom.EventStoreBlobIndexWrapper{Store: db, ServiceURL: bl.ServiceURL} // (do not use the same database used for the relay events)
bl.Store = blossom.EventStoreBlobIndexWrapper{Store: blobdb, ServiceURL: bl.ServiceURL}
// implement the required storage functions // implement the required storage functions
bl.StoreBlob = append(bl.StoreBlob, func(ctx context.Context, sha256 string, body []byte) error { bl.StoreBlob = append(bl.StoreBlob, func(ctx context.Context, sha256 string, body []byte) error {

View File

@@ -15,19 +15,22 @@ import (
func main() { func main() {
relay := khatru.NewRelay() relay := khatru.NewRelay()
db := &badger.BadgerBackend{Path: "/tmp/khatru-badger-blossom-tmp"} db := &badger.BadgerBackend{Path: "/tmp/khatru-badger-tmp"}
if err := db.Init(); err != nil { if err := db.Init(); err != nil {
panic(err) panic(err)
} }
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent) relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents) relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.CountEvents = append(relay.CountEvents, db.CountEvents) relay.CountEvents = append(relay.CountEvents, db.CountEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent) relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent) relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent)
bdb := &badger.BadgerBackend{Path: "/tmp/khatru-badger-blossom-tmp"}
if err := bdb.Init(); err != nil {
panic(err)
}
bl := blossom.New(relay, "http://localhost:3334") bl := blossom.New(relay, "http://localhost:3334")
bl.Store = blossom.EventStoreBlobIndexWrapper{Store: db, ServiceURL: bl.ServiceURL} bl.Store = blossom.EventStoreBlobIndexWrapper{Store: bdb, ServiceURL: bl.ServiceURL}
bl.StoreBlob = append(bl.StoreBlob, func(ctx context.Context, sha256 string, body []byte) error { bl.StoreBlob = append(bl.StoreBlob, func(ctx context.Context, sha256 string, body []byte) error {
fmt.Println("storing", sha256, len(body)) fmt.Println("storing", sha256, len(body))
return nil return nil

View File

@@ -3,6 +3,7 @@ package khatru
import ( import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -12,7 +13,6 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/cloudwego/base64x"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip86" "github.com/nbd-wtf/go-nostr/nip86"
) )
@@ -72,7 +72,7 @@ func (rl *Relay) HandleNIP86(w http.ResponseWriter, r *http.Request) {
goto respond goto respond
} }
evtj, err := base64x.StdEncoding.DecodeString(spl[1]) evtj, err := base64.StdEncoding.DecodeString(spl[1])
if err != nil { if err != nil {
resp.Error = "invalid base64 auth" resp.Error = "invalid base64 auth"
goto respond goto respond