Skip to content

Commit

Permalink
Change API of LoadBlob to return io.ReadSeeker instead of io.Reader a…
Browse files Browse the repository at this point in the history
…nd use ServeContent to serve blob content (#19)

* first test with http.ServeContent

* added debug to help me here

* removed debug messages

* changed LoadBlob to requisre io.ReadSeeker

---------

Co-authored-by: Your Name <[email protected]>
  • Loading branch information
girino and Your Name authored Nov 21, 2024
1 parent 76ecf4f commit 3f26a1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions blossom/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/liamg/magic"
"github.com/nbd-wtf/go-nostr"
Expand Down Expand Up @@ -190,8 +191,14 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) {
for _, lb := range bs.LoadBlob {
reader, _ := lb(r.Context(), hhash)
if reader != nil {
w.Header().Add("Content-Type", mime.TypeByExtension(ext))
io.Copy(w, reader)
// use unix epoch as the time if we can't find the descriptor
// as described in the http.ServeContent documentation
t := time.Unix(0, 0)
descriptor, err := bs.Store.Get(r.Context(), hhash)
if err == nil && descriptor != nil {
t = descriptor.Uploaded.Time()
}
http.ServeContent(w, r, hhash+ext, t, reader)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion blossom/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type BlossomServer struct {
Store BlobIndex

StoreBlob []func(ctx context.Context, sha256 string, body []byte) error
LoadBlob []func(ctx context.Context, sha256 string) (io.Reader, error)
LoadBlob []func(ctx context.Context, sha256 string) (io.ReadSeeker, error)
DeleteBlob []func(ctx context.Context, sha256 string) error

RejectUpload []func(ctx context.Context, auth *nostr.Event, size int, ext string) (bool, string, int)
Expand Down

0 comments on commit 3f26a1f

Please sign in to comment.