Files
Flog/server/api.ts
T
2026-10-07 11:28:51 +02:00

55 lines
1.5 KiB
TypeScript

import express from "express";
import { basicAuth, CustomRequest } from "./utils";
import { Encryptor } from "./encryptor";
export const api = express.Router();
api.use(basicAuth as any);
api.use((req, res, next) => {
req.body = [];
req.on('data', chunk => {
req.body.push(chunk);
});
req.on('end', () => {
req.body = Buffer.concat(req.body);
next();
});
});
api.get("/check", async (req: any, res: any) => {
try {
const r = req as CustomRequest;
const result = await Encryptor.exist(r._user!.username, r._user!.password);
return res.send(!!result);
} catch (error) {
return res.sendStatus(500);
}
});
api.get("/:file", async (req: any, res: any) => {
try {
const r = req as CustomRequest;
const en = await Encryptor.createEncryptor(r._user!.username, r._user!.password, r.params.file as string);
const buffer = await en.decrypt();
if (buffer) {
res.send(buffer)
} else {
return res.sendStatus(404);
}
} catch (error) {
return res.sendStatus(403);
}
});
api.post("/:file", async (req: any, res: any) => {
try {
const r = req as CustomRequest;
const en = await Encryptor.createEncryptor(r._user!.username, r._user!.password, r.params.file as string);
en.encrypt(req.body);
return res.send(true);
} catch (error) {
console.error(error);
return res.sendStatus(403);
}
});