From ee89b895d4f04baa1ea493fe916b3d499c4b4ec4 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 20 Sep 2020 14:48:43 +0200 Subject: convert to jpg, basic file cleanup --- src/SimpleHandlers.cpp | 28 +++++++++++++++++++++------- 1 file 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 reply_vec; std::vector 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); } -- cgit v1.2.3-54-g00ecf