Merge pull request #8 from theoleuthardt/rgb-to-hex

feat: rgb-to-hex tool
This commit is contained in:
Theo Leuthardt 2025-02-17 10:15:49 +01:00 committed by GitHub
commit a82a817664
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 443 additions and 137 deletions

3
backend/.prettierignore Normal file
View file

@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage

1
backend/.prettierrc Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -19,6 +19,7 @@
},
"devDependencies": {
"@types/node": "^22.13.4",
"prettier": "3.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
}
@ -758,6 +759,22 @@
"integrity": "sha512-e906FRY0+tV27iq4juKzSYPbUj2do2X2JX4EzSca1631EB2QJQUqGbDuERal7LCtOpxl6x3+nvo9NPZcmjkiFA==",
"license": "MIT"
},
"node_modules/prettier": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.1.tgz",
"integrity": "sha512-hPpFQvHwL3Qv5AdRvBFMhnKo4tYxp0ReXiPn2bxkiohEX6mBeBwEpBSQTkD458RaaDKQMYSp4hX4UtfUTA5wDw==",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/process-warning": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/process-warning/-/process-warning-4.0.1.tgz",

View file

@ -22,6 +22,7 @@
},
"devDependencies": {
"@types/node": "^22.13.4",
"prettier": "3.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
}

View file

@ -1,15 +1,17 @@
import Fastify from "fastify";
import cors from "@fastify/cors";
import multipart from '@fastify/multipart';
import multipart from "@fastify/multipart";
import { libreConvert } from "./src/routes/libreconvert.route";
import { colorConvert } from "./src/routes/colorconvert.route";
const app = Fastify({ logger: true });
app.register(cors, { origin: "*" });
app.register(multipart);
app.register(libreConvert);
app.register(colorConvert);
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}`);
});
console.log(`🚀Fastify is live on http://localhost:${PORT}`);
});

View file

@ -0,0 +1,29 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
interface RequestBody {
red: string;
green: string;
blue: string;
}
export async function colorConvert(app: FastifyInstance) {
app.post(
"/api/color-convert",
async (
request: FastifyRequest<{ Body: RequestBody }>,
reply: FastifyReply,
) => {
try {
const data = request.body;
if (!data) {
return reply.status(400).send({ error: "No RGB declared!" });
}
const hex = (`#${(+data.red).toString(16).padStart(2, "0")}${(+data.green).toString(16).padStart(2, "0")}${(+data.blue).toString(16).padStart(2, "0")}`).toUpperCase();
reply.header("Content-Type", "application/json").send({ hex: hex });
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}

View file

@ -5,26 +5,29 @@ 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!" });
}
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 ext = ".pdf";
const format = ext.substring(1);
const fileBuffer = await data.toBuffer();
const pdfBuffer = await libreConvertAsync(fileBuffer, ext, undefined);
const pdfBuffer = await libreConvertAsync(fileBuffer, ext, undefined);
reply
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!" });
}
});
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}