136 lines
3.9 KiB
TypeScript
136 lines
3.9 KiB
TypeScript
import { pathToFileURL } from "bun";
|
|
import { Hono } from "hono";
|
|
import { onlySSG } from "hono/ssg";
|
|
import { validator } from "hono/validator";
|
|
import { isQuestionOrPlusOrMinusToken } from "typescript/unstable/ast";
|
|
import { Signature } from "typescript/unstable/sync";
|
|
|
|
const appTmp = new Hono()
|
|
const app = appTmp.basePath("/watermark")
|
|
|
|
/**
|
|
* param: image: 图片大小必须<=10MB
|
|
* param: payload: 必须是长度<=8的字母大小写或数字组合
|
|
* param: strength: 取值>=0,一般取值0~1.5
|
|
* param: outputType: 输出的文件类型,只允许 jpeg | png
|
|
*/
|
|
app.post("/encode",
|
|
validator("form", (val, c) => {
|
|
const image = val.image
|
|
const payload = val.payload
|
|
const strength = val.strength
|
|
const outputType = val.outputType
|
|
if (
|
|
image == undefined ||
|
|
payload == undefined ||
|
|
strength == undefined ||
|
|
outputType == undefined
|
|
) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Paarameter is invalid"
|
|
})
|
|
}
|
|
|
|
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"
|
|
})
|
|
}
|
|
|
|
|
|
if ((payload as string).length > 8 || (payload as string).length <= 0) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Parameter[payload] length must > 0 and <= 8"
|
|
})
|
|
}
|
|
|
|
if (!/^[a-zA-Z0-9]+$/.test(payload as string)) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Parameter[payload] must be an ASCII string"
|
|
})
|
|
}
|
|
|
|
|
|
if (isNaN(Number(strength))) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Parameter[strength] must be a number"
|
|
})
|
|
}
|
|
|
|
const tmpStrength = Number(strength)
|
|
if (tmpStrength < 0 || tmpStrength > 100) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Parameter[strength] must >= 0 and <= 100"
|
|
})
|
|
}
|
|
|
|
if (
|
|
!((outputType as string) == "jpeg" ||
|
|
(outputType as string) == "png")
|
|
) {
|
|
c.status(400)
|
|
return c.json({
|
|
code: 400,
|
|
msg: "Parameter[outputType] only support jpeg and png"
|
|
})
|
|
}
|
|
|
|
return val
|
|
}),
|
|
async (c) => {
|
|
const formData = await c.req.formData()
|
|
const image: File = formData.get("image") as File
|
|
const payload: string = formData.get("payload") as string
|
|
const strength: number = Number(formData.get("strength"))
|
|
const outputType: string = formData.get("outputType") as string
|
|
|
|
const data = new FormData()
|
|
data.append("image", image)
|
|
data.append("payload", payload)
|
|
data.append("strength", strength.toString())
|
|
data.append("outputType", outputType)
|
|
let canFetch: boolean = true
|
|
const response = await fetch("http://localhost:8081/watermark/encode", {
|
|
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/encode"
|
|
})
|
|
}
|
|
|
|
const resImage = await response!.blob()
|
|
return c.body(resImage)
|
|
})
|
|
|
|
export default {
|
|
port: 8080,
|
|
fetch: app.fetch
|
|
} |