2024-10-27 17:20:10 -03:00
|
|
|
package blossom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-29 09:00:45 -03:00
|
|
|
"io"
|
2024-11-05 18:55:47 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-10-27 17:20:10 -03:00
|
|
|
|
|
|
|
"github.com/fiatjaf/khatru"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlossomServer struct {
|
|
|
|
ServiceURL string
|
2024-10-27 23:00:51 -03:00
|
|
|
Store BlobIndex
|
2024-10-27 17:20:10 -03:00
|
|
|
|
2025-02-02 00:33:17 +03:30
|
|
|
StoreBlob []func(ctx context.Context, sha256 string, body []byte) error
|
|
|
|
LoadBlob []func(ctx context.Context, sha256 string) (io.ReadSeeker, error)
|
|
|
|
DeleteBlob []func(ctx context.Context, sha256 string) error
|
|
|
|
ReceiveReport []func(ctx context.Context, reportEvt *nostr.Event) error
|
2024-10-27 17:20:10 -03:00
|
|
|
|
2024-10-29 09:00:45 -03:00
|
|
|
RejectUpload []func(ctx context.Context, auth *nostr.Event, size int, ext string) (bool, string, int)
|
2024-10-28 17:30:07 -03:00
|
|
|
RejectGet []func(ctx context.Context, auth *nostr.Event, sha256 string) (bool, string, int)
|
|
|
|
RejectList []func(ctx context.Context, auth *nostr.Event, pubkey string) (bool, string, int)
|
|
|
|
RejectDelete []func(ctx context.Context, auth *nostr.Event, sha256 string) (bool, string, int)
|
2024-10-27 17:20:10 -03:00
|
|
|
}
|
|
|
|
|
2024-10-27 23:00:51 -03:00
|
|
|
func New(rl *khatru.Relay, serviceURL string) *BlossomServer {
|
2024-10-27 17:20:10 -03:00
|
|
|
bs := &BlossomServer{
|
|
|
|
ServiceURL: serviceURL,
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:55:47 +00:00
|
|
|
base := rl.Router()
|
|
|
|
mux := http.NewServeMux()
|
2024-10-27 17:20:10 -03:00
|
|
|
|
2024-11-05 18:55:47 +00:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/upload" {
|
|
|
|
if r.Method == "PUT" {
|
|
|
|
bs.handleUpload(w, r)
|
|
|
|
return
|
|
|
|
} else if r.Method == "HEAD" {
|
|
|
|
bs.handleUploadCheck(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/list/") && r.Method == "GET" {
|
|
|
|
bs.handleList(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.SplitN(r.URL.Path, ".", 2)[0]) == 65 {
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
bs.handleHasBlob(w, r)
|
|
|
|
return
|
|
|
|
} else if r.Method == "GET" {
|
|
|
|
bs.handleGetBlob(w, r)
|
|
|
|
return
|
|
|
|
} else if r.Method == "DELETE" {
|
|
|
|
bs.handleDelete(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 00:33:17 +03:30
|
|
|
if r.URL.Path == "/report" {
|
|
|
|
if r.Method == "PUT" {
|
|
|
|
bs.handleReport(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 18:55:47 +00:00
|
|
|
base.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
rl.SetRouter(mux)
|
2024-10-27 17:20:10 -03:00
|
|
|
|
|
|
|
return bs
|
|
|
|
}
|