aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-11-15 22:59:56 -0800
committerAdam Soltys <[email protected]>2023-11-15 22:59:56 -0800
commita81cb5add30036b5cf206e9f8003287922344725 (patch)
tree3b574c3242b24f2c5998b36a9f57d154f92ed0d2
parent545820baa7bf625fdb4abc7ec94154049391b710 (diff)
downloadcoinos-server-a81cb5add30036b5cf206e9f8003287922344725.tar.gz
coinos-server-a81cb5add30036b5cf206e9f8003287922344725.zip
hash uploads
-rw-r--r--index.js2
-rw-r--r--lib/register.js2
-rw-r--r--lib/upload.js53
-rw-r--r--routes/users.js8
4 files changed, 50 insertions, 15 deletions
diff --git a/index.js b/index.js
index 40c72795..4a645f15 100644
--- a/index.js
+++ b/index.js
@@ -79,7 +79,7 @@ app.post("/disable2fa", auth, users.disable2fa);
app.post("/2fa", auth, users.enable2fa);
app.post("/user", auth, users.update);
app.post("/reset", auth, users.reset);
-app.post("/upload/:type", auth, users.upload);
+app.post("/upload/:type", users.upload);
app.get("/users/delete/:username", users.del);
app.post("/login", users.login);
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);
+ });
+}
diff --git a/routes/users.js b/routes/users.js
index 747041eb..4571104f 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -109,6 +109,7 @@ export default {
"username",
"banner",
"profile",
+ "banner",
"address",
"currency",
"npub",
@@ -132,9 +133,10 @@ export default {
async create(req, res) {
try {
const ip = req.headers["cf-connecting-ip"];
- let { cipher, pubkey, password, username, salt } = req.body.user;
+ let { profile, cipher, pubkey, password, username, salt } = req.body.user;
let user = {
+ profile,
cipher,
pubkey,
password,
@@ -184,6 +186,8 @@ export default {
l("updating user", user.username);
let {
+ profile,
+ banner,
confirm,
password,
pin,
@@ -215,6 +219,7 @@ export default {
let attributes = [
"address",
+ "banner",
"cipher",
"currencies",
"currency",
@@ -224,6 +229,7 @@ export default {
"fiat",
"locktime",
"prompt",
+ "profile",
"pubkey",
"salt",
"seed",