Commit a35ab656 authored by jan.koester's avatar jan.koester
Browse files

test

parent 2b314c44
Loading
Loading
Loading
Loading
+99 −21
Original line number Diff line number Diff line
@@ -757,28 +757,106 @@ void webedit::Api::handlePreview(libhttppp::HttpRequest &curreq,

void webedit::Api::handlePreviewPage(libhttppp::HttpRequest &curreq,
                                      const std::string &sessionid) {
    // Find an active blog connection for rendering
    std::string authid, blogUrl;
    {
        std::lock_guard<std::mutex> lk(_connSessionMtx);
        if (!_connSessions.empty()) {
            auto it = _connSessions.begin();
            authid = it->second.authid;
            blogUrl = it->second.blogUrl;
        }
    }

    if (authid.empty() || blogUrl.empty()) {
        std::string page = "<!DOCTYPE html><html><body style=\"display:flex;"
            "align-items:center;justify-content:center;height:100vh;margin:0;"
            "font-family:sans-serif;color:#999;\">"
            "<p>Bitte zuerst eine Verbindung herstellen.</p></body></html>";
        libhttppp::HttpResponse curres;
        curres.setState(HTTP200);
        curres.setContentType("text/html");
        curres.send(curreq, page.data(), page.size());
        return;
    }

    // Export the current document as XML
    std::string xmlData;
    {
        auto &doc = getDocState(sessionid);
        std::lock_guard<std::mutex> lk(doc.mtx);
        xmlData = exportTreeXml(doc);
    }

    std::string widgetHtml = renderTree(doc.root);
    try {
        // 1. Send ImportXml to blog server
        json_object *importArr = json_object_new_array();
        json_object *authObj = json_object_new_object();
        json_object_object_add(authObj, "authid", json_object_new_string(authid.c_str()));
        json_object_array_add(importArr, authObj);
        json_object *importCmd = json_object_new_object();
        json_object_object_add(importCmd, "command", json_object_new_string("ImportXml"));
        json_object_object_add(importCmd, "xml", json_object_new_string(xmlData.c_str()));
        json_object_array_add(importArr, importCmd);

        json_object *importResp = blogApiCall(blogUrl, importArr);
        json_object_put(importArr);
        if (importResp) json_object_put(importResp);

        // 2. Send ExportHtml to get the rendered HTML
        json_object *exportArr = json_object_new_array();
        json_object *authObj2 = json_object_new_object();
        json_object_object_add(authObj2, "authid", json_object_new_string(authid.c_str()));
        json_object_array_add(exportArr, authObj2);
        json_object *exportCmd = json_object_new_object();
        json_object_object_add(exportCmd, "command", json_object_new_string("ExportHtml"));
        json_object_array_add(exportArr, exportCmd);

        json_object *exportResp = blogApiCall(blogUrl, exportArr);
        json_object_put(exportArr);

        if (!exportResp) {
            sendJsonError(curreq, 502, "No response from blog server");
            return;
        }

    // Build a complete HTML page
        // Extract rendered HTML from response
        std::string renderedHtml;
        if (json_object_is_type(exportResp, json_type_array)) {
            size_t len = json_object_array_length(exportResp);
            for (size_t i = 0; i < len; ++i) {
                json_object *item = json_object_array_get_idx(exportResp, i);
                json_object *htmlObj = nullptr;
                if (json_object_object_get_ex(item, "html", &htmlObj)) {
                    const char *h = json_object_get_string(htmlObj);
                    if (h) renderedHtml = h;
                }
            }
        }
        json_object_put(exportResp);

        // Build complete HTML page
        std::string page;
        page += "<!DOCTYPE html>\n<html>\n<head>\n";
        page += "<meta charset=\"utf-8\">\n";
        page += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";
    page += "<style>\n";
    page += "html, body { margin: 0; padding: 0; }\n";
    page += "body { font-family: sans-serif; }\n";
    page += "</style>\n";
        page += "</head>\n<body>\n";
    page += widgetHtml;
        page += renderedHtml;
        page += "\n</body>\n</html>";

        libhttppp::HttpResponse curres;
        curres.setState(HTTP200);
        curres.setContentType("text/html");
        curres.send(curreq, page.data(), page.size());

    } catch (const std::exception &e) {
        std::string errPage = "<!DOCTYPE html><html><body><p>Preview error: " +
                              std::string(e.what()) + "</p></body></html>";
        libhttppp::HttpResponse curres;
        curres.setState(HTTP200);
        curres.setContentType("text/html");
        curres.send(curreq, errPage.data(), errPage.size());
    }
}

// --- Export XML ---