syndicate_utils/src/syndesizer/base64_decoder.nim

51 lines
1.5 KiB
Nim
Raw Permalink Normal View History

2024-04-01 18:24:18 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import
std/[base64, os],
pkg/nimcrypto/blake2,
preserves, preserves/sugar, syndicate,
../schema/config,
../schema/base64 as schema
2024-04-01 18:24:18 +00:00
export Base64DecoderArguments
export schema
2024-04-30 11:56:48 +00:00
proc spawnBase64Decoder*(turn: Turn; root: Cap): Actor {.discardable.} =
spawnActor(turn, "base64-decoder") do (turn: Turn):
2024-04-01 18:24:18 +00:00
let tmpDir = getTempDir()
during(turn, root, ?:Base64DecoderArguments) do (ds: Cap):
2024-04-22 12:49:14 +00:00
let decTextPat = observePattern(!Base64Text, { @[%0]: grabLit() })
2024-04-01 18:24:18 +00:00
during(turn, ds, decTextPat) do (txt: string):
discard publish(turn, ds, Base64Text(
txt: txt,
bin: cast[seq[byte]](decode(txt)),
))
2024-04-22 12:49:14 +00:00
let encTextPat = observePattern(!Base64Text, { @[%1]: grabLit() })
2024-04-01 18:24:18 +00:00
during(turn, ds, encTextPat) do (bin: seq[byte]):
discard publish(turn, ds, Base64Text(
txt: encode(bin),
bin: bin,
))
2024-04-22 12:49:14 +00:00
let decFilePat = observePattern( !Base64File, { @[%0]: grabLit() })
2024-04-01 18:24:18 +00:00
during(turn, ds, decFilePat) do (txt: string):
var
bin = decode(txt)
digest = $blake2_256.digest(bin)
2024-04-01 18:24:18 +00:00
path = tmpDir / digest
writeFile(path, bin)
discard publish(turn, ds, Base64File(
txt: txt,
path: path,
size: bin.len,
))
when isMainModule:
import syndicate/relays
2024-04-30 11:56:48 +00:00
runActor("main") do (turn: Turn):
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
2024-04-01 18:24:18 +00:00
spawnBase64Decoder(turn, ds)