diff options
Diffstat (limited to 'lib/upload.js')
| -rw-r--r-- | lib/upload.js | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/upload.js b/lib/upload.js index a91e4eba..8d1c85ef 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -7,6 +7,8 @@ import pump from "pump"; import Clone from "readable-stream-clone"; import fs from "fs"; import path from "path"; +import sharp from "sharp"; +import { pipeline } from "stream"; app.register(fastifyMultipart); @@ -30,12 +32,16 @@ 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.username}-${type}.png`; + let name = `${req.user.username}-${type}.webp`; let path = `/app/uploads/${name}`; + let size = type === "banner" ? 1920 : 240; - await new Promise(resolve => - s2.pipe(fs.createWriteStream(path).on("finish", resolve)) - ); + let t = sharp() + .rotate() + .resize(size) + .webp(); + + await new Promise(r => pipeline(s2, t, fs.createWriteStream(path), r)); req.user[type] = true; await req.user.save(); |
