diff --git a/codex/node.nim b/codex/node.nim index d303a01c..482ed6f9 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -78,8 +78,7 @@ type # This will go into a download manager, likely persisted to disk # so download sessions can be live across node shutdowns. - downloads*: Table[uint64, Swarm] - downloadCounter: uint64 + downloads*: Table[Cid, Swarm] CodexNodeRef* = ref CodexNode @@ -100,28 +99,24 @@ func discovery*(self: CodexNodeRef): Discovery = proc startDownload*( self: CodexNodeRef, manifest: Manifest -): Future[uint64] {.async: (raises: []).} = +): Future[Cid] {.async: (raises: []).} = ## Start a download for a manifest ## # This won't play nice with multiple downloads as the Swarm will install # its own callbacks and listeners, but for experimentation is fine. - let - id = self.downloadCounter - swarm = Swarm.new( - manifest, self.networkStore.localStore, self.engine.network, self.discovery - ) - - self.downloadCounter += 1 - self.downloads[id] = swarm + let swarm = Swarm.new( + manifest, self.networkStore.localStore, self.engine.network, self.discovery + ) + self.downloads[manifest.treeCid] = swarm await swarm.start() - return id + return manifest.treeCid -proc downloadStatus*(self: CodexNodeRef, id: uint64): ?(int, int) = +proc downloadStatus*(self: CodexNodeRef, dataset: Cid): ?(int, int) = try: - return self.downloads[id].downloadStatus().some() + return self.downloads[dataset].downloadStatus().some() except KeyError: return none((int, int)) diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 1e2c4a3b..3b34aecc 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -207,18 +207,15 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute info "Starting download for manifest", manifest = manifest.cid, treeCid = manifest.manifest.treeCid - let downloadId = await node.startDownload(manifest.manifest) - - info "Download id assigned", downloadId = downloadId - + let treeCid = await node.startDownload(manifest.manifest) return RestApiResponse.response( - $ %*{"downloadId": $downloadId}, contentType = "application/json" + $ %*{"downloadId": $treeCid}, contentType = "application/json" ) - router.rawApi(MethodGet, "/api/codex/v1/download/{id}") do( - id: uint64 + router.rawApi(MethodGet, "/api/codex/v1/download/{cid}") do( + cid: Cid ) -> RestApiResponse: - if (downloaded, total) =? node.downloadStatus(id.get()): + if (downloaded, total) =? node.downloadStatus(cid.get()): return RestApiResponse.response( $ %*{"downloaded": downloaded, "total": total}, contentType = "application/json" )