Skip to content

Commit

Permalink
http,ipfs: support raw blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Mar 4, 2024
1 parent 0ff4fe1 commit e1abcdb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
62 changes: 55 additions & 7 deletions http/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ func (is *ipfsGatewayServer) fetchAllowed(ctx context.Context, c cid.Cid) bool {
return false
}

func buildDispositionHeader(params url.Values) string {
disposition := "inline"
if download, ok := params["download"]; ok && strings.EqualFold(download[0], "true") {
disposition = "attachment"
}
if filename, ok := params["filename"]; ok {
disposition += "; filename=" + filename[0]
}
return disposition

}

Check failure on line 110 in http/ipfs.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

unnecessary trailing newline (whitespace)

func (is *ipfsGatewayServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand All @@ -123,17 +135,53 @@ func (is *ipfsGatewayServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

query, _ := url.ParseQuery(r.URL.RawQuery)

w.Header().Set("Content-Disposition", buildDispositionHeader(query))

var queryFormat string
if len(query["format"]) > 0 {
queryFormat = query["format"][0]
}

var format string
switch {
case strings.EqualFold(r.Header.Get("Accept"), "application/vnd.ipld.raw"), strings.EqualFold(queryFormat, "raw"):
format = "raw"
case strings.EqualFold(r.Header.Get("Accept"), "application/vnd.ipld.car"), strings.EqualFold(queryFormat, "car"):
format = "car"
}

log := is.log.Named("serve").With(zap.Stringer("cid", c), zap.Strings("path", path))
log.Info("serving content")

rsc, err := is.ipfs.DownloadUnixFile(ctx, c, path)
if err != nil {
http.Error(w, "", http.StatusNotFound)
is.log.Error("failed to download cid", zap.Error(err))
return
switch format {
case "car":
w.Header().Set("Content-Type", "application/vnd.ipld.car")
w.WriteHeader(http.StatusNotImplemented)
case "raw":
w.Header().Set("Content-Type", "application/vnd.ipld.raw")

ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()

block, err := is.ipfs.GetBlock(ctx, c)
if err != nil {
is.log.Error("failed to get block", zap.Error(err))
http.Error(w, "unable to get block", http.StatusNotFound)
return
}
w.Write(block.RawData())
default:
rsc, err := is.ipfs.DownloadUnixFile(ctx, c, path)
if err != nil {
http.Error(w, "", http.StatusNotFound)
is.log.Error("failed to download cid", zap.Error(err))
return
}
defer rsc.Close()
http.ServeContent(w, r, "", time.Now(), rsc)
}
defer rsc.Close()
http.ServeContent(w, r, "", time.Now(), rsc)
}

// NewIPFSGatewayHandler creates a new http.Handler for the IPFS gateway.
Expand Down
12 changes: 8 additions & 4 deletions ipfs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/provider"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
format "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -61,8 +60,8 @@ func (n *Node) Close() error {
}

// GetBlock fetches a block from the IPFS network
func (n *Node) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
return n.blockService.GetBlock(ctx, c)
func (n *Node) GetBlock(ctx context.Context, c cid.Cid) (format.Node, error) {
return n.dagService.Get(ctx, c)
}

// HasBlock checks if a block is locally pinned
Expand All @@ -87,7 +86,12 @@ func (n *Node) Peers() []peer.ID {

// AddPeer adds a peer to the peerstore
func (n *Node) AddPeer(addr peer.AddrInfo) {
n.host.Peerstore().AddAddrs(addr.ID, addr.Addrs, peerstore.PermanentAddrTTL)
n.host.Peerstore().AddAddrs(addr.ID, addr.Addrs, peerstore.AddressTTL)
}

// Pin pins a CID
func (n *Node) Pin(ctx context.Context, c cid.Cid) error {
return nil
}

func mustParsePeer(s string) peer.AddrInfo {
Expand Down

0 comments on commit e1abcdb

Please sign in to comment.