diff --git a/codex/node.nim b/codex/node.nim index bd769070..0fd4f827 100644 --- a/codex/node.nim +++ b/codex/node.nim @@ -81,6 +81,8 @@ type CodexNodeRef* = ref CodexNode + Torrent* = tuple[torrentManifest: BitTorrentManifest, codexManifest: Manifest] + OnManifest* = proc(cid: Cid, manifest: Manifest): void {.gcsafe, raises: [].} BatchProc* = proc(blocks: seq[bt.Block]): Future[?!void] {. gcsafe, async: (raises: [CancelledError]) @@ -439,7 +441,7 @@ proc fetchPieces*( codexManifest.treeCid, blockIter, pieceIter, numOfBlocksPerPiece, onPiece ) -proc streamTorrent( +proc streamTorrent*( self: CodexNodeRef, torrentManifest: BitTorrentManifest, codexManifest: Manifest ): Future[?!LPStream] {.async.} = trace "Retrieving pieces from torrent" @@ -495,7 +497,7 @@ proc streamTorrent( proc retrieveTorrent*( self: CodexNodeRef, infoHash: MultiHash -): Future[?!LPStream] {.async.} = +): Future[?!Torrent] {.async.} = without infoHashCid =? Cid.init(CIDv1, InfoHashV1Codec, infoHash).mapFailure, error: trace "Unable to create CID for BitTorrent info hash" return failure(error) @@ -509,7 +511,7 @@ proc retrieveTorrent*( trace "Unable to fetch Codex Manifest for torrent info hash" return failure(err) - await self.streamTorrent(torrentManifest, codexManifest) + success (torrentManifest: torrentManifest, codexManifest: codexManifest) proc deleteSingleBlock(self: CodexNodeRef, cid: Cid): Future[?!void] {.async.} = if err =? (await self.networkStore.delBlock(cid)).errorOption: diff --git a/codex/rest/api.nim b/codex/rest/api.nim index bd56d36c..a63a241d 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -167,37 +167,23 @@ proc retrieveInfoHash( var bytes = 0 try: - without stream =? (await node.retrieveTorrent(infoHash)), error: - if error of BlockNotFoundError: + without torrent =? (await node.retrieveTorrent(infoHash)), err: + error "Unable to fetch Torrent Metadata", err = err.msg + resp.status = Http404 + await resp.sendBody(err.msg) + return + let (torrentManifest, codexManifest) = torrent + + without stream =? (await node.streamTorrent(torrentManifest, codexManifest)), err: + if err of BlockNotFoundError: resp.status = Http404 await resp.sendBody("") return else: resp.status = Http500 - await resp.sendBody(error.msg) + await resp.sendBody(err.msg) return - # It is ok to fetch again the manifest because it will hit the cache - without infoHashCid =? Cid.init(CIDv1, InfoHashV1Codec, infoHash).mapFailure, err: - error "Unable to create CID for BitTorrent info hash", err = err.msg - resp.status = Http404 - await resp.sendBody(err.msg) - return - - without torrentManifest =? (await node.fetchTorrentManifest(infoHashCid)), err: - error "Unable to fetch Torrent Manifest", err = err.msg - resp.status = Http404 - await resp.sendBody(err.msg) - return - - without codexManifest =? ( - await node.fetchManifest(torrentManifest.codexManifestCid) - ), err: - error "Unable to fetch Codex Manifest for torrent info hash", err = err.msg - resp.status = Http404 - await resp.sendBody(err.msg) - return - if codexManifest.mimetype.isSome: resp.setHeader("Content-Type", codexManifest.mimetype.get()) else: