mirror of
https://github.com/khairul169/home-lab.git
synced 2025-04-28 16:49:36 +07:00
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import type { Context } from "hono";
|
|
import fs from "node:fs/promises";
|
|
import { uploadSchema } from "./schema";
|
|
import { HTTPException } from "hono/http-exception";
|
|
import { getFilePath } from "./utils";
|
|
|
|
export const upload = async (c: Context) => {
|
|
const input: any = (await c.req.parseBody()) as never;
|
|
const data = await uploadSchema.parseAsync(input);
|
|
|
|
const size = parseInt(input.size);
|
|
if (Number.isNaN(size) || !size) {
|
|
throw new HTTPException(400, { message: "Size is empty!" });
|
|
}
|
|
|
|
const files: File[] = [...Array(size)]
|
|
.map((_, idx) => input[`files.${idx}`])
|
|
.filter((i) => !!i);
|
|
|
|
if (!files.length) {
|
|
throw new HTTPException(400, { message: "Files is empty!" });
|
|
}
|
|
|
|
const { baseDir, path: targetDir } = getFilePath(data.path);
|
|
if (!baseDir?.length) {
|
|
throw new HTTPException(400, { message: "Path not found!" });
|
|
}
|
|
|
|
// files.forEach((file) => {
|
|
// const filepath = targetDir + "/" + file.name;
|
|
// if (existsSync(filepath)) {
|
|
// throw new HTTPException(400, { message: "File already exists!" });
|
|
// }
|
|
// });
|
|
|
|
await Promise.all(
|
|
files.map(async (file) => {
|
|
const filepath = targetDir + "/" + file.name;
|
|
const buffer = await file.arrayBuffer();
|
|
await fs.writeFile(filepath, new Uint8Array(buffer));
|
|
})
|
|
);
|
|
|
|
return c.json({ success: true });
|
|
};
|