From 6c5816581afdf088677d3831acc185e271ad0c21 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Wed, 5 Mar 2025 00:55:34 +0100 Subject: [PATCH] simplifies torrent streaming API --- codex/bittorrent/manifest/manifest.nim | 6 ------ codex/rest/api.nim | 9 +++------ codex/rest/coders.nim | 14 ++++---------- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/codex/bittorrent/manifest/manifest.nim b/codex/bittorrent/manifest/manifest.nim index 657bf6e2..83a31ec2 100644 --- a/codex/bittorrent/manifest/manifest.nim +++ b/codex/bittorrent/manifest/manifest.nim @@ -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] diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 5758ff1c..dbebf49a 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -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) diff --git a/codex/rest/coders.nim b/codex/rest/coders.nim index 95d39373..8d51251b 100644 --- a/codex/rest/coders.nim +++ b/codex/rest/coders.nim @@ -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] =