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