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)
}
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)
}

View File

@ -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,
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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"})
})

View File

@ -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[:])})