From b3b8d5804deb333d9a2d4e588cc4fae2183de00d Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 4 Mar 2025 19:20:11 -0300 Subject: [PATCH] nipb0: fixes to blossom. --- nipb0/blossom/check.go | 2 +- nipb0/blossom/client.go | 7 ++++++- nipb0/blossom/delete.go | 3 +-- nipb0/blossom/http.go | 2 +- nipb0/blossom/list.go | 13 +++---------- nipb0/blossom/upload.go | 2 +- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/nipb0/blossom/check.go b/nipb0/blossom/check.go index 37b2f35..8000d0a 100644 --- a/nipb0/blossom/check.go +++ b/nipb0/blossom/check.go @@ -13,7 +13,7 @@ func (c *Client) Check(ctx context.Context, hash string) error { return fmt.Errorf("%s is not a valid 32-byte hex string", hash) } - err := c.httpCall(ctx, "HEAD", c.mediaserver+"/"+hash, "", nil, nil, 0, nil) + err := c.httpCall(ctx, "HEAD", hash, "", nil, nil, 0, nil) if err != nil { return fmt.Errorf("failed to check for %s: %w", hash, err) } diff --git a/nipb0/blossom/client.go b/nipb0/blossom/client.go index b20bae4..466fd44 100644 --- a/nipb0/blossom/client.go +++ b/nipb0/blossom/client.go @@ -1,6 +1,7 @@ package blossom import ( + "strings" "time" "github.com/nbd-wtf/go-nostr" @@ -16,8 +17,12 @@ type Client struct { // NewClient creates a new Blossom client func NewClient(mediaserver string, signer nostr.Signer) *Client { + if !strings.HasPrefix(mediaserver, "http") { + mediaserver = "https://" + mediaserver + } + return &Client{ - mediaserver: mediaserver, + mediaserver: strings.TrimSuffix(mediaserver, "/") + "/", httpClient: createHTTPClient(), signer: signer, } diff --git a/nipb0/blossom/delete.go b/nipb0/blossom/delete.go index dcdf64d..4a6d3fd 100644 --- a/nipb0/blossom/delete.go +++ b/nipb0/blossom/delete.go @@ -9,13 +9,12 @@ import ( // Delete deletes a file from the media server by its hash func (c *Client) Delete(ctx context.Context, hash string) error { - err := c.httpCall(ctx, "DELETE", c.mediaserver+"/"+hash, "", func() string { + err := c.httpCall(ctx, "DELETE", hash, "", func() string { return c.authorizationHeader(ctx, func(evt *nostr.Event) { evt.Tags = append(evt.Tags, nostr.Tag{"t", "delete"}) evt.Tags = append(evt.Tags, nostr.Tag{"x", hash}) }) }, nil, 0, nil) - if err != nil { return fmt.Errorf("failed to delete %s: %w", hash, err) } diff --git a/nipb0/blossom/http.go b/nipb0/blossom/http.go index 61efbb7..89d0e48 100644 --- a/nipb0/blossom/http.go +++ b/nipb0/blossom/http.go @@ -28,7 +28,7 @@ func (c *Client) httpCall( req := fasthttp.AcquireRequest() defer fasthttp.ReleaseRequest(req) - req.SetRequestURI(url) + req.SetRequestURI(c.mediaserver + url) req.Header.SetMethod(method) req.Header.SetContentType(contentType) diff --git a/nipb0/blossom/list.go b/nipb0/blossom/list.go index 98f7dc0..83f36df 100644 --- a/nipb0/blossom/list.go +++ b/nipb0/blossom/list.go @@ -8,21 +8,14 @@ import ( ) // List retrieves a list of blobs from a specific pubkey -func (c *Client) List(ctx context.Context, pubkey string) ([]BlobDescriptor, error) { - if pubkey == "" { - var err error - pubkey, err = c.signer.GetPublicKey(ctx) - if err != nil { - return nil, fmt.Errorf("could not get pubkey: %w", err) - } - } - +func (c *Client) List(ctx context.Context) ([]BlobDescriptor, error) { + pubkey, err := c.signer.GetPublicKey(ctx) if !nostr.IsValidPublicKey(pubkey) { return nil, fmt.Errorf("pubkey %s is not valid", pubkey) } bds := make([]BlobDescriptor, 0, 100) - err := c.httpCall(ctx, "GET", c.mediaserver+"/list/"+pubkey, "", func() string { + err = c.httpCall(ctx, "GET", "list/"+pubkey, "", func() string { return c.authorizationHeader(ctx, func(evt *nostr.Event) { evt.Tags = append(evt.Tags, nostr.Tag{"t", "list"}) }) diff --git a/nipb0/blossom/upload.go b/nipb0/blossom/upload.go index 975a83f..6d5907a 100644 --- a/nipb0/blossom/upload.go +++ b/nipb0/blossom/upload.go @@ -36,7 +36,7 @@ func (c *Client) UploadFile(ctx context.Context, filePath string) (*BlobDescript contentType := mime.TypeByExtension(filepath.Ext(filePath)) bd := BlobDescriptor{} - err = c.httpCall(ctx, "PUT", c.mediaserver+"/upload", contentType, func() string { + err = c.httpCall(ctx, "PUT", "upload", contentType, func() string { return c.authorizationHeader(ctx, func(evt *nostr.Event) { evt.Tags = append(evt.Tags, nostr.Tag{"t", "upload"}) evt.Tags = append(evt.Tags, nostr.Tag{"x", hex.EncodeToString(hash[:])})