From b4f0cceb48486e441b6aac43830c59beadf59e1c Mon Sep 17 00:00:00 2001 From: CancerAi Date: Mon, 28 Sep 2026 22:31:44 +0800 Subject: [PATCH] =?UTF-8?q?done:=20=E5=AE=8C=E6=88=90=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E7=9B=B2=E6=B0=B4=E5=8D=B0API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/backend/src/index.ts b/backend/src/index.ts index d6227d0..72d0035 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -33,6 +33,14 @@ app.post("/encode", }) } + if ((image as File[]).length >= 2) { + c.status(400) + return c.json({ + code: 400, + msg: "Only support upload one image" + }) + } + if ((image as File).size == 0) { c.status(400) return c.json({ @@ -130,6 +138,77 @@ app.post("/encode", return c.body(resImage) }) +app.post("/decode", + validator("form", (val, c) => { + const image = val.image + if (image == undefined) { + c.status(400) + return c.json({ + code: 400, + msg: "Paarameter is invalid" + }) + } + + if ((image as File[]).length >= 2) { + c.status(400) + return c.json({ + code: 400, + msg: "Only support upload one image" + }) + } + + if ((image as File).size == 0) { + c.status(400) + return c.json({ + code: 400, + msg: "Image is empty" + }) + } + + const MAX_IMAGE_SIZE = 10 * 1024 * 1024 + if ((image as File).size > MAX_IMAGE_SIZE) { + c.status(400) + return c.json({ + code: 400, + msg: "Image size is too large, must<=10MB" + }) + } + },), + async (c) => { + const formData = await c.req.formData() + const image: File = formData.get("image") as File + const data = new FormData() + let canFetch: boolean = true + data.append("image", image) + + const response = await fetch("http://localhost:8081/watermark/decode", { + method: "POST", + body: data + }).catch((e) => { + canFetch = false + }) + + if (!canFetch) { + c.status(500) + return c.json({ + code: 500, + msg: "Cannot fetch http://localhost:8081/watermark/decode" + }) + } + + const res = await response!.json() + /** + * Response struct + * { + * "hasWatermark": true|false, + * "payload": "abc" | null, + * "schema": 1 | null + * } + */ + return c.body(JSON.stringify(res)) + } +) + export default { port: 8080, fetch: app.fetch