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