+ // TODO: Use SSE to run this in the background and notify client when process is done.
+
+ @Path("ankiExport")
+ @POST
+ @Produces(MediaType.APPLICATION_OCTET_STREAM)
+ public Response exportToAnki(
+ @RestForm("lang") String lang,
+ @PartType(MediaType.MULTIPART_FORM_DATA)
+ @RestForm("anki-export") FileUpload fileUpload
+ ) throws Exception {
+ final var file = fileUpload.uploadedFile().toFile();
+ final var sb = new StringBuilder(
+ """
+ #separator:Tab
+ #html:true
+ #notetype:Basic
+ #deck:%s
+ """.formatted("CHANGEME") // TODO: Allow customizing deck name.
+ );
+
+ try (
+ final var reader = new InputStreamReader(new FileInputStream(file));
+ final var br = new BufferedReader(reader)
+ ) {
+ final var words = br.lines().toList();
+ for (final var word : words) {
+ final var def = definitions.define(word, lang);
+ sb.append(def.word())
+ .append("\t")
+ .append("<ul>")
+ .append(
+ def.definitions()
+ .stream()
+ .limit(3)
+ .map("<li>%s</li>"::formatted)
+ .collect(Collectors.joining())
+ )
+ .append("</ul>")
+ .append("\n");
+ Thread.sleep(500);
+ }
+ }
+
+ final var contents = sb.toString();
+ return Response.ok()
+ .header("Content-Disposition", "attachment; filename=\"anki.txt\"")
+ .header("Content-Length", contents.length())
+ .entity(contents)
+ .build();
+ }
+