feat: use dataset CID to identify download instead of integer

This commit is contained in:
gmega 2025-04-16 14:21:53 -03:00
parent 5d39d126dc
commit 256e33193e
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 14 additions and 22 deletions

View File

@ -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))

View File

@ -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"
)