From 91e847126c832efa25ab0bf8d6f3b7e537378fa7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 16 Jan 2023 23:40:25 -0800 Subject: shuffle things around --- lib/upload.js | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'lib/upload.js') diff --git a/lib/upload.js b/lib/upload.js index 95dfe21a..b37e0bf2 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,7 +1,3 @@ -import app from "$app"; -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"; @@ -9,21 +5,13 @@ import fs from "fs"; import path from "path"; import sharp from "sharp"; import { pipeline } from "stream"; +import { g, s } from "$lib/db"; -app.register(fastifyMultipart); - -app.register(fastifyStatic, { - root: path.join("/app/uploads"), - prefix: "/public/" -}); - -app.post("/upload/:type", auth, async function(req, res) { - try { - let { type } = req.params; +export default async ({ params: { type }, file, user }, res) => { if (!["banner", "profile"].includes(type)) throw new Error("invalid upload"); - let data = await req.file(); + let data = await file(); let s1 = new Clone(data.file); let s2 = new Clone(data.file); @@ -32,7 +20,7 @@ app.post("/upload/:type", auth, async function(req, res) { if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext)) throw new Error("unsupported file type"); - let name = `${req.user.uuid}-${type}.webp`; + let name = `${user.id}-${type}.webp`; let path = `/app/uploads/${name}`; let size = type === "banner" ? 1920 : 240; @@ -43,12 +31,14 @@ app.post("/upload/:type", auth, async function(req, res) { await new Promise(r => pipeline(s2, t, fs.createWriteStream(path), r)); - req.user[type] = true; - await req.user.save(); + let r = async () => { + await db.watch(`user:${user.id}`); + user = await g(`user:${user.id}`); + user[type] = true; + let m = await db.multi(); + await s(`user:${user.id}`, user); + }; + await r(); res.send(name); - } catch (e) { - console.log(e); - return res.code(500).send(e); - } -}); +} -- cgit v1.2.3