simplifies torrent streaming API

This commit is contained in:
Marcin Czenko 2025-03-05 00:55:34 +01:00
parent d5928988d0
commit 6c5816581a
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0
3 changed files with 7 additions and 22 deletions

View File

@ -15,7 +15,6 @@ type
name*: ?string
BitTorrentInfoHash* = MultiHash
BitTorrentInfoHashV1* = distinct array[20, byte]
BitTorrentManifest* = ref object
info*: BitTorrentInfo
@ -26,11 +25,6 @@ proc newBitTorrentManifest*(
): BitTorrentManifest =
BitTorrentManifest(info: info, codexManifestCid: codexManifestCid)
# needed to be able to create a MultiHash from BitTorrentInfoHashV1
proc init*(
mhtype: typedesc[MultiHash], hashname: string, bdigest: BitTorrentInfoHashV1
): MhResult[MultiHash] {.borrow.}
func bencode*(info: BitTorrentInfo): seq[byte] =
# flatten pieces
var pieces: seq[byte]

View File

@ -349,21 +349,18 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
await node.retrieveCid(cid.get(), local = false, resp = resp)
router.api(MethodGet, "/api/codex/v1/data/{infoHash}/network/torrent") do(
infoHash: BitTorrentInfoHashV1, resp: HttpResponseRef
infoHash: MultiHash, resp: HttpResponseRef
) -> RestApiResponse:
var headers = buildCorsHeaders("GET", allowedOrigin)
without infoHash =? infoHash.tryGet.catch, error:
return RestApiResponse.error(Http400, error.msg, headers = headers)
without infoMultiHash =? MultiHash.init($Sha1HashCodec, infoHash).mapFailure, error:
without infoHash =? infoHash.mapFailure, error:
return RestApiResponse.error(Http400, error.msg, headers = headers)
if corsOrigin =? allowedOrigin:
resp.setCorsHeaders("GET", corsOrigin)
resp.setHeader("Access-Control-Headers", "X-Requested-With")
trace "torrent requested: ", multihash = $infoMultiHash
trace "torrent requested: ", multihash = $infoHash
return RestApiResponse.response(Http200)

View File

@ -21,7 +21,7 @@ import ../sales
import ../purchasing
import ../utils/stintutils
from ../bittorrent/manifest import BitTorrentInfoHashV1
from ../codextypes import Sha1HashCodec
proc encodeString*(cid: type Cid): Result[string, cstring] =
ok($cid)
@ -84,19 +84,13 @@ proc decodeString*(
except ValueError as e:
err e.msg.cstring
proc decodeString*(
_: type array[20, byte], value: string
): Result[array[20, byte], cstring] =
proc decodeString*(_: type MultiHash, value: string): Result[MultiHash, cstring] =
try:
ok array[20, byte].fromHex(value)
let bytes = value.hexToSeqByte
MultiHash.init($Sha1HashCodec, bytes)
except ValueError as e:
err e.msg.cstring
proc decodeString*[T: BitTorrentInfoHashV1](
_: type T, value: string
): Result[T, cstring] =
array[20, byte].decodeString(value).map(id => T(id))
proc decodeString*[T: PurchaseId | RequestId | Nonce | SlotId | AvailabilityId](
_: type T, value: string
): Result[T, cstring] =