added 500 code when pdf generation goes wrong

This commit is contained in:
s5260822 2025-03-22 20:46:42 +01:00
parent 6d06aae46c
commit 417aee9cc1

View file

@ -8,6 +8,7 @@ import org.texttechnologylab.project.gruppe_05_1.util.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import static org.texttechnologylab.project.gruppe_05_1.export.TeXUtil.*;
@ -23,22 +24,38 @@ public class SpeechesExportController {
responses = {
@OpenApiResponse(status = "200")
})
public static void exportSpeech(Context ctx) throws IOException {
public static void exportSpeech(Context ctx) {
byte[] pdfBytes = new byte[0];
try {
pdfBytes = Base64.getDecoder().decode(getExportedSpeechBase64StringBySpeechId(ctx.pathParam("id")));
} catch (Exception e) {
Logger.error("Failed to generate Export of Speech with ID " + ctx.pathParam("id"));
Logger.error(e.getMessage());
Logger.debug(Arrays.toString(e.getStackTrace()));
}
// Set the response content type to PDF
ctx.contentType("application/pdf");
// Send the PDF as a response
ctx.result(new ByteArrayInputStream(pdfBytes));
ByteArrayInputStream stream = new ByteArrayInputStream(pdfBytes);
// delete the temporary folder
deleteTeXTempDirContents();
Logger.pink(String.valueOf(stream.available()));
if (stream.available() == 0) {
Logger.error("PDF stream is empty.");
ctx.result("Internal Server Error");
ctx.status(500);
return;
}
// Send the PDF as a response
ctx.result(stream);
try {
// delete the temporary folder
deleteTeXTempDirContents();
} catch (IOException e) {
Logger.error("Failed to delete temporary folder.");
Logger.error(e.getMessage());
Logger.debug(Arrays.toString(e.getStackTrace()));
}
}
}