From 92d1a5b671429ea884ede82462b22571b0bc6ff6 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 28 Oct 2024 17:30:25 -0300 Subject: [PATCH] blossom: implement bud06 (upload requirements). --- blossom/handlers.go | 23 +++++++++++++++++++++++ blossom/server.go | 14 ++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/blossom/handlers.go b/blossom/handlers.go index 45d4f02..65567e7 100644 --- a/blossom/handlers.go +++ b/blossom/handlers.go @@ -264,6 +264,29 @@ func (bs BlossomServer) handleDelete(w http.ResponseWriter, r *http.Request) { } } +func (bs BlossomServer) handleUploadCheck(w http.ResponseWriter, r *http.Request) { + auth, err := readAuthorization(r) + if err != nil { + http.Error(w, err.Error(), 400) + return + } + + mimetype := r.Header.Get("X-Content-Type") + exts, _ := mime.ExtensionsByType(mimetype) + var ext string + if len(exts) > 0 { + ext = exts[0] + } + + for _, rb := range bs.RejectUpload { + reject, reason, code := rb(r.Context(), auth, ext) + if reject { + http.Error(w, reason, code) + return + } + } +} + func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) { } diff --git a/blossom/server.go b/blossom/server.go index 837bccf..de6fc9b 100644 --- a/blossom/server.go +++ b/blossom/server.go @@ -32,10 +32,16 @@ func New(rl *khatru.Relay, serviceURL string) *BlossomServer { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/upload" && r.Method == "PUT" { - setCors(w) - bs.handleUpload(w, r) - return + if r.URL.Path == "/upload" { + if r.Method == "PUT" { + setCors(w) + bs.handleUpload(w, r) + return + } else if r.Method == "HEAD" { + setCors(w) + bs.handleUploadCheck(w, r) + return + } } if strings.HasPrefix(r.URL.Path, "/list/") && r.Method == "GET" {