Commit 2042d9a8 authored by Zhili Tian's avatar Zhili Tian
Browse files

optimise functionality

parent 1e838533
Pipeline #173743 passed with stages
in 3 minutes and 46 seconds
package ic.doc.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
import java.nio.file.Files;
import javax.servlet.http.HttpServletResponse;
......@@ -18,24 +15,34 @@ public class MarkdownResultPage {
}
public void writeTo(HttpServletResponse resp) throws IOException {
// creating a temporary file.
File temp = new File("temp.md");
// writing the result of the query.
final PrintStream out = new PrintStream(new FileOutputStream(temp));
if (answer == null || answer.isEmpty()) {
out.printf("Sorry, we didn't understand %s", query);
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("</body>");
writer.println("</html>");
} else {
File temp = new File("temp.md");
final PrintStream out = new PrintStream(new FileOutputStream(temp));
out.printf("# %s\n\n%s\n", query, answer);
}
out.close();
out.close();
// serving the file.
resp.setContentType("application/octet-stream");
resp.setContentLengthLong(temp.length());
resp.setHeader("Content-Disposition", "filename=query.md");
Files.copy(temp.toPath(), resp.getOutputStream());
resp.setContentType("application/octet-stream");
resp.setContentLengthLong(temp.length());
resp.setHeader("Content-Disposition", "filename=result.md");
Files.copy(temp.toPath(), resp.getOutputStream());
temp.deleteOnExit();
temp.deleteOnExit();
}
}
}
......@@ -17,38 +17,43 @@ public class PDFResultPage implements Page{
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
// creating a temporary file.
File temp = new File("temp.md");
File pdf = new File("temp.pdf");
// writing the result of the query.
final PrintStream out = new PrintStream(new FileOutputStream(temp));
if (answer == null || answer.isEmpty()) {
out.printf("Sorry, we didn't understand %s", query);
} else {
out.printf("# %s\n\n%s\n", query, answer);
}
out.close();
try {
Process process = new ProcessBuilder().command("pandoc", "-s", "-o",
pdf.getPath(), temp.getPath()).start();
process.waitFor();
} catch (InterruptedException e) {
System.out.println("converting failed");
}
// serving the file.
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment;filename=query.pdf");
// Files.copy(pdf.toPath(), resp.getOutputStream());
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
writer.println("</body>");
writer.println("</html>");
InputStream targetStream = new FileInputStream(pdf);
targetStream.transferTo(resp.getOutputStream());
targetStream.close();
} else {
File temp = new File("temp.md");
File pdf = new File("temp.pdf");
temp.deleteOnExit();
pdf.deleteOnExit();
final PrintStream out = new PrintStream(new FileOutputStream(temp));
out.printf("# %s\n\n%s\n", query, answer);
out.close();
try {
Process process = new ProcessBuilder().command("pandoc", "-s", "-o",
pdf.getPath(), temp.getPath()).start();
process.waitFor();
} catch (InterruptedException e) {
System.out.println("converting failed");
}
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment;filename=result.pdf");
Files.copy(pdf.toPath(), resp.getOutputStream());
temp.deleteOnExit();
pdf.deleteOnExit();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment