removes duplication while streaming torrent content

This commit is contained in:
Marcin Czenko 2025-03-12 15:49:20 +01:00
parent ecb435beea
commit 040341b796
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0
2 changed files with 15 additions and 27 deletions

View File

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

View File

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