blob.go raw

   1  package blossom
   2  
   3  import (
   4  	"encoding/json"
   5  	"time"
   6  )
   7  
   8  // BlobDescriptor represents a blob descriptor as defined in BUD-02
   9  type BlobDescriptor struct {
  10  	URL      string     `json:"url"`
  11  	SHA256   string     `json:"sha256"`
  12  	Size     int64      `json:"size"`
  13  	Type     string     `json:"type"`
  14  	Uploaded int64      `json:"uploaded"`
  15  	NIP94    [][]string `json:"nip94,omitempty"`
  16  }
  17  
  18  // BlobMetadata stores metadata about a blob in the database
  19  type BlobMetadata struct {
  20  	Pubkey    []byte `json:"pubkey"`
  21  	MimeType  string `json:"mime_type"`
  22  	Uploaded  int64  `json:"uploaded"`
  23  	Size      int64  `json:"size"`
  24  	Extension string `json:"extension"` // File extension (e.g., ".png", ".pdf")
  25  }
  26  
  27  // NewBlobDescriptor creates a new blob descriptor
  28  func NewBlobDescriptor(
  29  	url, sha256 string, size int64, mimeType string, uploaded int64,
  30  ) *BlobDescriptor {
  31  	if mimeType == "" {
  32  		mimeType = "application/octet-stream"
  33  	}
  34  	return &BlobDescriptor{
  35  		URL:      url,
  36  		SHA256:   sha256,
  37  		Size:     size,
  38  		Type:     mimeType,
  39  		Uploaded: uploaded,
  40  	}
  41  }
  42  
  43  // NewBlobMetadata creates a new blob metadata struct
  44  func NewBlobMetadata(pubkey []byte, mimeType string, size int64) *BlobMetadata {
  45  	if mimeType == "" {
  46  		mimeType = "application/octet-stream"
  47  	}
  48  	return &BlobMetadata{
  49  		Pubkey:    pubkey,
  50  		MimeType:  mimeType,
  51  		Uploaded:  time.Now().Unix(),
  52  		Size:      size,
  53  		Extension: "", // Will be set by SaveBlob
  54  	}
  55  }
  56  
  57  // Serialize serializes blob metadata to JSON
  58  func (bm *BlobMetadata) Serialize() (data []byte, err error) {
  59  	return json.Marshal(bm)
  60  }
  61  
  62  // DeserializeBlobMetadata deserializes blob metadata from JSON
  63  func DeserializeBlobMetadata(data []byte) (bm *BlobMetadata, err error) {
  64  	bm = &BlobMetadata{}
  65  	err = json.Unmarshal(data, bm)
  66  	return
  67  }
  68