aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/SimpleHandlers.cpp28
1 files changed, 21 insertions, 7 deletions
diff --git a/src/SimpleHandlers.cpp b/src/SimpleHandlers.cpp
index 66d10d7..eba9331 100644
--- a/src/SimpleHandlers.cpp
+++ b/src/SimpleHandlers.cpp
@@ -215,30 +215,44 @@ std::string base64_encode_file(std::string const &path) {
json Handler::latexRenderHandler(std::string const &arguments, std::string const &session, void *payload) {
(void) payload;
- std::string file = exec("mktemp");
- file.pop_back(); // remove trailing new line
+ std::string png_file = exec("mktemp");
+ png_file.pop_back(); // remove trailing new line
+ std::string jpg_file = png_file + ".jpg";
- //std::cout << ("file " + file) << exec(("file " + file).c_str()) << std::endl;
+ //std::cout << ("png_file " + png_file) << exec(("png_file " + png_file).c_str()) << std::endl;
std::string result = exec(("curl -s 'https://latex.codecogs.com/png.latex?" + url_encode(arguments) + "' "
"-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0' "
"-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' "
"-H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'DNT: 1' -H 'Connection: keep-alive' "
- "-H 'Upgrade-Insecure-Requests: 1' --output " + file).c_str());
+ "-H 'Upgrade-Insecure-Requests: 1' --output " + png_file).c_str());
- //std::cout << ("file " + file) << exec(("file " + file).c_str()) << std::endl;
+ //std::cout << ("png_file " + png_file) << exec(("png_file " + png_file).c_str()) << std::endl;
if (!result.empty())
- return simple_response("Error: " + result + "\n"
+ return simple_response("Error getting image: " + result + "\n"
+ "Please report to an admin", session, false);
+
+ // convert to jpg, mainly to loose background transparency ( :( ), so the images work in dark theme clients too
+ result = exec(("convert " + png_file + " " + jpg_file).c_str());
+
+ if (!result.empty())
+ return simple_response("Error converting image: " + result + "\n"
"Please report to an admin", session, false);
std::vector<json> reply_vec;
std::vector<json> annotations;
annotations.emplace_back(
- create_annotation(Reply::AnnotationType::attachment, "latex.png;png;" + base64_encode_file(file)));
+ create_annotation(
+ Reply::AnnotationType::attachment,
+ "latex.png;png;" + base64_encode_file(jpg_file)));
reply_vec.emplace_back(create_text("", std::move(annotations)));
+ // single shot try to remove the files
+ std::remove(jpg_file.c_str());
+ std::remove(png_file.c_str());
+
return create_response(std::move(reply_vec), session, true);
}