khatru/blossom: get rid of custom BlobDescriptor with useless Owner field.
This commit is contained in:
@@ -5,22 +5,13 @@ import (
|
|||||||
"iter"
|
"iter"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
|
"fiatjaf.com/nostr/nipb0/blossom"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlobDescriptor struct {
|
|
||||||
URL string `json:"url"`
|
|
||||||
SHA256 string `json:"sha256"`
|
|
||||||
Size int `json:"size"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Uploaded nostr.Timestamp `json:"uploaded"`
|
|
||||||
|
|
||||||
Owner nostr.PubKey `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type BlobIndex interface {
|
type BlobIndex interface {
|
||||||
Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error
|
Keep(ctx context.Context, blob blossom.BlobDescriptor, pubkey nostr.PubKey) error
|
||||||
List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor]
|
List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor]
|
||||||
Get(ctx context.Context, sha256 string) (*BlobDescriptor, error)
|
Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error)
|
||||||
Delete(ctx context.Context, sha256 string, pubkey nostr.PubKey) error
|
Delete(ctx context.Context, sha256 string, pubkey nostr.PubKey) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ type EventStoreBlobIndexWrapper struct {
|
|||||||
ServiceURL string
|
ServiceURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (es EventStoreBlobIndexWrapper) Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error {
|
func (es EventStoreBlobIndexWrapper) Keep(
|
||||||
|
ctx context.Context,
|
||||||
|
blob blossom.BlobDescriptor,
|
||||||
|
pubkey nostr.PubKey,
|
||||||
|
) error {
|
||||||
next, stop := iter.Pull(
|
next, stop := iter.Pull(
|
||||||
es.Store.QueryEvents(nostr.Filter{Authors: []nostr.PubKey{pubkey}, Kinds: []nostr.Kind{24242}, Tags: nostr.TagMap{"x": []string{blob.SHA256}}}, 1),
|
es.Store.QueryEvents(nostr.Filter{Authors: []nostr.PubKey{pubkey}, Kinds: []nostr.Kind{24242}, Tags: nostr.TagMap{"x": []string{blob.SHA256}}}, 1),
|
||||||
)
|
)
|
||||||
@@ -42,8 +46,8 @@ func (es EventStoreBlobIndexWrapper) Keep(ctx context.Context, blob BlobDescript
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor] {
|
func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] {
|
||||||
return func(yield func(BlobDescriptor) bool) {
|
return func(yield func(blossom.BlobDescriptor) bool) {
|
||||||
for evt := range es.Store.QueryEvents(nostr.Filter{
|
for evt := range es.Store.QueryEvents(nostr.Filter{
|
||||||
Authors: []nostr.PubKey{pubkey},
|
Authors: []nostr.PubKey{pubkey},
|
||||||
Kinds: []nostr.Kind{24242},
|
Kinds: []nostr.Kind{24242},
|
||||||
@@ -53,7 +57,7 @@ func (es EventStoreBlobIndexWrapper) List(ctx context.Context, pubkey nostr.PubK
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (es EventStoreBlobIndexWrapper) Get(ctx context.Context, sha256 string) (*BlobDescriptor, error) {
|
func (es EventStoreBlobIndexWrapper) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) {
|
||||||
next, stop := iter.Pull(
|
next, stop := iter.Pull(
|
||||||
es.Store.QueryEvents(nostr.Filter{Tags: nostr.TagMap{"x": []string{sha256}}, Kinds: []nostr.Kind{24242}, Limit: 1}, 1),
|
es.Store.QueryEvents(nostr.Filter{Tags: nostr.TagMap{"x": []string{sha256}}, Kinds: []nostr.Kind{24242}, Limit: 1}, 1),
|
||||||
)
|
)
|
||||||
@@ -87,14 +91,13 @@ func (es EventStoreBlobIndexWrapper) Delete(ctx context.Context, sha256 string,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) BlobDescriptor {
|
func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) blossom.BlobDescriptor {
|
||||||
hhash := evt.Tags[0][1]
|
hhash := evt.Tags[0][1]
|
||||||
mimetype := evt.Tags[1][1]
|
mimetype := evt.Tags[1][1]
|
||||||
ext := blossom.GetExtension(mimetype)
|
ext := blossom.GetExtension(mimetype)
|
||||||
size, _ := strconv.Atoi(evt.Tags[2][1])
|
size, _ := strconv.Atoi(evt.Tags[2][1])
|
||||||
|
|
||||||
return BlobDescriptor{
|
return blossom.BlobDescriptor{
|
||||||
Owner: evt.PubKey,
|
|
||||||
Uploaded: evt.CreatedAt,
|
Uploaded: evt.CreatedAt,
|
||||||
URL: es.ServiceURL + "/" + hhash + ext,
|
URL: es.ServiceURL + "/" + hhash + ext,
|
||||||
SHA256: hhash,
|
SHA256: hhash,
|
||||||
|
|||||||
@@ -36,11 +36,7 @@ func (bs BlossomServer) handleUploadCheck(w http.ResponseWriter, r *http.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
mimetype := r.Header.Get("X-Content-Type")
|
mimetype := r.Header.Get("X-Content-Type")
|
||||||
exts, _ := mime.ExtensionsByType(mimetype)
|
ext := blossom.GetExtension(mimetype)
|
||||||
var ext string
|
|
||||||
if len(exts) > 0 {
|
|
||||||
ext = exts[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the file size from the incoming header
|
// get the file size from the incoming header
|
||||||
size, _ := strconv.Atoi(r.Header.Get("X-Content-Length"))
|
size, _ := strconv.Atoi(r.Header.Get("X-Content-Length"))
|
||||||
@@ -135,7 +131,7 @@ func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keep track of the blob descriptor
|
// keep track of the blob descriptor
|
||||||
bd := BlobDescriptor{
|
bd := blossom.BlobDescriptor{
|
||||||
URL: bs.ServiceURL + "/" + hhash + ext,
|
URL: bs.ServiceURL + "/" + hhash + ext,
|
||||||
SHA256: hhash,
|
SHA256: hhash,
|
||||||
Size: len(b),
|
Size: len(b),
|
||||||
@@ -472,7 +468,7 @@ func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keep track of the blob descriptor
|
// keep track of the blob descriptor
|
||||||
bd := BlobDescriptor{
|
bd := blossom.BlobDescriptor{
|
||||||
URL: bs.ServiceURL + "/" + hhash + ext,
|
URL: bs.ServiceURL + "/" + hhash + ext,
|
||||||
SHA256: hhash,
|
SHA256: hhash,
|
||||||
Size: len(body),
|
Size: len(body),
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
|
"fiatjaf.com/nostr/nipb0/blossom"
|
||||||
"github.com/puzpuzpuz/xsync/v3"
|
"github.com/puzpuzpuz/xsync/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ownedBlob struct {
|
type ownedBlob struct {
|
||||||
blob BlobDescriptor
|
blob blossom.BlobDescriptor
|
||||||
owners []nostr.PubKey
|
owners []nostr.PubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ func NewMemoryBlobIndex() MemoryBlobIndex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x MemoryBlobIndex) Keep(ctx context.Context, blob BlobDescriptor, pubkey nostr.PubKey) error {
|
func (x MemoryBlobIndex) Keep(ctx context.Context, blob blossom.BlobDescriptor, pubkey nostr.PubKey) error {
|
||||||
x.m.Compute(blob.SHA256, func(oldValue ownedBlob, loaded bool) (newValue ownedBlob, delete bool) {
|
x.m.Compute(blob.SHA256, func(oldValue ownedBlob, loaded bool) (newValue ownedBlob, delete bool) {
|
||||||
if loaded {
|
if loaded {
|
||||||
newValue = oldValue
|
newValue = oldValue
|
||||||
@@ -45,10 +46,10 @@ func (x MemoryBlobIndex) Keep(ctx context.Context, blob BlobDescriptor, pubkey n
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[BlobDescriptor] {
|
func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq[blossom.BlobDescriptor] {
|
||||||
return func(yield func(BlobDescriptor) bool) {
|
return func(yield func(blossom.BlobDescriptor) bool) {
|
||||||
x.m.Range(func(key string, value ownedBlob) bool {
|
x.m.Range(func(key string, value ownedBlob) bool {
|
||||||
if value.blob.Owner == value.owners[0] {
|
if slices.Contains(value.owners, pubkey) {
|
||||||
if !yield(value.blob) {
|
if !yield(value.blob) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -58,9 +59,8 @@ func (x MemoryBlobIndex) List(ctx context.Context, pubkey nostr.PubKey) iter.Seq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x MemoryBlobIndex) Get(ctx context.Context, sha256 string) (*BlobDescriptor, error) {
|
func (x MemoryBlobIndex) Get(ctx context.Context, sha256 string) (*blossom.BlobDescriptor, error) {
|
||||||
if val, ok := x.m.Load(sha256); ok {
|
if val, ok := x.m.Load(sha256); ok {
|
||||||
val.blob.Owner = val.owners[0]
|
|
||||||
return &val.blob, nil
|
return &val.blob, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("not found")
|
return nil, errors.New("not found")
|
||||||
|
|||||||
Reference in New Issue
Block a user