diff options
| author | Adam Soltys <[email protected]> | 2023-11-15 22:59:56 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-11-15 22:59:56 -0800 |
| commit | a81cb5add30036b5cf206e9f8003287922344725 (patch) | |
| tree | 3b574c3242b24f2c5998b36a9f57d154f92ed0d2 /lib | |
| parent | 545820baa7bf625fdb4abc7ec94154049391b710 (diff) | |
| download | coinos-server-a81cb5add30036b5cf206e9f8003287922344725.tar.gz coinos-server-a81cb5add30036b5cf206e9f8003287922344725.zip | |
hash uploads
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/register.js | 2 | ||||
| -rw-r--r-- | lib/upload.js | 53 |
2 files changed, 42 insertions, 13 deletions
diff --git a/lib/register.js b/lib/register.js index 9f8f927e..798565b9 100644 --- a/lib/register.js +++ b/lib/register.js @@ -8,7 +8,7 @@ import { s, g } from "$lib/db"; import { got } from "got"; export default async (user, ip, requireChallenge) => { - let { password, pubkey, username } = user; + let { profile, password, pubkey, username } = user; l("registering", username); if (!username) throw new Error("Username required"); diff --git a/lib/upload.js b/lib/upload.js index d46c116c..8d6413b0 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -6,14 +6,32 @@ import path from "path"; import sharp from "sharp"; import { pipeline } from "stream"; import { g, s, db } from "$lib/db"; +import crypto from "crypto"; + +async function getSha256Hash(stream) { + return new Promise((resolve, reject) => { + const hash = crypto.createHash("sha256"); + stream.pipe(hash); + + let hashValue; + hash.on("data", data => { + hashValue = data.toString("hex"); + }); + + hash.on("end", () => { + resolve(hashValue); + }); + + hash.on("error", reject); + }); +} export default async (req, res) => { let { - params: { type }, - user + params: { type } } = req; - if (!["banner", "profile"].includes(type)) throw new Error("invalid upload"); + let width = type === "banner" ? 1920 : 240; let data = await req.file(); let s1 = new Clone(data.file); @@ -24,19 +42,30 @@ export default async (req, res) => { if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext)) throw new Error("unsupported file type"); - let name = `${user.id}-${type}.webp`; - let path = `/app/data/uploads/${name}`; - let size = type === "banner" ? 1920 : 240; - let t = sharp() .rotate() - .resize(size) + .resize(width) .webp(); - await new Promise(r => pipeline(s2, t, fs.createWriteStream(path), r)); + let processedBuffer = await streamToBuffer(s2.pipe(t)); + let hash = crypto + .createHash("sha256") + .update(processedBuffer) + .digest("hex"); + + let filePath = `/app/data/uploads/${hash}.webp`; + fs.writeFileSync(filePath, processedBuffer); - user[type] = true; - await s(`user:${user.id}`, user); + console.log("SENDING", hash) - res.send(name); + res.send({ hash }); }; + +function streamToBuffer(stream) { + return new Promise((resolve, reject) => { + const chunks = []; + stream.on("data", chunk => chunks.push(chunk)); + stream.on("end", () => resolve(Buffer.concat(chunks))); + stream.on("error", reject); + }); +} |
