NOISSUE clean up and fix paste.ee upload

This commit is contained in:
Petr Mrázek 2017-10-09 22:50:15 +02:00
parent eba8e61ce9
commit 1388751fd4
2 changed files with 22 additions and 19 deletions

View File

@ -2,32 +2,33 @@
#include "Env.h" #include "Env.h"
#include <QDebug> #include <QDebug>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QFile>
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window) PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window)
{ {
m_key = key; m_key = key;
QByteArray temp; QByteArray temp;
temp = text.toUtf8(); QJsonObject topLevelObj;
temp.replace("\n", "\\n"); QJsonObject sectionObject;
m_textSize = temp.size(); sectionObject.insert("contents", text);
m_text = "{\"description\": \"MultiMC Log Upload\", \"sections\": [{\"contents\": \"" + temp + "\"}]}"; QJsonArray sectionArray;
QByteArray jsonDocument(m_text); sectionArray.append(sectionObject);
m_json = jsonDocument; topLevelObj.insert("description", "MultiMC Log Upload");
qDebug() << m_json; topLevelObj.insert("sections", sectionArray);
QJsonDocument docOut;
docOut.setObject(topLevelObj);
m_jsonContent = docOut.toJson();
} }
PasteUpload::~PasteUpload() PasteUpload::~PasteUpload()
{ {
if(buf)
{
delete buf;
}
} }
bool PasteUpload::validateText() bool PasteUpload::validateText()
{ {
return m_textSize <= maxSize(); return m_jsonContent.size() <= maxSize();
} }
void PasteUpload::executeTask() void PasteUpload::executeTask()
@ -36,10 +37,10 @@ void PasteUpload::executeTask()
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
request.setRawHeader("Content-Type", "application/json"); 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()); 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<QNetworkReply>(rep); m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Uploading to paste.ee")); setStatus(tr("Uploading to paste.ee"));
@ -57,10 +58,15 @@ void PasteUpload::downloadError(QNetworkReply::NetworkError error)
void PasteUpload::downloadFinished() 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 the download succeeded
if (m_reply->error() == QNetworkReply::NetworkError::NoError) if (m_reply->error() == QNetworkReply::NetworkError::NoError)
{ {
QByteArray data = m_reply->readAll();
m_reply.reset(); m_reply.reset();
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);

View File

@ -34,15 +34,12 @@ protected:
private: private:
bool parseResult(QJsonDocument doc); bool parseResult(QJsonDocument doc);
QByteArray m_text;
QString m_error; QString m_error;
QWidget *m_window; QWidget *m_window;
QString m_pasteID; QString m_pasteID;
QString m_pasteLink; QString m_pasteLink;
QString m_key; QString m_key;
int m_textSize = 0; QByteArray m_jsonContent;
QBuffer * buf = nullptr;
QByteArray m_json;
std::shared_ptr<QNetworkReply> m_reply; std::shared_ptr<QNetworkReply> m_reply;
public public
slots: slots: