mirror of
https://github.com/khairul169/home-lab.git
synced 2026-10-02 17:13:57 +07:00
feat: add file operation
This commit is contained in:
@@ -13,6 +13,11 @@ const getFilesSchema = z
|
||||
.partial()
|
||||
.optional();
|
||||
|
||||
const uploadSchema = z.object({
|
||||
path: z.string().min(1),
|
||||
size: z.string().min(1),
|
||||
});
|
||||
|
||||
const filesDirList = process.env.FILE_DIRS
|
||||
? process.env.FILE_DIRS.split(";").map((i) => ({
|
||||
name: i.split("/").at(-1),
|
||||
@@ -68,6 +73,50 @@ const route = new Hono()
|
||||
|
||||
return c.json([]);
|
||||
})
|
||||
.post("/upload", async (c) => {
|
||||
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 pathSlices = data.path.split("/");
|
||||
const baseName = pathSlices[1] || null;
|
||||
const path = pathSlices.slice(2).join("/");
|
||||
const baseDir = filesDirList.find((i) => i.name === baseName)?.path;
|
||||
if (!baseDir?.length) {
|
||||
throw new HTTPException(400, { message: "Path not found!" });
|
||||
}
|
||||
|
||||
const targetDir = [baseDir, path].join("/");
|
||||
|
||||
// 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 });
|
||||
})
|
||||
.get("/download/*", async (c) => {
|
||||
const dlFile = c.req.query("dl") === "true";
|
||||
const url = new URL(c.req.url, `http://${c.req.header("host")}`);
|
||||
@@ -132,4 +181,23 @@ const route = new Hono()
|
||||
}
|
||||
});
|
||||
|
||||
function getFilePath(path: string) {
|
||||
const pathSlices = path.split("/");
|
||||
const baseName = pathSlices[1] || null;
|
||||
const filePath = pathSlices.slice(2).join("/");
|
||||
|
||||
const baseDir = filesDirList.find((i) => i.name === baseName)?.path;
|
||||
if (!baseDir?.length) {
|
||||
throw new HTTPException(400, { message: "Path not found!" });
|
||||
}
|
||||
|
||||
return {
|
||||
path: [baseDir, filePath].join("/"),
|
||||
pathname: ["", baseName, filePath].join("/"),
|
||||
baseName,
|
||||
baseDir,
|
||||
filePath,
|
||||
};
|
||||
}
|
||||
|
||||
export default route;
|
||||
|
||||
Reference in New Issue
Block a user