nipb0: fixes to blossom.

This commit is contained in:
fiatjaf
2025-03-04 19:20:11 -03:00
parent 2865cccc46
commit b3b8d5804d
6 changed files with 13 additions and 16 deletions

View File

@@ -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) 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 { if err != nil {
return fmt.Errorf("failed to check for %s: %w", hash, err) return fmt.Errorf("failed to check for %s: %w", hash, err)
} }

View File

@@ -1,6 +1,7 @@
package blossom package blossom
import ( import (
"strings"
"time" "time"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
@@ -16,8 +17,12 @@ type Client struct {
// NewClient creates a new Blossom client // NewClient creates a new Blossom client
func NewClient(mediaserver string, signer nostr.Signer) *Client { func NewClient(mediaserver string, signer nostr.Signer) *Client {
if !strings.HasPrefix(mediaserver, "http") {
mediaserver = "https://" + mediaserver
}
return &Client{ return &Client{
mediaserver: mediaserver, mediaserver: strings.TrimSuffix(mediaserver, "/") + "/",
httpClient: createHTTPClient(), httpClient: createHTTPClient(),
signer: signer, signer: signer,
} }

View File

@@ -9,13 +9,12 @@ import (
// Delete deletes a file from the media server by its hash // Delete deletes a file from the media server by its hash
func (c *Client) Delete(ctx context.Context, hash string) error { 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) { return c.authorizationHeader(ctx, func(evt *nostr.Event) {
evt.Tags = append(evt.Tags, nostr.Tag{"t", "delete"}) evt.Tags = append(evt.Tags, nostr.Tag{"t", "delete"})
evt.Tags = append(evt.Tags, nostr.Tag{"x", hash}) evt.Tags = append(evt.Tags, nostr.Tag{"x", hash})
}) })
}, nil, 0, nil) }, nil, 0, nil)
if err != nil { if err != nil {
return fmt.Errorf("failed to delete %s: %w", hash, err) return fmt.Errorf("failed to delete %s: %w", hash, err)
} }

View File

@@ -28,7 +28,7 @@ func (c *Client) httpCall(
req := fasthttp.AcquireRequest() req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req) defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(url) req.SetRequestURI(c.mediaserver + url)
req.Header.SetMethod(method) req.Header.SetMethod(method)
req.Header.SetContentType(contentType) req.Header.SetContentType(contentType)

View File

@@ -8,21 +8,14 @@ import (
) )
// List retrieves a list of blobs from a specific pubkey // List retrieves a list of blobs from a specific pubkey
func (c *Client) List(ctx context.Context, pubkey string) ([]BlobDescriptor, error) { func (c *Client) List(ctx context.Context) ([]BlobDescriptor, error) {
if pubkey == "" { pubkey, err := c.signer.GetPublicKey(ctx)
var err error
pubkey, err = c.signer.GetPublicKey(ctx)
if err != nil {
return nil, fmt.Errorf("could not get pubkey: %w", err)
}
}
if !nostr.IsValidPublicKey(pubkey) { if !nostr.IsValidPublicKey(pubkey) {
return nil, fmt.Errorf("pubkey %s is not valid", pubkey) return nil, fmt.Errorf("pubkey %s is not valid", pubkey)
} }
bds := make([]BlobDescriptor, 0, 100) 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) { return c.authorizationHeader(ctx, func(evt *nostr.Event) {
evt.Tags = append(evt.Tags, nostr.Tag{"t", "list"}) evt.Tags = append(evt.Tags, nostr.Tag{"t", "list"})
}) })

View File

@@ -36,7 +36,7 @@ func (c *Client) UploadFile(ctx context.Context, filePath string) (*BlobDescript
contentType := mime.TypeByExtension(filepath.Ext(filePath)) contentType := mime.TypeByExtension(filepath.Ext(filePath))
bd := BlobDescriptor{} 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) { return c.authorizationHeader(ctx, func(evt *nostr.Event) {
evt.Tags = append(evt.Tags, nostr.Tag{"t", "upload"}) evt.Tags = append(evt.Tags, nostr.Tag{"t", "upload"})
evt.Tags = append(evt.Tags, nostr.Tag{"x", hex.EncodeToString(hash[:])}) evt.Tags = append(evt.Tags, nostr.Tag{"x", hex.EncodeToString(hash[:])})