khatru: the accioly blossom redirect patch, reworked.

This commit is contained in:
fiatjaf
2025-06-10 16:43:05 -03:00
parent 50a753504d
commit 027d016d97
4 changed files with 84 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ import (
"mime"
"net/http"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@@ -199,8 +200,21 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) {
ext = "." + spl[1]
}
if nil != bs.LoadBlob {
reader, _ := bs.LoadBlob(r.Context(), hhash)
if bs.LoadBlob != nil {
reader, redirectURL, err := bs.LoadBlob(r.Context(), hhash)
if err == nil && redirectURL != nil {
// check that the redirectURL contains the hash of the file
if ok, _ := regexp.MatchString(`\b`+hhash+`\b`, redirectURL.String()); !ok {
blossomError(w, "redirect url doesn't contain the file hash", 500)
return
}
w.Header().Set("ETag", hhash)
w.Header().Set("Cache-Control", "public, max-age=604800, immutable")
http.Redirect(w, r, redirectURL.String(), http.StatusTemporaryRedirect)
return
}
if reader != nil {
// use unix epoch as the time if we can't find the descriptor
// as described in the http.ServeContent documentation
@@ -211,7 +225,11 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("ETag", hhash)
w.Header().Set("Cache-Control", "public, max-age=604800, immutable")
http.ServeContent(w, r, hhash+ext, t, reader)
name := hhash
if ext != "" {
name += ext
}
http.ServeContent(w, r, name, t, reader)
return
}
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"io"
"net/http"
"net/url"
"strings"
"fiatjaf.com/nostr"
@@ -15,7 +16,7 @@ type BlossomServer struct {
Store BlobIndex
StoreBlob func(ctx context.Context, sha256 string, body []byte) error
LoadBlob func(ctx context.Context, sha256 string) (io.ReadSeeker, error)
LoadBlob func(ctx context.Context, sha256 string) (io.ReadSeeker, *url.URL, error)
DeleteBlob func(ctx context.Context, sha256 string) error
ReceiveReport func(ctx context.Context, reportEvt nostr.Event) error