diff options
| author | Adam Soltys <[email protected]> | 2022-10-17 13:31:59 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-10-18 21:58:51 -0700 |
| commit | aec345fd11fd85ff74d435d338482d595f3de5be (patch) | |
| tree | 5443dec96d8bde17fcb0ef0af9287e02c3791fab /lib/upload.js | |
| parent | 1f3fef8eac67a6f7a96b567c5e0edcd923866117 (diff) | |
| download | coinos-server-aec345fd11fd85ff74d435d338482d595f3de5be.tar.gz coinos-server-aec345fd11fd85ff74d435d338482d595f3de5be.zip | |
process uploads to webp
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(); |
