chore(restapi): add headers to support on progress when downloading (#1150)

* Add headers to support on progress on download

* Replace http session by http client in downloadBytes

* Use int instead of int64 for datasetSize

* Rename variable to avoid shallowing client
This commit is contained in:
Arnaud 2025-03-10 16:59:24 +01:00 committed by GitHub
parent 2a3a29720f
commit 703921df32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View File

@ -114,6 +114,8 @@ proc retrieveCid(
else:
resp.setHeader("Content-Disposition", "attachment")
resp.setHeader("Content-Length", $manifest.datasetSize.int)
await resp.prepareChunked()
while not stream.atEof:
@ -342,6 +344,7 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
resp.setCorsHeaders("GET", corsOrigin)
resp.setHeader("Access-Control-Headers", "X-Requested-With")
resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition")
await node.retrieveCid(cid.get(), local = false, resp = resp)
router.api(MethodGet, "/api/codex/v1/data/{cid}/network/manifest") do(

View File

@ -76,15 +76,15 @@ proc downloadNoStream*(client: CodexClient, cid: Cid): ?!string =
proc downloadBytes*(
client: CodexClient, cid: Cid, local = false
): Future[?!seq[byte]] {.async.} =
let uri =
parseUri(client.baseurl & "/data/" & $cid & (if local: "" else: "/network/stream"))
let uri = client.baseurl & "/data/" & $cid & (if local: "" else: "/network/stream")
let (status, bytes) = await client.session.fetch(uri)
let httpClient = newHttpClient()
let response = httpClient.get(uri)
if status != 200:
return failure("fetch failed with status " & $status)
if response.status != "200 OK":
return failure("fetch failed with status " & $response.status)
success bytes
success response.body.toBytes
proc delete*(client: CodexClient, cid: Cid): ?!void =
let