From cdffcd2a32d54137de96567f0d211eda6039a9d3 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 20 Feb 2021 17:42:04 +0100 Subject: Multiple images ShouldTM work --- profile/index.js | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'profile/index.js') diff --git a/profile/index.js b/profile/index.js index 6bc2221..3240c77 100644 --- a/profile/index.js +++ b/profile/index.js @@ -35,6 +35,13 @@ app.get("/api/questions", async (req, res) => { const qid = questions.findIndex((question) => question.id === answer.question_id); if (qid >= 0) questions[qid].answer = answer.answer; } + + const ratios = await db.query("SELECT question_id, x, y FROM profile_image_ratios"); + for (const { question_id, x, y } of ratios) { + const qid = questions.findIndex((question) => question.id === question_id); + if (qid >= 0) questions[qid].ratio = { x, y }; + } + res.json(questions); }); @@ -78,45 +85,32 @@ async function answer(req, res, qs) { } } -app.post("/api/answerImage", async (req, res) => { - return await answerImage(req, res, "INSERT INTO profile_answers (answer, question_id, user_id) VALUE (?,?,?)"); -}); -app.put("/api/answerImage", async (req, res) => { - return await answerImage(req, res, "UPDATE profile_answers SET answer = ? WHERE question_id = ? AND user_id = ?"); -}); +app.post("/api/answerImage", async (req, res) => await answerImage(req, res)); -async function answerImage(req, res, qs) { +async function answerImage(req, res) { try { for (const fid in req.files) { if (!req.files.hasOwnProperty(fid)) continue; const image = req.files[fid]; - const name = `child_${req.session.uid}.jpg`; + const name = `${fid}_${req.session.uid}.jpg`; const params = [name, fid, req.session.uid]; try { - await image.mv(`${__dirname}/public/uploads/${name}`); // Overwrite anyway - tbh we don't need update stmt - await db.query(qs, params); + await image.mv(`${__dirname}/public/uploads/${name}`); // Overwrite anyway + await db.query("INSERT INTO profile_answers (answer, question_id, user_id) VALUE (?,?,?)", params); } catch (e) { if (e.code === "ER_DUP_ENTRY") { - // Fix strange POST behaviour - try { - await db.query( - "UPDATE profile_answers SET answer = ? WHERE question_id = ? AND user_id = ?", - params, - ); - } catch (e) { - console.error(e); - return res.json({ success: false }); - } + console.log("Image already in db!"); + return res.json({ success: true }); } else { console.error(e); return res.json({ success: false }); } } } - res.json({ success: true }); + return res.json({ success: true }); } catch (e) { console.error(e); - res.json({ success: false }); + return res.json({ success: false }); } } -- cgit v1.2.3 From 8ef918222c285f64bec6e7a14c7cb0df250b9b1d Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 20 Feb 2021 18:56:52 +0100 Subject: Some style fixes --- profile/index.js | 1 - profile/public/style.css | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'profile/index.js') diff --git a/profile/index.js b/profile/index.js index 3240c77..63bb2ec 100644 --- a/profile/index.js +++ b/profile/index.js @@ -99,7 +99,6 @@ async function answerImage(req, res) { await db.query("INSERT INTO profile_answers (answer, question_id, user_id) VALUE (?,?,?)", params); } catch (e) { if (e.code === "ER_DUP_ENTRY") { - console.log("Image already in db!"); return res.json({ success: true }); } else { console.error(e); diff --git a/profile/public/style.css b/profile/public/style.css index 1fc5a9d..c7c8edb 100644 --- a/profile/public/style.css +++ b/profile/public/style.css @@ -35,8 +35,8 @@ select { } img { - max-width: 80%; - max-height: 80%; + max-width: 50%; + max-height: 50%; } @media only screen and (max-width: 600px) { @@ -140,6 +140,7 @@ img { position: absolute; display: none; max-width: 600px; + max-height: 600px; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); -- cgit v1.2.3