52 lines
1.6 KiB
Nim
52 lines
1.6 KiB
Nim
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/[base64, os]
|
|
import pkg/hashlib/misc/blake2
|
|
import preserves, syndicate
|
|
import ../schema/config
|
|
import ../schema/base64 as schema
|
|
|
|
export Base64DecoderArguments
|
|
export schema
|
|
|
|
proc spawnBase64Decoder*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
|
spawnActor(turn, "base64-decoder") do (turn: var Turn):
|
|
let tmpDir = getTempDir()
|
|
during(turn, root, ?:Base64DecoderArguments) do (ds: Cap):
|
|
|
|
let decTextPat = ?Observe(pattern: !Base64Text) ?? { 0: grabLit() }
|
|
during(turn, ds, decTextPat) do (txt: string):
|
|
discard publish(turn, ds, Base64Text(
|
|
txt: txt,
|
|
bin: cast[seq[byte]](decode(txt)),
|
|
))
|
|
|
|
let encTextPat = ?Observe(pattern: !Base64Text) ?? { 1: grabLit() }
|
|
during(turn, ds, encTextPat) do (bin: seq[byte]):
|
|
discard publish(turn, ds, Base64Text(
|
|
txt: encode(bin),
|
|
bin: bin,
|
|
))
|
|
|
|
let decFilePat = ?Observe(pattern: !Base64File) ?? { 0: grabLit() }
|
|
during(turn, ds, decFilePat) do (txt: string):
|
|
var bin = decode(txt)
|
|
var ctx = init[BLAKE2B_512]()
|
|
ctx.update(bin)
|
|
let
|
|
digest = $ctx.final()
|
|
path = tmpDir / digest
|
|
writeFile(path, bin)
|
|
discard publish(turn, ds, Base64File(
|
|
txt: txt,
|
|
path: path,
|
|
size: bin.len,
|
|
))
|
|
|
|
when isMainModule:
|
|
import syndicate/relays
|
|
runActor("main") do (turn: var Turn):
|
|
resolveEnvironment(turn) do (turn: var Turn; ds: Cap):
|
|
spawnBase64Decoder(turn, ds)
|