@cryptotaxi247 / kubo / commits / 33843bfe3

fix: honor url filename when downloading as CAR/BLOCK

Henrique Dias committed Jun 9, 2022 at 14:55 UTC 33843bfe3a577ee3425cd81733bd79f8d4b8a92b
4 files changed +24 -2
core/corehttp/gateway_handler_block.go
+6 -1
@@ -31,7 +31,12 @@ func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWrite
31 content := bytes.NewReader(block)
32
33 // Set Content-Disposition
34 - name := blockCid.String() + ".bin"
34 + var name string
35 + if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
36 + name = urlFilename
37 + } else {
38 + name = blockCid.String() + ".bin"
39 + }
40 setContentDispositionHeader(w, name, "attachment")
41
42 // Set remaining headers
core/corehttp/gateway_handler_car.go
+6 -1
@@ -35,7 +35,12 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
35 rootCid := resolvedPath.Cid()
36
37 // Set Content-Disposition
38 - name := rootCid.String() + ".car"
38 + var name string
39 + if urlFilename := r.URL.Query().Get("filename"); urlFilename != "" {
40 + name = urlFilename
41 + } else {
42 + name = rootCid.String() + ".car"
43 + }
44 setContentDispositionHeader(w, name, "attachment")
45
46 // Weak Etag W/ because we can't guarantee byte-for-byte identical responses
test/sharness/t0117-gateway-block.sh
+6
@@ -49,6 +49,12 @@ test_expect_success "Create text fixtures" '
49 grep "< X-Content-Type-Options: nosniff" curl_output
50 '
51
52 + test_expect_success "GET for application/vnd.ipld.raw with query filename includes Content-Disposition with custom filename" '
53 + curl -svX GET -H "Accept: application/vnd.ipld.raw" "http://127.0.0.1:$GWAY_PORT/ipfs/$ROOT_DIR_CID/dir/ascii.txt?filename=foobar.bin" >/dev/null 2>curl_output_filename &&
54 + cat curl_output_filename &&
55 + grep "< Content-Disposition: attachment\; filename=\"foobar.bin\"" curl_output_filename
56 + '
57 +
58 # Cache control HTTP headers
59 # (basic checks, detailed behavior is tested in t0116-gateway-cache.sh)
60
test/sharness/t0118-gateway-car.sh
+6
@@ -110,6 +110,12 @@ test_launch_ipfs_daemon_without_network
110 grep "< Accept-Ranges: none" curl_output
111 '
112
113 + test_expect_success "GET for application/vnd.ipld.car with query filename includes Content-Disposition with custom filename" '
114 + curl -svX GET -H "Accept: application/vnd.ipld.car" "http://127.0.0.1:$GWAY_PORT/ipfs/$ROOT_DIR_CID/subdir/ascii.txt?filename=foobar.car" > curl_output_filename 2>&1 &&
115 + cat curl_output_filename &&
116 + grep "< Content-Disposition: attachment\; filename=\"foobar.car\"" curl_output_filename
117 + '
118 +
119 # Cache control HTTP headers
120
121 test_expect_success "GET response for application/vnd.ipld.car includes a weak Etag" '