From 1388751fd484f651345ff5adfad76dea64868d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 9 Oct 2017 22:50:15 +0200 Subject: [PATCH] NOISSUE clean up and fix paste.ee upload --- api/logic/net/PasteUpload.cpp | 36 ++++++++++++++++++++--------------- api/logic/net/PasteUpload.h | 5 +---- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/api/logic/net/PasteUpload.cpp b/api/logic/net/PasteUpload.cpp index 39557e2e..45a5a209 100644 --- a/api/logic/net/PasteUpload.cpp +++ b/api/logic/net/PasteUpload.cpp @@ -2,32 +2,33 @@ #include "Env.h" #include #include +#include #include +#include PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window) { m_key = key; QByteArray temp; - temp = text.toUtf8(); - temp.replace("\n", "\\n"); - m_textSize = temp.size(); - m_text = "{\"description\": \"MultiMC Log Upload\", \"sections\": [{\"contents\": \"" + temp + "\"}]}"; - QByteArray jsonDocument(m_text); - m_json = jsonDocument; - qDebug() << m_json; + QJsonObject topLevelObj; + QJsonObject sectionObject; + sectionObject.insert("contents", text); + QJsonArray sectionArray; + sectionArray.append(sectionObject); + topLevelObj.insert("description", "MultiMC Log Upload"); + topLevelObj.insert("sections", sectionArray); + QJsonDocument docOut; + docOut.setObject(topLevelObj); + m_jsonContent = docOut.toJson(); } PasteUpload::~PasteUpload() { - if(buf) - { - delete buf; - } } bool PasteUpload::validateText() { - return m_textSize <= maxSize(); + return m_jsonContent.size() <= maxSize(); } void PasteUpload::executeTask() @@ -36,10 +37,10 @@ void PasteUpload::executeTask() request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setRawHeader("Content-Type", "application/json"); - request.setRawHeader("Content-Length", QByteArray::number(m_text.size())); + request.setRawHeader("Content-Length", QByteArray::number(m_jsonContent.size())); request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str()); - QNetworkReply *rep = ENV.qnam().post(request, m_json); + QNetworkReply *rep = ENV.qnam().post(request, m_jsonContent); m_reply = std::shared_ptr(rep); setStatus(tr("Uploading to paste.ee")); @@ -57,10 +58,15 @@ void PasteUpload::downloadError(QNetworkReply::NetworkError error) void PasteUpload::downloadFinished() { + QByteArray data = m_reply->readAll(); + qDebug() << "RECEIVED" << data; + QFile out("/tmp/RECEIVED.json"); + out.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + out.write(data); + out.close(); // if the download succeeded if (m_reply->error() == QNetworkReply::NetworkError::NoError) { - QByteArray data = m_reply->readAll(); m_reply.reset(); QJsonParseError jsonError; QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); diff --git a/api/logic/net/PasteUpload.h b/api/logic/net/PasteUpload.h index d3a4e2e2..62d2e766 100644 --- a/api/logic/net/PasteUpload.h +++ b/api/logic/net/PasteUpload.h @@ -34,15 +34,12 @@ protected: private: bool parseResult(QJsonDocument doc); - QByteArray m_text; QString m_error; QWidget *m_window; QString m_pasteID; QString m_pasteLink; QString m_key; - int m_textSize = 0; - QBuffer * buf = nullptr; - QByteArray m_json; + QByteArray m_jsonContent; std::shared_ptr m_reply; public slots: