mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
feat: extended backend functionality for converting files to the selected file extension sent to the backend via api
This commit is contained in:
parent
2c582aaa60
commit
43abb6d5fd
2 changed files with 47 additions and 12 deletions
|
|
@ -5,7 +5,7 @@ import { libreConvert } from "./src/routes/libreconvert.route";
|
||||||
|
|
||||||
const app = Fastify({ logger: true });
|
const app = Fastify({ logger: true });
|
||||||
|
|
||||||
app.register(cors, { origin: "*" });
|
app.register(cors, { origin: "*", exposedHeaders: 'Content-Disposition' });
|
||||||
app.register(multipart);
|
app.register(multipart);
|
||||||
app.register(libreConvert);
|
app.register(libreConvert);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,62 @@
|
||||||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
import * as libre from "libreoffice-convert";
|
import * as libre from "libreoffice-convert";
|
||||||
import { promisify } from "util";
|
import { promisify } from "util";
|
||||||
|
import { MultipartValue } from "@fastify/multipart";
|
||||||
|
|
||||||
const libreConvertAsync = promisify(libre.convert);
|
const libreConvertAsync = promisify(libre.convert);
|
||||||
|
|
||||||
|
const mimeTypes: { [key: string]: string } = {
|
||||||
|
'pdf': 'application/pdf',
|
||||||
|
'html': 'text/html',
|
||||||
|
'doc': 'application/msword',
|
||||||
|
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||||
|
'txt': 'text/plain',
|
||||||
|
'rtf': 'application/rtf',
|
||||||
|
'odt': 'application/vnd.oasis.opendocument.text',
|
||||||
|
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
|
'xls': 'application/vnd.ms-excel',
|
||||||
|
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
|
||||||
|
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||||
|
'ppt': 'application/vnd.ms-powerpoint',
|
||||||
|
'odp': 'application/vnd.oasis.opendocument.presentation'
|
||||||
|
};
|
||||||
|
|
||||||
export async function libreConvert(app: FastifyInstance) {
|
export async function libreConvert(app: FastifyInstance) {
|
||||||
app.post("/api/libre-convert", async (request: FastifyRequest, reply: FastifyReply) => {
|
app.post("/api/libre-convert", async (request: FastifyRequest, reply: FastifyReply) => {
|
||||||
try {
|
try {
|
||||||
const data = await request.file();
|
const parts = request.parts();
|
||||||
if (!data) {
|
|
||||||
return reply.status(400).send({ error: "No file uploaded!" });
|
let fileBuffer: Buffer | null = null;
|
||||||
|
let outputFileExt = "";
|
||||||
|
|
||||||
|
for await (const part of parts) {
|
||||||
|
if (part.type === "file") {
|
||||||
|
fileBuffer = await part.toBuffer();
|
||||||
|
} else if (part.fieldname === "outputFormat" && part.type === "field") {
|
||||||
|
outputFileExt = (part as MultipartValue<string>).value;
|
||||||
|
console.log("Output format:", outputFileExt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ext = ".pdf";
|
if (!fileBuffer) {
|
||||||
const format = ext.substring(1);
|
return reply.status(400).send({ error: "No file uploaded!" });
|
||||||
const fileBuffer = await data.toBuffer();
|
}
|
||||||
|
if (!outputFileExt) {
|
||||||
|
return reply.status(400).send({ error: "No output format provided!" });
|
||||||
|
}
|
||||||
|
if (!outputFileExt.startsWith(".")) {
|
||||||
|
outputFileExt = "." + outputFileExt;
|
||||||
|
}
|
||||||
|
|
||||||
const pdfBuffer = await libreConvertAsync(fileBuffer, ext, undefined);
|
const format = outputFileExt.substring(1);
|
||||||
|
const mimeType = mimeTypes[format] || 'application/octet-stream';
|
||||||
|
|
||||||
|
const convertedBuffer = await libreConvertAsync(fileBuffer, outputFileExt, undefined);
|
||||||
|
|
||||||
reply
|
reply
|
||||||
.header("Content-Type", "application/" + format)
|
.header("Content-Type", mimeType)
|
||||||
.header("Content-Disposition", "attachment; filename=converted" + ext)
|
.header("Content-Disposition", `attachment; filename="converted${outputFileExt}"`)
|
||||||
.send(pdfBuffer);
|
.send(convertedBuffer);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Convert error:", error);
|
console.error("Convert error:", error);
|
||||||
reply.status(500).send({ error: "Error while converting!" });
|
reply.status(500).send({ error: "Error while converting!" });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue