feat: add proof download support to download endpoint

This commit is contained in:
Derrick Hammer 2024-02-09 15:49:34 -05:00
parent 850b575e1c
commit 0c00e2e7d9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 18 additions and 2 deletions

View File

@ -1346,6 +1346,12 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) {
} }
var hashBytes []byte var hashBytes []byte
isProof := false
if strings.HasSuffix(cid, ".bao") {
isProof = true
cid = strings.TrimSuffix(cid, ".bao")
}
cidDecoded, err := encoding.CIDFromString(cid) cidDecoded, err := encoding.CIDFromString(cid)
@ -1358,7 +1364,6 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) {
hashBytes = hashDecoded.HashBytes() hashBytes = hashDecoded.HashBytes()
} else { } else {
hashBytes = cidDecoded.Hash.HashBytes() hashBytes = cidDecoded.Hash.HashBytes()
} }
@ -1376,6 +1381,18 @@ func (h *HttpHandler) DownloadFile(jc jape.Context) {
} }
}(file) }(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()) jc.ResponseWriter.Header().Set("Content-Type", file.Mime())
http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file) http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file)

View File

@ -183,7 +183,6 @@ func getRoutes(s *S5API) map[string]jape.Handler {
// Download API // Download API
"GET /s5/blob/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadBlob, middleware.AuthMiddleware(s.identity, s.accounts)), "GET /s5/blob/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadBlob, middleware.AuthMiddleware(s.identity, s.accounts)),
"GET /s5/metadata/:cid": s.httpHandler.DownloadMetadata, "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), "GET /s5/download/:cid": middleware.ApplyMiddlewares(s.httpHandler.DownloadFile, cors.Default().Handler),
// Pins API // Pins API