added export for list of speeches

This commit is contained in:
s5260822 2025-03-22 22:48:14 +01:00
parent dabac9e316
commit edb91b7b72
3 changed files with 68 additions and 0 deletions

View file

@ -122,6 +122,21 @@ public class TeXUtil {
return convertTexToBase64PDF(tex.toString());
}
public static String getBulkExportedSpeechesBase64String(List<String> speechIds) throws IOException, InterruptedException {
StringBuilder tex = new StringBuilder();
tex.append(PREAMBLE.replace("$$EXPORTCATEGORY$$", "selected speeches"));
tex.append(BEGIN_DOCUMENT);
tex.append(TABLEOFCONTENTS);
for (String speechId : speechIds) {
tex.append(getSpeechToTexComponent(speechId));
tex.append(NEWPAGE);
}
tex.append(END_DOCUMENT);
return convertTexToBase64PDF(tex.toString());
}
public static String convertTexToBase64PDF(String tex) throws IOException, InterruptedException {
// Create a temporary directory
File tempDir = new File(TEMP_EXPORT_DIR);

View file

@ -70,5 +70,6 @@ public class RESTHandler {
app.get("/export/speaker/{id}", SpeechesExportController::exportSpeechesFromSpeaker); // exportiere alle Reden eines Parlamentariers als PDF
app.get("/export/topic/{topic}", SpeechesExportController::exportSpeechesWithTopic); // exportiere alle Reden zu einem Thema als PDF
app.get("/export/all", SpeechesExportController::exportAllSpeeches); // exportiere alle Reden als PDF CAUTION!!!: This will take forever but is required in the exercise
app.get("/export/speeches/{speechIds}", SpeechesExportController::exportSpeeches); // exportiere eine Liste von Reden als PDF
}
}

View file

@ -10,6 +10,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import static org.texttechnologylab.project.gruppe_05_1.export.TeXUtil.*;
@ -188,4 +189,55 @@ public class SpeechesExportController {
Logger.debug(Arrays.toString(e.getStackTrace()));
}
}
@OpenApi(
summary = "Get speeches by IDs as a PDF",
description = "Returns a LaTeX-generated PDF of the speeches specified by their IDs",
operationId = "getSpeechesByIds",
path = "/export/speeches/{speechIds}", // Comma-separated IDs
methods = HttpMethod.GET,
tags = {"Export", "Speeches", "PDF"},
responses = {
@OpenApiResponse(status = "200")
})
public static void exportSpeeches(Context ctx) {
byte[] pdfBytes = new byte[0];
try {
// Extract speech IDs from the path
String speechIdsParam = ctx.pathParam("speechIds");
List<String> speechIds = Arrays.asList(speechIdsParam.split(","));
// Generate PDF for given speech IDs
pdfBytes = Base64.getDecoder().decode(getBulkExportedSpeechesBase64String(speechIds));
} catch (Exception e) {
Logger.error("Failed to generate export for speeches: " + ctx.pathParam("speechIds"));
Logger.error(e.getMessage());
Logger.debug(Arrays.toString(e.getStackTrace()));
}
// Set response content type
ctx.contentType("application/pdf");
ByteArrayInputStream stream = new ByteArrayInputStream(pdfBytes);
if (stream.available() == 0) {
Logger.error("PDF stream is empty.");
ctx.result("Internal Server Error");
ctx.status(500);
return;
}
// Send the PDF as response
ctx.result(stream);
try {
// Clean up temporary files
deleteTeXTempDirContents();
Logger.debug("Temporary folder deleted.");
} catch (IOException e) {
Logger.error("Failed to delete temporary folder.");
Logger.error(e.getMessage());
Logger.debug(Arrays.toString(e.getStackTrace()));
}
}
}