From d954ecb28cc43d8deb122e0f55afb80e0ba63ffa Mon Sep 17 00:00:00 2001 From: Peter Hebden Date: Sat, 27 Jan 2024 16:36:25 +0000 Subject: [PATCH] Image submissions --- client/package.json | 2 +- client/src/routes/+page.svelte | 1 + client/src/routes/add/+page.svelte | 71 +++++++++++++++++-- client/src/routes/login/+page.svelte | 1 - client/src/routes/review/+page.svelte | 49 +++++++++++-- client/vite.config.ts | 9 +++ server/Cargo.lock | 27 ++++++- server/Cargo.toml | 4 +- .../2024-01-26-171232_accept_images/down.sql | 5 ++ .../2024-01-26-171232_accept_images/up.sql | 5 ++ server/src/auth.rs | 2 +- server/src/main.rs | 64 +++++++++++++---- server/src/models.rs | 10 ++- server/src/schema.rs | 6 +- 14 files changed, 221 insertions(+), 35 deletions(-) create mode 100644 server/migrations/2024-01-26-171232_accept_images/down.sql create mode 100644 server/migrations/2024-01-26-171232_accept_images/up.sql diff --git a/client/package.json b/client/package.json index e635e63..f925e06 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "client", - "version": "0.0.1", + "version": "0.0.2", "private": true, "scripts": { "dev": "vite dev", diff --git a/client/src/routes/+page.svelte b/client/src/routes/+page.svelte index dfa6f3c..9c60ec6 100644 --- a/client/src/routes/+page.svelte +++ b/client/src/routes/+page.svelte @@ -80,6 +80,7 @@ Review Add problems +

Watson client v{PKG.version}

Contributions Leaderboard

diff --git a/client/src/routes/add/+page.svelte b/client/src/routes/add/+page.svelte index b5024d0..835ff07 100644 --- a/client/src/routes/add/+page.svelte +++ b/client/src/routes/add/+page.svelte @@ -3,6 +3,7 @@ import { axios, exampleProblem } from "$lib"; import TexBox from "$lib/TexBox.svelte"; import Box from "$lib/Box.svelte"; + import { PUBLIC_WATSON_MEDIA_BASE } from "$env/static/public"; let { body, soln } = exampleProblem; let source = ""; let author = ""; @@ -35,7 +36,8 @@ errors = []; if (selectedModule < -1) errors = ["Please select a module", ...errors]; if (selectedTopic < -1) errors = ["Please select a topic", ...errors]; - if (body.replaceAll(/\s+/g, "") === "") errors = ["Please enter a problem", ...errors]; + if (problemImg === null && body.replaceAll(/\s+/g, "") === "") + errors = ["Please enter a problem", ...errors]; if (errors.length > 0) return; waiting = true; @@ -45,13 +47,17 @@ author, body, soln: soln.replaceAll(/\s+/g, "") === "" ? null : soln, + soln_img: solnImg, solnlink: solnLink, + img_path: problemImg, module: selectedModule === -1 ? newModule : selectedModule, topic: selectedTopic === -1 ? newTopic : selectedTopic, }) .then(() => { body = ""; soln = ""; + problemImg = null; + solnImg = null; if (selectedModule === -1) selectedModule = -2; if (selectedTopic === -1) selectedTopic = -2; waiting = false; @@ -61,6 +67,39 @@ waiting = false; }); } + + // Srcs for images relative to the media base URL. + let problemImg: string | null = null; + let solnImg: string | null = null; + + type ImgSrc = "problem" | "solution"; + + const uploadClipboardImg = (imgData, src: ImgSrc) => { + if (imgData !== undefined && imgData.type.startsWith("image/")) { + axios + .post("/upload", { file: imgData }, { headers: { "content-type": "multipart/form-data" } }) + .then((res) => { + if (src === "problem") { + problemImg = res.data; + } else { + solnImg = res.data; + } + }) + .catch((e) => console.log(e)); + } + }; + + const onloadProblem = (el) => { + el.addEventListener("paste", (ev) => { + uploadClipboardImg(ev.clipboardData.files[0], "problem"); + }); + }; + + const onloadSolution = (el) => { + el.addEventListener("paste", (ev) => { + uploadClipboardImg(ev.clipboardData.files[0], "solution"); + }); + };
@@ -100,14 +139,28 @@ class="p-2 w-full bg-midnight text-white" />
- -