diff options
Diffstat (limited to 'lib/upload.ts')
| -rw-r--r-- | lib/upload.ts | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/upload.ts b/lib/upload.ts index 2be55a35..bc358ede 100644 --- a/lib/upload.ts +++ b/lib/upload.ts @@ -8,24 +8,28 @@ const sharp = require("sharp"); export default async (req, res) => { try { - let { + const { params: { type }, } = req; - let data = await req.file(); + const data = await req.file(); let buf = await data.toBuffer(); - let [format, ext] = (await fileTypeFromBuffer(buf)).mime.split("/"); + const [format, ext] = (await fileTypeFromBuffer(buf)).mime.split("/"); if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext)) fail("unsupported file type"); - let w = type === "banner" ? 1920 : 240; - buf = await sharp(buf, { failOnError: false }).rotate().resize(w).webp().toBuffer(); + const w = type === "banner" ? 1920 : 240; + buf = await sharp(buf, { failOnError: false }) + .rotate() + .resize(w) + .webp() + .toBuffer(); - let hash = createHash("sha256").update(buf).digest("hex"); + const hash = createHash("sha256").update(buf).digest("hex"); - let filePath = `/home/bun/app/data/uploads/${hash}.webp`; + const filePath = `/home/bun/app/data/uploads/${hash}.webp`; writeFileSync(filePath, buf); res.send({ hash }); |
