pollymc/api/logic/net/PasteUpload.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

#include "PasteUpload.h"
2015-02-09 06:21:14 +05:30
#include "Env.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
2015-10-02 03:42:53 +05:30
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window)
{
2015-10-02 03:42:53 +05:30
m_key = key;
QByteArray temp;
temp = text.toUtf8();
2017-08-23 01:19:47 +05:30
temp.replace("\n", "\\n");
2015-10-02 03:42:53 +05:30
m_textSize = temp.size();
2017-08-23 01:19:47 +05:30
m_text = "{\"description\": \"MultiMC Log Upload\", \"sections\": [{\"contents\": \"" + temp + "\"}]}";
QByteArray jsonDocument(m_text);
m_json = jsonDocument;
qDebug() << m_json;
2015-10-02 03:42:53 +05:30
}
PasteUpload::~PasteUpload()
{
if(buf)
{
delete buf;
}
}
bool PasteUpload::validateText()
{
2015-10-02 03:42:53 +05:30
return m_textSize <= maxSize();
}
void PasteUpload::executeTask()
{
2017-08-23 01:19:47 +05:30
QNetworkRequest request(QUrl("https://api.paste.ee/v1/pastes"));
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
2015-10-02 03:42:53 +05:30
2017-08-23 01:19:47 +05:30
request.setRawHeader("Content-Type", "application/json");
2015-10-02 03:42:53 +05:30
request.setRawHeader("Content-Length", QByteArray::number(m_text.size()));
2017-08-23 01:19:47 +05:30
request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str());
2017-08-23 01:19:47 +05:30
QNetworkReply *rep = ENV.qnam().post(request, m_json);
m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Uploading to paste.ee"));
2015-10-02 03:42:53 +05:30
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
void PasteUpload::downloadError(QNetworkReply::NetworkError error)
{
// error happened during download.
qCritical() << "Network error: " << error;
emitFailed(m_reply->errorString());
}
void PasteUpload::downloadFinished()
{
// 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);
if (jsonError.error != QJsonParseError::NoError)
{
emitFailed(jsonError.errorString());
return;
}
if (!parseResult(doc))
{
emitFailed(tr("paste.ee returned an error. Please consult the logs for more information"));
return;
}
}
// else the download failed
else
{
2013-12-07 03:43:09 +05:30
emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
m_reply.reset();
return;
}
emitSucceeded();
}
bool PasteUpload::parseResult(QJsonDocument doc)
{
auto object = doc.object();
2017-08-23 01:19:47 +05:30
auto status = object.value("success").toBool();
if (!status)
{
qCritical() << "paste.ee reported error:" << QString(object.value("error").toString());
return false;
}
2017-08-23 01:19:47 +05:30
m_pasteLink = object.value("link").toString();
m_pasteID = object.value("id").toString();
qDebug() << m_pasteLink;
return true;
}