mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
feat: new separate frontend and backend folders with fastify as nodejs framework, libreconvert is configured in fastify as route
This commit is contained in:
parent
7b584d2fff
commit
9733de9a39
26 changed files with 1171 additions and 61 deletions
30
backend/src/routes/libreconvert.route.ts
Normal file
30
backend/src/routes/libreconvert.route.ts
Normal 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!" });
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue