mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
rgb-to-hex converter fully implemented
This commit is contained in:
parent
6d71eaef03
commit
557efd74ba
9 changed files with 228 additions and 19 deletions
29
backend/src/routes/colorconvert.route.ts
Normal file
29
backend/src/routes/colorconvert.route.ts
Normal 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!" });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
@ -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!" });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue