Skip to content

Commit f209507

Browse files
committed
simplifies torrent streaming API
1 parent 1ca6bb8 commit f209507

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

codex/bittorrent/manifest/manifest.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type
1515
name*: ?string
1616

1717
BitTorrentInfoHash* = MultiHash
18-
BitTorrentInfoHashV1* = distinct array[20, byte]
1918

2019
BitTorrentManifest* = ref object
2120
info*: BitTorrentInfo
@@ -26,11 +25,6 @@ proc newBitTorrentManifest*(
2625
): BitTorrentManifest =
2726
BitTorrentManifest(info: info, codexManifestCid: codexManifestCid)
2827

29-
# needed to be able to create a MultiHash from BitTorrentInfoHashV1
30-
proc init*(
31-
mhtype: typedesc[MultiHash], hashname: string, bdigest: BitTorrentInfoHashV1
32-
): MhResult[MultiHash] {.borrow.}
33-
3428
func bencode*(info: BitTorrentInfo): seq[byte] =
3529
# flatten pieces
3630
var pieces: seq[byte]

codex/rest/api.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,18 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
332332
await node.retrieveCid(cid.get(), local = false, resp = resp)
333333

334334
router.api(MethodGet, "/api/codex/v1/data/{infoHash}/network/torrent") do(
335-
infoHash: BitTorrentInfoHashV1, resp: HttpResponseRef
335+
infoHash: MultiHash, resp: HttpResponseRef
336336
) -> RestApiResponse:
337337
var headers = buildCorsHeaders("GET", allowedOrigin)
338338

339-
without infoHash =? infoHash.tryGet.catch, error:
340-
return RestApiResponse.error(Http400, error.msg, headers = headers)
341-
342-
without infoMultiHash =? MultiHash.init($Sha1HashCodec, infoHash).mapFailure, error:
339+
without infoHash =? infoHash.mapFailure, error:
343340
return RestApiResponse.error(Http400, error.msg, headers = headers)
344341

345342
if corsOrigin =? allowedOrigin:
346343
resp.setCorsHeaders("GET", corsOrigin)
347344
resp.setHeader("Access-Control-Headers", "X-Requested-With")
348345

349-
trace "torrent requested: ", multihash = $infoMultiHash
346+
trace "torrent requested: ", multihash = $infoHash
350347

351348
return RestApiResponse.response(Http200)
352349

codex/rest/coders.nim

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import ../sales
2121
import ../purchasing
2222
import ../utils/stintutils
2323

24-
from ../bittorrent/manifest import BitTorrentInfoHashV1
24+
from ../codextypes import Sha1HashCodec
2525

2626
proc encodeString*(cid: type Cid): Result[string, cstring] =
2727
ok($cid)
@@ -84,19 +84,13 @@ proc decodeString*(
8484
except ValueError as e:
8585
err e.msg.cstring
8686

87-
proc decodeString*(
88-
_: type array[20, byte], value: string
89-
): Result[array[20, byte], cstring] =
87+
proc decodeString*(_: type MultiHash, value: string): Result[MultiHash, cstring] =
9088
try:
91-
ok array[20, byte].fromHex(value)
89+
let bytes = value.hexToSeqByte
90+
MultiHash.init($Sha1HashCodec, bytes)
9291
except ValueError as e:
9392
err e.msg.cstring
9493

95-
proc decodeString*[T: BitTorrentInfoHashV1](
96-
_: type T, value: string
97-
): Result[T, cstring] =
98-
array[20, byte].decodeString(value).map(id => T(id))
99-
10094
proc decodeString*[T: PurchaseId | RequestId | Nonce | SlotId | AvailabilityId](
10195
_: type T, value: string
10296
): Result[T, cstring] =

0 commit comments

Comments
 (0)