diff options
| author | Adam Soltys <[email protected]> | 2022-09-07 13:11:12 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-07 13:11:12 -0700 |
| commit | 06b59b585ac9af7be106b9c1271d251ba20f6630 (patch) | |
| tree | d01db8fa25ade19c534f9ac34b11c3bff1ac507d /lib | |
| parent | ca5ccaf2deeba7eda2c16fd1b7d33aa4c25e960f (diff) | |
| download | coinos-server-06b59b585ac9af7be106b9c1271d251ba20f6630.tar.gz coinos-server-06b59b585ac9af7be106b9c1271d251ba20f6630.zip | |
clean up
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mail.js | 144 | ||||
| -rw-r--r-- | lib/upload.js | 66 |
2 files changed, 65 insertions, 145 deletions
diff --git a/lib/mail.js b/lib/mail.js index 537614d2..a146d128 100644 --- a/lib/mail.js +++ b/lib/mail.js @@ -1,132 +1,34 @@ -import { emit } from "$lib/sockets"; import app from "$app"; import config from "$config"; -import fs from "fs"; -import Imap from "imap"; -import { inspect } from "util"; import persist from "./persist"; const emails = persist("data/emails.json"); -import { rates } from "./store"; -let imap = new Imap(config.imap); -function openInbox(cb) { - imap.openBox("INBOX", true, cb); -} +app.post("/email", async (req, res) => { + try { + let file = persist("data/emails.json"); + file.emails = [...file.emails, req.body]; -app.post("/contact-sil", (req, res) => { - const mailgun = require("mailgun-js")(config.mailgun); - let data = { - subject: "Contact form", - text: JSON.stringify(req.body), - from: "[email protected]", - to: "[email protected]" - }; + try { + var postmark = await import("postmark"); - mailgun.messages().send(data); - res.send(req.body); -}); - -app.post("/contact", (req, res) => { - const mailgun = require("mailgun-js")(config.mailgun); - let data = { - subject: "Contact form", - text: JSON.stringify(req.body), - from: "[email protected]", - to: "[email protected]" - }; - - mailgun.messages().send(data); - res.send(req.body); -}); + var client = new postmark.ServerClient(config.postmark); -imap.once("ready", function() { - openInbox(function(err, box) { - if (err) throw err; - setInterval(() => { - let f = imap.seq.fetch(`${box.messages.total - 5}:*`, { - bodies: ["TEXT"], - struct: true + await client.sendEmail({ + From: "[email protected]", + To: "[email protected]", + Subject: req.body.subject || "Email Signup", + HtmlBody: JSON.stringify(req.body), + TextBody: JSON.stringify(req.body), + MessageStream: "outbound" }); - f.on("message", function(msg, seqno) { - const prefix = "(#" + seqno + ") "; - msg.on("body", function(stream, info) { - let buffer = ""; - stream.on("data", function(chunk) { - //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'}); - buffer += chunk.toString("utf8"); - }); - stream.once("end", async function() { - let accounts = config.imap.accounts; - let a = Object.keys(accounts).find(a => buffer.includes(a)); - let user_id = accounts[a]; - - if (!emails[seqno] && user_id) { - let cad = parseFloat( - buffer - .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") - .replace(",", "") - ); - emails[seqno] = cad; - - try { - await db.transaction(async transaction => { - const account = await db.Account.findOne({ - where: { - user_id, - asset: config.liquid.btcasset, - pubkey: null - }, - include: { - model: db.User, - as: "user" - }, - lock: transaction.LOCK.UPDATE, - transaction - }); - - let { user } = account; - - let rate = store.rates["CAD"] * 1.02; - let amount = Math.round((cad / rate) * 100000000); - - await account.increment({ balance: amount }, { transaction }); - - let payment = await db.Payment.create({ - account_id: account.id, - user_id: account.user_id, - hash: `Interac deposit: ${cad} CAD`, - amount, - currency: "CAD", - rate, - received: true, - tip: 0, - confirmed: true, - address: "", - network: "COINOS" - }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - - emit(user.username, "account", account); - emit(user.username, "payment", payment); - - l("interac deposit detected for", user.username, cad, amount); - }); - } catch (e) { - console.log(e); - } - } - }); - }); - msg.once("end", function() { - // console.log(prefix + 'Finished'); - }); - - imap.seq.addFlags(seqno, ["Seen"]); - }); - }, 10000); - }); + res.send({ ok: true }); + } catch (e) { + console.log("problem sending email", e); + res.code(500).send(e.message); + } + } catch (e) { + console.log(e); + res.code(500).send(e.message); + } }); - -imap.connect(); diff --git a/lib/upload.js b/lib/upload.js index 4deaa130..ceead928 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,30 +1,48 @@ -import { auth } from "./passport"; import app from "$app"; -import multer from "multer"; +import { auth } from "./passport"; +import fastifyMultipart from "@fastify/multipart"; +import fastifyStatic from "@fastify/static"; +import { fileTypeFromStream } from "file-type"; +import pump from "pump"; +import Clone from "readable-stream-clone"; +import fs from "fs"; +import path from "path"; -const storage = multer.diskStorage({ - destination: (req, file, cb) => { - cb(null, "uploads"); - }, - filename: async (req, file, cb) => { - const parts = file.originalname.split("."); - const ext = parts.length > 1 ? parts[parts.length - 1] : "bin"; - cb(null, `${req.user.username}-${file.fieldname}.${ext}`); - } -}); +app.register(fastifyMultipart); -export const upload = multer({ - storage, - onFileUploadStart: (file, req, res) => { - console.log("CHECKING SIZE"); - const maxSize = 32 * 1000 * 1000; - if (req.files.file.length > maxSize) { - return false; - } - } +app.register(fastifyStatic, { + root: path.join("/app/uploads"), + prefix: "/public/" }); -app.post("/profile", auth, upload.single("squirt"), async (req, res) => { - let { filename } = req.file; - res.send({ filename }); +app.post("/upload/:type", auth, async function(req, res) { + try { + let { type } = req.params; + if (!["banner", "profile"].includes(type)) + throw new Error("invalid upload"); + + let data = await req.file(); + let s1 = new Clone(data.file); + let s2 = new Clone(data.file); + + let [format, ext] = (await fileTypeFromStream(s1)).mime.split("/"); + + if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext)) + throw new Error("unsupported file type"); + + let name = `${req.user.username}-${type}.png`; + let path = `/app/uploads/${name}`; + + await new Promise(resolve => + s2.pipe(fs.createWriteStream(path).on("finish", resolve)) + ); + + req.user[type] = true; + await req.user.save(); + + res.send(name); + } catch (e) { + console.log(e); + return res.status(500).send(e); + } }); |
