2013-12-06 21:24:55 +00:00
|
|
|
#include "PasteUpload.h"
|
|
|
|
#include "MultiMC.h"
|
|
|
|
#include "logger/QsLog.h"
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include "gui/dialogs/CustomMessageBox.h"
|
2013-12-06 22:17:32 +00:00
|
|
|
#include <QDesktopServices>
|
2013-12-06 21:24:55 +00:00
|
|
|
|
|
|
|
PasteUpload::PasteUpload(QWidget *window, QString text) : m_text(text), m_window(window)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::executeTask()
|
|
|
|
{
|
|
|
|
QNetworkRequest request(QUrl("http://paste.ee/api"));
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
|
|
|
|
QByteArray content(
|
2014-07-03 19:29:44 +01:00
|
|
|
"key=public&description=MultiMC5+Log+File&language=plain&format=json&expire=2592000&paste=" +
|
2013-12-06 21:24:55 +00:00
|
|
|
m_text.toUtf8());
|
|
|
|
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
request.setRawHeader("Content-Length", QByteArray::number(content.size()));
|
|
|
|
|
|
|
|
auto worker = MMC->qnam();
|
|
|
|
QNetworkReply *rep = worker->post(request, content);
|
|
|
|
|
|
|
|
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
2014-11-09 01:09:01 +00:00
|
|
|
setStatus(tr("Uploading to paste.ee"));
|
2013-12-06 21:24:55 +00:00
|
|
|
connect(rep, &QNetworkReply::downloadProgress, [&](qint64 value, qint64 max)
|
2014-03-07 15:15:38 +00:00
|
|
|
{ setProgress(value / qMax((qint64)1, max) * 100); });
|
2013-12-06 21:24:55 +00:00
|
|
|
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.
|
|
|
|
QLOG_ERROR() << "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;
|
|
|
|
}
|
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();
|
|
|
|
auto status = object.value("status").toString("error");
|
|
|
|
if (status == "error")
|
|
|
|
{
|
2014-03-07 15:15:38 +00:00
|
|
|
QLOG_ERROR() << "paste.ee reported error:" << QString(object.value("error").toString());
|
2013-12-06 21:24:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-06 23:02:04 +01:00
|
|
|
m_pasteLink = object.value("paste").toObject().value("link").toString();
|
|
|
|
m_pasteID = object.value("paste").toObject().value("id").toString();
|
2013-12-06 21:24:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-15 14:00:09 +00:00
|
|
|
|