nipb0/blossom: mirror.

This commit is contained in:
fiatjaf
2026-01-18 14:30:02 -03:00
parent bcf973f997
commit 57d595a5b4

35
nipb0/blossom/mirror.go Normal file
View File

@@ -0,0 +1,35 @@
package blossom
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"fiatjaf.com/nostr"
)
// MirrorBlob mirrors a blob from a remote URL to the media server
func (c *Client) MirrorBlob(ctx context.Context, remoteBlobURL string) (*BlobDescriptor, error) {
bodyBytes, err := json.Marshal(struct{ URL string }{URL: remoteBlobURL})
if err != nil {
return nil, fmt.Errorf("failed to marshal mirror request: %w", err)
}
spl := strings.Split(remoteBlobURL, "/")
hhash := strings.Split(spl[len(spl)-1], ".")[0]
bd := BlobDescriptor{}
err = c.httpCall(ctx, "PUT", "mirror", "application/json", 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", hhash})
})
}, bytes.NewReader(bodyBytes), int64(len(bodyBytes)), &bd)
if err != nil {
return nil, fmt.Errorf("failed to mirror blob: %w", err)
}
return &bd, nil
}