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>
|
2017-10-09 20:50:15 +00:00
|
|
|
#include <QJsonArray>
|
2013-12-06 21:24:55 +00:00
|
|
|
#include <QJsonDocument>
|
2017-10-09 20:50:15 +00:00
|
|
|
#include <QFile>
|
2021-07-01 18:24:29 +00:00
|
|
|
#include <BuildConfig.h>
|
2013-12-06 21:24:55 +00:00
|
|
|
|
2015-10-01 22:12:53 +00:00
|
|
|
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window)
|
2013-12-06 21:24:55 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_key = key;
|
|
|
|
QByteArray temp;
|
|
|
|
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();
|
2015-10-01 22:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PasteUpload::~PasteUpload()
|
|
|
|
{
|
2015-01-11 02:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PasteUpload::validateText()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return m_jsonContent.size() <= maxSize();
|
2013-12-06 21:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::executeTask()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QNetworkRequest request(QUrl("https://api.paste.ee/v1/pastes"));
|
2021-07-01 18:24:29 +00:00
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED);
|
2015-10-01 22:12:53 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
request.setRawHeader("Content-Type", "application/json");
|
|
|
|
request.setRawHeader("Content-Length", QByteArray::number(m_jsonContent.size()));
|
|
|
|
request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str());
|
2013-12-06 21:24:55 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QNetworkReply *rep = ENV.qnam().post(request, m_jsonContent);
|
2013-12-06 21:24:55 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
|
|
|
setStatus(tr("Uploading to paste.ee"));
|
|
|
|
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()));
|
2013-12-06 21:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
// error happened during download.
|
|
|
|
qCritical() << "Network error: " << error;
|
|
|
|
emitFailed(m_reply->errorString());
|
2013-12-06 21:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::downloadFinished()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QByteArray data = m_reply->readAll();
|
|
|
|
// if the download succeeded
|
|
|
|
if (m_reply->error() == QNetworkReply::NetworkError::NoError)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
|
|
|
|
m_reply.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
2013-12-06 21:24:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:15:38 +00:00
|
|
|
bool PasteUpload::parseResult(QJsonDocument doc)
|
2013-12-06 21:24:55 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto object = doc.object();
|
|
|
|
auto status = object.value("success").toBool();
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
qCritical() << "paste.ee reported error:" << QString(object.value("error").toString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_pasteLink = object.value("link").toString();
|
|
|
|
m_pasteID = object.value("id").toString();
|
|
|
|
qDebug() << m_pasteLink;
|
|
|
|
return true;
|
2013-12-06 21:24:55 +00:00
|
|
|
}
|
2013-12-15 14:00:09 +00:00
|
|
|
|