diff options
| -rw-r--r-- | lib/payments.js | 30 | ||||
| -rw-r--r-- | routes/payments.js | 1 |
2 files changed, 22 insertions, 9 deletions
diff --git a/lib/payments.js b/lib/payments.js index dba7f4d0..9172c794 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -5,13 +5,14 @@ import { v4 } from "uuid"; import { notify } from "$lib/notifications"; import { db, g, s, t } from "$lib/db"; import { l, err, warn } from "$lib/logging"; -import { fail } from "$lib/utils"; +import { fail, wait } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; import dollarsToWords from "dollars-to-words"; import got from "got"; import mqtt from "mqtt"; import { spawn } from "child_process"; import ffmpeg from 'fluent-ffmpeg'; +import fs from "fs"; export const types = { internal: "internal", @@ -116,6 +117,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { tip = parseInt(tip) || 0; if (!memo) ({ memo } = invoice); + if (amount < 0 || tip < 0) fail("Invalid amount"); if (type === types.internal) amount += tip; let user = await g(`user:${invoice.uid}`); @@ -181,29 +183,39 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { parseFloat(((amount * p.rate) / 100000000).toFixed(2)) ); - l(words); - const client = mqtt.connect('mqtt://ln.coinos.io', config.mqtt); const raw = `/app/data/uploads/${p.id}-raw.mp3`; const out = `/app/data/uploads/${p.id}.mp3`; const mimic = spawn("/mimic", ["-t", words, "-voice", "/cmu_us_axb.flitevox", "-o", raw]); + let connected; + let f = fs.createWriteStream(out); + + mimic.stdout.on("end", async () => { + await wait(() => connected); + client.publish(username, "start"); - mimic.stdout.on("end", () => { ffmpeg(raw) .audioBitrate(128) .audioFrequency(44100) .toFormat('mp3') - .on('end', () => { - client.publish(username, "done"); - }) .on('error', (err) => { - console.error('Error:', err); + console.log('Error:', err); }) - .save(out); + .stream() + .on('data', (chunk) => { + client.publish(username, chunk); + f.write(chunk); + }) + .on('end', () => { + console.log("FFMPEG done"); + f.end(); + client.publish(username, "end"); + }); }); client.on('connect', function () { client.publish(username, `${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}`); + setTimeout(() => (connected = true), 50); }); } diff --git a/routes/payments.js b/routes/payments.js index 71260e11..425561f1 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -233,6 +233,7 @@ export default { async take({ body: { name, amount, hash }, user }, res) { amount = parseInt(amount); + if (amount < 0) fail("Invalid amount"); await t(`pot:${name}`, async (balance, db) => { if (balance < amount) fail("Insufficient funds"); |
