mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
fix: no console log and error anymore
This commit is contained in:
parent
8fe6657200
commit
80daad2700
2 changed files with 60 additions and 47 deletions
|
|
@ -6,60 +6,74 @@ import { MultipartValue } from "@fastify/multipart";
|
||||||
const libreConvertAsync = promisify(libre.convert);
|
const libreConvertAsync = promisify(libre.convert);
|
||||||
|
|
||||||
const mimeTypes: { [key: string]: string } = {
|
const mimeTypes: { [key: string]: string } = {
|
||||||
'pdf': 'application/pdf',
|
pdf: "application/pdf",
|
||||||
'html': 'text/html',
|
html: "text/html",
|
||||||
'doc': 'application/msword',
|
doc: "application/msword",
|
||||||
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
'txt': 'text/plain',
|
txt: "text/plain",
|
||||||
'rtf': 'application/rtf',
|
rtf: "application/rtf",
|
||||||
'odt': 'application/vnd.oasis.opendocument.text',
|
odt: "application/vnd.oasis.opendocument.text",
|
||||||
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
'xls': 'application/vnd.ms-excel',
|
xls: "application/vnd.ms-excel",
|
||||||
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
|
ods: "application/vnd.oasis.opendocument.spreadsheet",
|
||||||
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||||
'ppt': 'application/vnd.ms-powerpoint',
|
ppt: "application/vnd.ms-powerpoint",
|
||||||
'odp': 'application/vnd.oasis.opendocument.presentation'
|
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(
|
||||||
try {
|
"/api/libre-convert",
|
||||||
const parts = request.parts();
|
async (request: FastifyRequest, reply: FastifyReply) => {
|
||||||
|
try {
|
||||||
|
const parts = request.parts();
|
||||||
|
|
||||||
let fileBuffer: Buffer | null = null;
|
let fileBuffer: Buffer | null = null;
|
||||||
let outputFileExt = "";
|
let outputFileExt = "";
|
||||||
|
|
||||||
for await (const part of parts) {
|
for await (const part of parts) {
|
||||||
if (part.type === "file") {
|
if (part.type === "file") {
|
||||||
fileBuffer = await part.toBuffer();
|
fileBuffer = await part.toBuffer();
|
||||||
} else if (part.fieldname === "outputFormat" && part.type === "field") {
|
} else if (
|
||||||
outputFileExt = (part as MultipartValue<string>).value;
|
part.fieldname === "outputFormat" &&
|
||||||
console.log("Output format:", outputFileExt);
|
part.type === "field"
|
||||||
|
) {
|
||||||
|
outputFileExt = (part as MultipartValue<string>).value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!fileBuffer) {
|
if (!fileBuffer) {
|
||||||
return reply.status(400).send({ error: "No file uploaded!" });
|
return reply.status(400).send({ error: "No file uploaded!" });
|
||||||
}
|
}
|
||||||
if (!outputFileExt) {
|
if (!outputFileExt) {
|
||||||
return reply.status(400).send({ error: "No output format provided!" });
|
return reply
|
||||||
}
|
.status(400)
|
||||||
if (!outputFileExt.startsWith(".")) {
|
.send({ error: "No output format provided!" });
|
||||||
outputFileExt = "." + outputFileExt;
|
}
|
||||||
}
|
if (!outputFileExt.startsWith(".")) {
|
||||||
|
outputFileExt = "." + outputFileExt;
|
||||||
|
}
|
||||||
|
|
||||||
const format = outputFileExt.substring(1);
|
const format = outputFileExt.substring(1);
|
||||||
const mimeType = mimeTypes[format] || 'application/octet-stream';
|
const mimeType = mimeTypes[format] || "application/octet-stream";
|
||||||
|
|
||||||
const convertedBuffer = await libreConvertAsync(fileBuffer, outputFileExt, undefined);
|
const convertedBuffer = await libreConvertAsync(
|
||||||
|
fileBuffer,
|
||||||
|
outputFileExt,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
reply
|
reply
|
||||||
.header("Content-Type", mimeType)
|
.header("Content-Type", mimeType)
|
||||||
.header("Content-Disposition", `attachment; filename="converted${outputFileExt}"`)
|
.header(
|
||||||
|
"Content-Disposition",
|
||||||
|
`attachment; filename="converted${outputFileExt}"`,
|
||||||
|
)
|
||||||
.send(convertedBuffer);
|
.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!" });
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ export default function DocConverter() {
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
if (event.target.files && event.target.files.length > 0) {
|
if (event.target.files && event.target.files.length > 0) {
|
||||||
const selectedFile = event.target.files[0];
|
const selectedFile = event.target.files[0];
|
||||||
const fileExtension = selectedFile.name.split(".").pop()?.toLowerCase();
|
const fileExtension = selectedFile.name.split(".").pop()?.toLowerCase();
|
||||||
|
|
@ -28,7 +30,6 @@ export default function DocConverter() {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!isSupported) {
|
if (!isSupported) {
|
||||||
console.error("Not supported file uploaded!");
|
|
||||||
alert("File format not supported!");
|
alert("File format not supported!");
|
||||||
event.target.value = "";
|
event.target.value = "";
|
||||||
return;
|
return;
|
||||||
|
|
@ -70,9 +71,7 @@ export default function DocConverter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
console.log("Blob:", blob);
|
|
||||||
const url = window.URL.createObjectURL(blob);
|
const url = window.URL.createObjectURL(blob);
|
||||||
console.log("Download URL:", url);
|
|
||||||
setDownloadUrl(url);
|
setDownloadUrl(url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error while converting:", error);
|
console.error("Error while converting:", error);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue