Commit e86872b6 authored by unknown's avatar unknown
Browse files

Merge branch 'michael-pdf'

parents 2b826d43 bed56497
Pipeline #171519 passed with stages
in 51 seconds
package ic.doc;
import ic.doc.web.DownloadFilePage;
import ic.doc.web.DownloadFileResponseData;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownDownload;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -32,8 +33,13 @@ public class WebServer {
if (query == null) {
new IndexPage().writeTo(resp);
} else if ("md".equals(download)) {
new MarkdownDownload(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
new DownloadFilePage(query, new QueryProcessor().process(query),
DownloadFileResponseData.MD).writeTo(resp);
} else if ("pdf".equals(download)){
new DownloadFilePage(query, new QueryProcessor().process(query),
DownloadFileResponseData.PDF).writeTo(resp);
}
else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
}
......
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class DownloadFilePage implements Page {
private final String query;
private final String answer;
private final DownloadFileResponseData downloadFileResponseData;
public DownloadFilePage(String query, String answer, DownloadFileResponseData downloadFileResponseData) {
String EMPTY_QUERY = "EMPTY";
this.query = (query.isEmpty()) ? EMPTY_QUERY : query;
this.answer = answer;
this.downloadFileResponseData = downloadFileResponseData;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File temp = this.downloadFileResponseData.getTargetFile(query, answer, query);
if (temp != null){
// "targetTemp" successfully created
resp.setContentType(this.downloadFileResponseData.getContentType());
resp.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"",
temp.getName()));
InputStream inputStream = new FileInputStream(temp);
OutputStream outputStream = resp.getOutputStream();
inputStream.transferTo(outputStream);
temp.delete();
}
else{
System.out.println("Failed to create target temp file");
}
}
}
package ic.doc.web;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public enum DownloadFileResponseData {
MD {
@Override
public String getFileExtension() {
return "md";
}
@Override
public String getContentType() {
return "text/markdown";
}
@Override
public File getTargetFile(String query, String answer,
String targetFileName) {
String targetFileNameWithExtension = getFileNameWithExtension(targetFileName);
this.ensureFileExists(targetFileNameWithExtension);
try {
FileWriter fileWriter = new FileWriter(targetFileNameWithExtension);
String content;
if (answer == null || answer.isEmpty()) {
content = "# Sorry\n Sorry, we didn't understand " + query;
}
else {
content = "# " + query + "\n" + answer;
}
fileWriter.write(content);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return new File(targetFileNameWithExtension);
}
},
PDF {
@Override
public String getFileExtension() {
return "pdf";
}
@Override
public String getContentType() {
return "application/pdf";
}
@Override
public File getTargetFile(String query, String answer,
String targetFileName) {
// Uses "sourceFile" to create another "targetFile" at the given "targetFilePath"
File mdFile = DownloadFileResponseData.MD.getTargetFile(query, answer, targetFileName);
// Then use pandoc on this result
ProcessBuilder pd = new ProcessBuilder();
List<String> command = new ArrayList<>();
// pandoc file.md -s -o file.pdf
command.add("pandoc");
command.add(mdFile.getName());
command.add("-s");
command.add("-o");
String fileNameWithExtension = this.getFileNameWithExtension(targetFileName);
command.add(fileNameWithExtension);
pd.command(command);
try {
Process p = pd.inheritIO().start();
p.waitFor();
} catch (Exception e) {
mdFile.delete();
return null;
}
mdFile.delete();
return new File(fileNameWithExtension);
}
};
public String getFileNameWithExtension(String fileName) {
return String.format("%s.%s", fileName, this.getFileExtension());
}
public void ensureFileExists(String filePath) {
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public abstract String getFileExtension();
public abstract String getContentType();
public abstract File getTargetFile(String query, String answer,
String targetFileName);
}
......@@ -21,6 +21,8 @@ public class IndexPage implements Page {
"<form>" +
"<input type=\"radio\" name=\"download\" value=\"md\">" +
"<label for=\"download\">Download Markdown</label><br>" +
"<input type=\"radio\" name=\"download\" value=\"pdf\">" +
"<label for=\"download\">Download PDF</label><br>" +
"<input type=\"radio\" name=\"download\" value=\"html\">" +
"<label for=\"download\">HTML</label><br>" +
"<p>Enter your query in the box below:<br>" +
......
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class MarkdownDownload implements Page {
private final String query;
private final String answer;
public MarkdownDownload(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/markdown");
resp.setHeader("Content-disposition", String.format("attachment; filename=%s.md", query));
File temp = new File(query + ".txt");
if (!temp.createNewFile()) {
System.out.println("File already exists.");
return;
}
FileWriter fileWriter = new FileWriter(query + ".txt");
if (answer == null || answer.isEmpty()) {
fileWriter.write("# Sorry\n Sorry, we didn't understand " + query);
} else {
fileWriter.write("# " + query + "\n" + answer);
}
fileWriter.close();
InputStream inputStream = new FileInputStream(temp);
OutputStream outputStream = resp.getOutputStream();
inputStream.transferTo(outputStream);
temp.delete();
}
}
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