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

1046
backend/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

28
backend/package.json Normal file
View file

@ -0,0 +1,28 @@
{
"name": "backend",
"version": "1.0.0",
"main": "server.ts",
"scripts": {
"dev": "ts-node server.ts",
"build": "tsc",
"start": "node dist/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@fastify/cors": "^10.0.2",
"@fastify/multipart": "^9.0.3",
"dotenv": "^16.4.7",
"fastify": "^5.2.1",
"fastify-cors": "^6.0.3",
"fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0"
},
"devDependencies": {
"@types/node": "^22.13.4",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
}
}

15
backend/server.ts Normal file
View file

@ -0,0 +1,15 @@
import Fastify from "fastify";
import cors from "@fastify/cors";
import multipart from '@fastify/multipart';
import { libreConvert } from "./src/routes/libreconvert.route";
const app = Fastify({ logger: true });
app.register(cors, { origin: "*" });
app.register(multipart);
app.register(libreConvert);
const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {
console.log(`🚀Fastify is live on http://localhost:${PORT}`);
});

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!" });
}
});
}

10
backend/tsconfig.json Normal file
View file

@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true
}
}