Documentation
¶
Index ¶
- Constants
- func BuildBlobURL(baseURL, sha256Hex, ext string) string
- func CalculateSHA256(data []byte) []byte
- func CalculateSHA256Hex(data []byte) string
- func DetectMimeType(contentType string, ext string) string
- func ExtractAuthEvent(r *http.Request) (ev *event.E, err error)
- func ExtractSHA256FromPath(path string) (sha256Hex string, ext string, err error)
- func ExtractSHA256FromURL(urlStr string) (sha256Hex string, err error)
- func GetExtensionFromMimeType(mimeType string) string
- func GetFileExtensionFromPath(path string) string
- func GetMimeTypeFromExtension(ext string) string
- func GetPubkeyFromRequest(r *http.Request) (pubkey []byte, err error)
- func OptimizeMedia(data []byte, mimeType string) (optimizedData []byte, optimizedMimeType string)
- func ParseRangeHeader(rangeHeader string, contentLength int64) (start, end int64, valid bool, err error)
- func SetPaymentRequired(w http.ResponseWriter, paymentHeaders map[string]string)
- func ValidateSHA256Hex(s string) bool
- func WriteRangeResponse(w http.ResponseWriter, data []byte, start, end, totalLength int64)
- type AuthEvent
- func ValidateAuthEvent(r *http.Request, verb string, sha256Hash []byte) (authEv *AuthEvent, err error)
- func ValidateAuthEventForGet(r *http.Request, serverURL string, sha256Hash []byte) (authEv *AuthEvent, err error)
- func ValidateAuthEventOptional(r *http.Request, verb string, sha256Hash []byte) (authEv *AuthEvent, err error)
- type BandwidthLimiter
- func (bl *BandwidthLimiter) CheckAndConsume(identity string, sizeBytes int64) bool
- func (bl *BandwidthLimiter) Cleanup()
- func (bl *BandwidthLimiter) GetAvailable(identity string) int64
- func (bl *BandwidthLimiter) GetTimeUntilAvailable(identity string, sizeBytes int64) time.Duration
- func (bl *BandwidthLimiter) Stats() int
- type BandwidthState
- type BaseURLKey
- type BlobDescriptor
- type BlobMetadata
- type Config
- type PaymentChecker
- type Server
- type Storage
- func (s *Storage) DeleteBlob(sha256Hash []byte, pubkey []byte) error
- func (s *Storage) GetBlob(sha256Hash []byte) (data []byte, metadata *BlobMetadata, err error)
- func (s *Storage) GetBlobMetadata(sha256Hash []byte) (*BlobMetadata, error)
- func (s *Storage) GetTotalStorageUsed(pubkey []byte) (totalMB int64, err error)
- func (s *Storage) HasBlob(sha256Hash []byte) (exists bool, err error)
- func (s *Storage) ListAllUserStats() ([]*UserBlobStats, error)
- func (s *Storage) ListBlobs(pubkey []byte, since, until int64) ([]*BlobDescriptor, error)
- func (s *Storage) SaveBlob(sha256Hash []byte, data []byte, pubkey []byte, mimeType string, ...) error
- func (s *Storage) SaveReport(sha256Hash []byte, reportData []byte) error
- type UserBlobStats
Constants ¶
const ( // BlossomAuthKind is the Nostr event kind for Blossom authorization events (BUD-01) BlossomAuthKind = 24242 // AuthorizationHeader is the HTTP header name for authorization AuthorizationHeader = "Authorization" // NostrAuthPrefix is the prefix for Nostr authorization scheme NostrAuthPrefix = "Nostr" )
Variables ¶
This section is empty.
Functions ¶
func BuildBlobURL ¶
BuildBlobURL builds a blob URL with optional extension
func CalculateSHA256 ¶
CalculateSHA256 calculates the SHA256 hash of data
func CalculateSHA256Hex ¶
CalculateSHA256Hex calculates the SHA256 hash and returns it as hex string
func DetectMimeType ¶
DetectMimeType detects MIME type from Content-Type header or file extension
func ExtractAuthEvent ¶
ExtractAuthEvent extracts and parses a kind 24242 authorization event from the Authorization header
func ExtractSHA256FromPath ¶
ExtractSHA256FromPath extracts SHA256 hash from URL path Supports both /<sha256> and /<sha256>.<ext> formats
func ExtractSHA256FromURL ¶
ExtractSHA256FromURL extracts SHA256 hash from a URL string Uses the last occurrence of a 64 char hex string (as per BUD-03)
func GetExtensionFromMimeType ¶
GetExtensionFromMimeType returns file extension based on MIME type
func GetFileExtensionFromPath ¶
GetFileExtensionFromPath extracts file extension from a path
func GetMimeTypeFromExtension ¶
GetMimeTypeFromExtension returns MIME type based on file extension
func GetPubkeyFromRequest ¶
GetPubkeyFromRequest extracts pubkey from Authorization header if present
func OptimizeMedia ¶
OptimizeMedia optimizes media content (BUD-05) This is a placeholder implementation - actual optimization would use libraries like image processing, video encoding, etc.
func ParseRangeHeader ¶
func ParseRangeHeader(rangeHeader string, contentLength int64) ( start, end int64, valid bool, err error, )
ParseRangeHeader parses HTTP Range header (RFC 7233) Returns start, end, and total length
func SetPaymentRequired ¶
func SetPaymentRequired(w http.ResponseWriter, paymentHeaders map[string]string)
SetPaymentRequired sets a 402 Payment Required response with payment headers
func ValidateSHA256Hex ¶
ValidateSHA256Hex validates that a string is a valid SHA256 hex string
func WriteRangeResponse ¶
func WriteRangeResponse( w http.ResponseWriter, data []byte, start, end, totalLength int64, )
WriteRangeResponse writes a partial content response (206)
Types ¶
type AuthEvent ¶
AuthEvent represents a validated authorization event
func ValidateAuthEvent ¶
func ValidateAuthEvent( r *http.Request, verb string, sha256Hash []byte, ) (authEv *AuthEvent, err error)
ValidateAuthEvent validates a kind 24242 authorization event according to BUD-01
func ValidateAuthEventForGet ¶
func ValidateAuthEventForGet( r *http.Request, serverURL string, sha256Hash []byte, ) (authEv *AuthEvent, err error)
ValidateAuthEventForGet validates authorization for GET requests (BUD-01) GET requests may have either: - A server tag matching the server URL - At least one x tag matching the blob hash
func ValidateAuthEventOptional ¶
func ValidateAuthEventOptional( r *http.Request, verb string, sha256Hash []byte, ) (authEv *AuthEvent, err error)
ValidateAuthEventOptional validates authorization but returns nil if no auth header is present This is used for endpoints where authorization is optional
type BandwidthLimiter ¶ added in v0.49.0
type BandwidthLimiter struct {
// contains filtered or unexported fields
}
BandwidthLimiter implements token bucket rate limiting for uploads. Each identity gets a bucket that replenishes at dailyLimit/day rate. Uploads consume tokens from the bucket.
func NewBandwidthLimiter ¶ added in v0.49.0
func NewBandwidthLimiter(dailyLimitMB, burstLimitMB int64) *BandwidthLimiter
NewBandwidthLimiter creates a new bandwidth limiter. dailyLimitMB is the average daily limit in megabytes. burstLimitMB is the maximum burst capacity in megabytes.
func (*BandwidthLimiter) CheckAndConsume ¶ added in v0.49.0
func (bl *BandwidthLimiter) CheckAndConsume(identity string, sizeBytes int64) bool
CheckAndConsume checks if an upload of the given size is allowed for the identity, and if so, consumes the tokens. Returns true if allowed, false if rate limited. The identity should be pubkey hex for authenticated users, or IP for anonymous.
func (*BandwidthLimiter) Cleanup ¶ added in v0.49.0
func (bl *BandwidthLimiter) Cleanup()
Cleanup removes entries that have fully replenished (at burst limit).
func (*BandwidthLimiter) GetAvailable ¶ added in v0.49.0
func (bl *BandwidthLimiter) GetAvailable(identity string) int64
GetAvailable returns the currently available bytes for an identity.
func (*BandwidthLimiter) GetTimeUntilAvailable ¶ added in v0.49.0
func (bl *BandwidthLimiter) GetTimeUntilAvailable(identity string, sizeBytes int64) time.Duration
GetTimeUntilAvailable returns how long until the given bytes will be available.
func (*BandwidthLimiter) Stats ¶ added in v0.49.0
func (bl *BandwidthLimiter) Stats() int
Stats returns the number of tracked identities.
type BandwidthState ¶ added in v0.49.0
type BandwidthState struct {
BucketBytes int64 // Current token bucket level (bytes available)
LastUpdate time.Time // Last time bucket was updated
}
BandwidthState tracks upload bandwidth for an identity
type BaseURLKey ¶ added in v0.36.12
type BaseURLKey struct{}
BaseURLKey is the context key for the base URL (exported for use by app handler)
type BlobDescriptor ¶
type BlobDescriptor struct {
URL string `json:"url"`
SHA256 string `json:"sha256"`
Size int64 `json:"size"`
Type string `json:"type"`
Uploaded int64 `json:"uploaded"`
NIP94 [][]string `json:"nip94,omitempty"`
}
BlobDescriptor represents a blob descriptor as defined in BUD-02
type BlobMetadata ¶
type BlobMetadata struct {
Pubkey []byte `json:"pubkey"`
MimeType string `json:"mime_type"`
Uploaded int64 `json:"uploaded"`
Size int64 `json:"size"`
Extension string `json:"extension"` // File extension (e.g., ".png", ".pdf")
}
BlobMetadata stores metadata about a blob in the database
func DeserializeBlobMetadata ¶
func DeserializeBlobMetadata(data []byte) (bm *BlobMetadata, err error)
DeserializeBlobMetadata deserializes blob metadata from JSON
func NewBlobMetadata ¶
func NewBlobMetadata(pubkey []byte, mimeType string, size int64) *BlobMetadata
NewBlobMetadata creates a new blob metadata struct
func (*BlobMetadata) Serialize ¶
func (bm *BlobMetadata) Serialize() (data []byte, err error)
Serialize serializes blob metadata to JSON
type Config ¶
type Config struct {
BaseURL string
MaxBlobSize int64
AllowedMimeTypes []string
RequireAuth bool
// Rate limiting (for non-followed users)
RateLimitEnabled bool
DailyLimitMB int64
BurstLimitMB int64
}
Config holds configuration for the Blossom server
type PaymentChecker ¶
type PaymentChecker struct {
}
PaymentChecker handles payment requirements (BUD-07)
func NewPaymentChecker ¶
func NewPaymentChecker() *PaymentChecker
NewPaymentChecker creates a new payment checker
func (*PaymentChecker) CheckPaymentRequired ¶
func (pc *PaymentChecker) CheckPaymentRequired( endpoint string, ) (required bool, paymentHeaders map[string]string)
CheckPaymentRequired checks if payment is required for an endpoint Returns payment method headers if payment is required
func (*PaymentChecker) ValidatePayment ¶
func (pc *PaymentChecker) ValidatePayment( paymentMethod, proof string, ) (valid bool, err error)
ValidatePayment validates a payment proof
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server provides a Blossom server implementation
func NewServer ¶
NewServer creates a new Blossom server instance
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage provides blob storage operations using the database interface. This is a thin wrapper that delegates to the database's blob methods.
func NewStorage ¶
NewStorage creates a new storage instance using the database interface.
func (*Storage) DeleteBlob ¶
DeleteBlob deletes a blob and its metadata.
func (*Storage) GetBlob ¶
func (s *Storage) GetBlob(sha256Hash []byte) (data []byte, metadata *BlobMetadata, err error)
GetBlob retrieves blob data by SHA256 hash. Returns the data and metadata from the database.
func (*Storage) GetBlobMetadata ¶
func (s *Storage) GetBlobMetadata(sha256Hash []byte) (*BlobMetadata, error)
GetBlobMetadata retrieves only metadata for a blob.
func (*Storage) GetTotalStorageUsed ¶
GetTotalStorageUsed calculates total storage used by a pubkey in MB.
func (*Storage) HasBlob ¶
HasBlob checks if a blob exists.
func (*Storage) ListAllUserStats ¶ added in v0.36.21
func (s *Storage) ListAllUserStats() ([]*UserBlobStats, error)
ListAllUserStats returns storage statistics for all users who have uploaded blobs.
func (*Storage) ListBlobs ¶
func (s *Storage) ListBlobs(pubkey []byte, since, until int64) ([]*BlobDescriptor, error)
ListBlobs lists all blobs for a given pubkey. Returns blob descriptors with time filtering.
type UserBlobStats ¶ added in v0.36.21
type UserBlobStats struct {
PubkeyHex string `json:"pubkey"`
BlobCount int64 `json:"blob_count"`
TotalSizeBytes int64 `json:"total_size_bytes"`
}
UserBlobStats represents storage statistics for a single user. This mirrors database.UserBlobStats for API compatibility.
Source Files
¶
- auth.go
- blob.go
- handlers.go
- media.go
- payment.go
- ratelimit.go
- server.go
- storage.go
- utils.go