- 在项目依赖中添加了@hono/zod-validator和zod包。 - 添加依赖库"@hono/zod-validator"和"zod"以增强数据验证功能。 - 添加了请求验证以确保提供了有效的图像文件。
This commit is contained in:
6
bun.lock
6
bun.lock
@@ -4,7 +4,9 @@
|
||||
"": {
|
||||
"name": "wxocr",
|
||||
"dependencies": {
|
||||
"@hono/zod-validator": "^0.7.1",
|
||||
"hono": "^4.8.4",
|
||||
"zod": "^4.0.5",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^4.16.2",
|
||||
@@ -67,6 +69,8 @@
|
||||
|
||||
"@eslint/plugin-kit": ["@eslint/plugin-kit@0.3.3", "", { "dependencies": { "@eslint/core": "^0.15.1", "levn": "^0.4.1" } }, "sha512-1+WqvgNMhmlAambTvT3KPtCl/Ibr68VldY2XY40SL1CE0ZXiakFR/cbTspaF5HsnpDMvcYYoJHfl4980NBjGag=="],
|
||||
|
||||
"@hono/zod-validator": ["@hono/zod-validator@0.7.1", "", { "peerDependencies": { "hono": ">=3.9.0", "zod": "^3.25.0 || ^4.0.0" } }, "sha512-8+vJT1RvezAx5sN7hiZ5Mis0RMuFL77nBEcqQQgT9ufoLkxr+7ll461WlBJQcGoQSY6EGMClVae19v3s/7bbgQ=="],
|
||||
|
||||
"@humanfs/core": ["@humanfs/core@0.19.1", "", {}, "sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA=="],
|
||||
|
||||
"@humanfs/node": ["@humanfs/node@0.16.6", "", { "dependencies": { "@humanfs/core": "^0.19.1", "@humanwhocodes/retry": "^0.3.0" } }, "sha512-YuI2ZHQL78Q5HbhDiBA1X4LmYdXCKCMQIfw0pw7piHJwyREFebJUvrQN4cMssyES6x+vfUbx1CIpaQUKYdQZOw=="],
|
||||
@@ -633,6 +637,8 @@
|
||||
|
||||
"yocto-queue": ["yocto-queue@0.1.0", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
|
||||
|
||||
"zod": ["zod@4.0.5", "", {}, "sha512-/5UuuRPStvHXu7RS+gmvRf4NXrNxpSllGwDnCBcJZtQsKrviYXm54yDGV2KYNLT5kq0lHGcl7lqWJLgSaG+tgA=="],
|
||||
|
||||
"zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="],
|
||||
|
||||
"@eslint-community/eslint-utils/eslint-visitor-keys": ["eslint-visitor-keys@3.4.3", "", {}, "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag=="],
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"hono": "^4.8.4"
|
||||
"@hono/zod-validator": "^0.7.1",
|
||||
"hono": "^4.8.4",
|
||||
"zod": "^4.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^4.16.2",
|
||||
|
||||
21
src/index.ts
21
src/index.ts
@@ -1,20 +1,22 @@
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { Hono } from 'hono'
|
||||
import { Buffer } from 'node:buffer'
|
||||
import { z } from 'zod'
|
||||
|
||||
import { WechatOCRClient } from './wcocr'
|
||||
|
||||
const app = new Hono()
|
||||
const client = new WechatOCRClient()
|
||||
|
||||
app.post('/ocr', async (c) => {
|
||||
const formData = await c.req.formData()
|
||||
const imageFile = formData.get('image')
|
||||
app.post(
|
||||
'/ocr',
|
||||
zValidator('form', z.object({
|
||||
image: z.instanceof(File, { message: 'Image file is required' }),
|
||||
})),
|
||||
async (c) => {
|
||||
const { image } = c.req.valid('form')
|
||||
|
||||
if (!imageFile || typeof imageFile === 'string') {
|
||||
return c.json({ error: 'Image file is required' }, 400)
|
||||
}
|
||||
|
||||
const imageBuffer = Buffer.from(await imageFile.arrayBuffer())
|
||||
const imageBuffer = Buffer.from(await image.arrayBuffer())
|
||||
|
||||
try {
|
||||
const result = await client.recognize(imageBuffer)
|
||||
@@ -23,6 +25,7 @@ app.post('/ocr', async (c) => {
|
||||
console.error('OCR processing error:', error)
|
||||
return c.json({ error: 'Failed to process image' }, 500)
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
export default app
|
||||
|
||||
Reference in New Issue
Block a user