2013-12-07 02:54:55 +05:30
|
|
|
#include "PasteUpload.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "Env.h"
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2013-12-07 02:54:55 +05:30
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
2015-10-02 03:42:53 +05:30
|
|
|
PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window)
|
2013-12-07 02:54:55 +05:30
|
|
|
{
|
2015-10-02 03:42:53 +05:30
|
|
|
m_key = key;
|
|
|
|
QByteArray temp;
|
|
|
|
temp = text.toUtf8();
|
|
|
|
temp.replace('\n', "\r\n");
|
|
|
|
m_textSize = temp.size();
|
|
|
|
m_text = "key=" + m_key.toLatin1() + "&description=MultiMC5+Log+File&language=plain&format=json&expire=2592000&paste=" + temp.toPercentEncoding();
|
|
|
|
buf = new QBuffer(&m_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
PasteUpload::~PasteUpload()
|
|
|
|
{
|
|
|
|
if(buf)
|
|
|
|
{
|
|
|
|
delete buf;
|
|
|
|
}
|
2015-01-11 07:38:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool PasteUpload::validateText()
|
|
|
|
{
|
2015-10-02 03:42:53 +05:30
|
|
|
return m_textSize <= maxSize();
|
2013-12-07 02:54:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::executeTask()
|
|
|
|
{
|
|
|
|
QNetworkRequest request(QUrl("http://paste.ee/api"));
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
|
2015-10-02 03:42:53 +05:30
|
|
|
|
2013-12-07 02:54:55 +05:30
|
|
|
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
|
2015-10-02 03:42:53 +05:30
|
|
|
request.setRawHeader("Content-Length", QByteArray::number(m_text.size()));
|
2013-12-07 02:54:55 +05:30
|
|
|
|
2016-11-07 02:28:54 +05:30
|
|
|
QNetworkReply *rep = ENV.qnam().post(request, buf);
|
2013-12-07 02:54:55 +05:30
|
|
|
|
|
|
|
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
2014-11-09 06:39:01 +05:30
|
|
|
setStatus(tr("Uploading to paste.ee"));
|
2015-10-02 03:42:53 +05:30
|
|
|
connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
|
2015-07-26 21:25:29 +05:30
|
|
|
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
|
2013-12-07 02:54:55 +05:30
|
|
|
connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasteUpload::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
|
|
|
// error happened during download.
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Network error: " << error;
|
2013-12-07 02:54:55 +05:30
|
|
|
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 20:45:38 +05:30
|
|
|
if (!parseResult(doc))
|
2013-12-07 02:54:55 +05:30
|
|
|
{
|
2014-03-07 20:45:38 +05:30
|
|
|
emitFailed(tr("paste.ee returned an error. Please consult the logs for more information"));
|
2013-12-07 02:54:55 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else the download failed
|
|
|
|
else
|
|
|
|
{
|
2013-12-07 03:43:09 +05:30
|
|
|
emitFailed(QString("Network error: %1").arg(m_reply->errorString()));
|
2013-12-07 02:54:55 +05:30
|
|
|
m_reply.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:45:38 +05:30
|
|
|
bool PasteUpload::parseResult(QJsonDocument doc)
|
2013-12-07 02:54:55 +05:30
|
|
|
{
|
|
|
|
auto object = doc.object();
|
|
|
|
auto status = object.value("status").toString("error");
|
|
|
|
if (status == "error")
|
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "paste.ee reported error:" << QString(object.value("error").toString());
|
2013-12-07 02:54:55 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-07-07 03:32:04 +05:30
|
|
|
m_pasteLink = object.value("paste").toObject().value("link").toString();
|
|
|
|
m_pasteID = object.value("paste").toObject().value("id").toString();
|
2013-12-07 02:54:55 +05:30
|
|
|
return true;
|
|
|
|
}
|
2013-12-15 19:30:09 +05:30
|
|
|
|