feat: new separate frontend and backend folders with fastify as nodejs framework, libreconvert is configured in fastify as route

This commit is contained in:
theoleuthardt 2025-02-14 14:40:35 +01:00
parent 7b584d2fff
commit 9733de9a39
26 changed files with 1171 additions and 61 deletions

View file

@ -0,0 +1,30 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import * as libre from "libreoffice-convert";
import { promisify } from "util";
const libreConvertAsync = promisify(libre.convert);
export async function libreConvert(app: FastifyInstance) {
app.post("/api/libre-convert", async (request: FastifyRequest, reply: FastifyReply) => {
try {
const data = await request.file();
if (!data) {
return reply.status(400).send({ error: "No file uploaded!" });
}
const ext = ".pdf";
const format = ext.substring(1);
const fileBuffer = await data.toBuffer();
const pdfBuffer = await libreConvertAsync(fileBuffer, ext, undefined);
reply
.header("Content-Type", "application/" + format)
.header("Content-Disposition", "attachment; filename=converted" + ext)
.send(pdfBuffer);
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
});
}