From 0c00e2e7d93182aba6065c4aabfaef7c5a3effbf Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 9 Feb 2024 15:49:34 -0500 Subject: [PATCH] feat: add proof download support to download endpoint --- api/s5/http.go | 19 ++++++++++++++++++- api/s5/s5.go | 1 - 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/s5/http.go b/api/s5/http.go index 8de7280..aaa3c8d 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -1346,6 +1346,12 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) { } var hashBytes []byte + isProof := false + + if strings.HasSuffix(cid, ".bao") { + isProof = true + cid = strings.TrimSuffix(cid, ".bao") + } cidDecoded, err := encoding.CIDFromString(cid) @@ -1358,7 +1364,6 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) { hashBytes = hashDecoded.HashBytes() } else { - hashBytes = cidDecoded.Hash.HashBytes() } @@ -1376,6 +1381,18 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) { } }(file) + if isProof { + proof, err := file.Proof() + + if jc.Check("error getting proof", err) != nil { + return + } + + jc.ResponseWriter.Header().Set("Content-Type", "application/octet-stream") + http.ServeContent(jc.ResponseWriter, jc.Request, fmt.Sprintf("%.bao", file.Name()), file.Modtime(), bytes.NewReader(proof)) + return + } + jc.ResponseWriter.Header().Set("Content-Type", file.Mime()) http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file) diff --git a/api/s5/s5.go b/api/s5/s5.go index ce5e97f..1c3cb86 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -183,7 +183,6 @@ func getRoutes(s *S5API) map[string]jape.Handler { // Download API "GET /s5/blob/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadBlob, middleware.AuthMiddleware(s.identity, s.accounts)), "GET /s5/metadata/:cid": s.httpHandler.DownloadMetadata, - // "GET /s5/download/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadFile, middleware.AuthMiddleware(portal)), "GET /s5/download/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadFile, cors.Default().Handler), // Pins API