add: 新增上传文件api
This commit is contained in:
1 parent
30d499f858
commit
a4be42d769
1 file changed
+24
@@ -5,6 +5,7 @@ import { postJson } from "./service/postJson";
|
|||||||
import { isEmpty } from "./utils/is_empty";
|
import { isEmpty } from "./utils/is_empty";
|
||||||
import { get } from "./service/get";
|
import { get } from "./service/get";
|
||||||
import { cors } from "hono/cors";
|
import { cors } from "hono/cors";
|
||||||
|
import { bodyLimit } from "hono/body-limit";
|
||||||
|
|
||||||
const tmpApp: Hono = new Hono()
|
const tmpApp: Hono = new Hono()
|
||||||
export const app = tmpApp.basePath("/test/")
|
export const app = tmpApp.basePath("/test/")
|
||||||
@@ -71,6 +72,29 @@ app.delete("/user",
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
app.post("/file",
|
||||||
|
bodyLimit({
|
||||||
|
maxSize: 5 * 1024 * 1024,
|
||||||
|
onError: (c) => {
|
||||||
|
return c.json(ApiResponse.err(400, "File must less than 5MB"))
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
async (c) => {
|
||||||
|
const body = await c.req.parseBody()
|
||||||
|
const file = body["file"]
|
||||||
|
if (!(file instanceof File)) {
|
||||||
|
return c.json(ApiResponse.err(400, "Not file"))
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileName = file.name
|
||||||
|
const fileSize = file.size
|
||||||
|
return c.json(ApiResponse.success(200, "Upload file successfully", {
|
||||||
|
fileName,
|
||||||
|
fileSize
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
app.notFound((c) => {
|
app.notFound((c) => {
|
||||||
console.log(`Not found [${c.req.method}]: ${c.req.path}`);
|
console.log(`Not found [${c.req.method}]: ${c.req.path}`);
|
||||||
return c.json(ApiResponse.err(404, "Not Found"))
|
return c.json(ApiResponse.err(404, "Not Found"))
|
||||||
|
|||||||
Reference in new issue
Block a user