switch to new paste.ee API

This commit is contained in:
Joona 2017-08-22 12:49:47 -07:00 committed by Petr Mrázek
parent 140c31293c
commit 4d54d2662a
2 changed files with 16 additions and 10 deletions

View File

@ -9,10 +9,12 @@ PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(
m_key = key; m_key = key;
QByteArray temp; QByteArray temp;
temp = text.toUtf8(); temp = text.toUtf8();
temp.replace('\n', "\r\n"); temp.replace("\n", "\\n");
m_textSize = temp.size(); m_textSize = temp.size();
m_text = "key=" + m_key.toLatin1() + "&description=MultiMC5+Log+File&language=plain&format=json&expire=2592000&paste=" + temp.toPercentEncoding(); m_text = "{\"description\": \"MultiMC Log Upload\", \"sections\": [{\"contents\": \"" + temp + "\"}]}";
buf = new QBuffer(&m_text); QByteArray jsonDocument(m_text);
m_json = jsonDocument;
qDebug() << m_json;
} }
PasteUpload::~PasteUpload() PasteUpload::~PasteUpload()
@ -30,13 +32,15 @@ bool PasteUpload::validateText()
void PasteUpload::executeTask() void PasteUpload::executeTask()
{ {
QNetworkRequest request(QUrl("https://paste.ee/api")); QNetworkRequest request(QUrl("https://api.paste.ee/v1/pastes"));
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", QByteArray::number(m_text.size())); request.setRawHeader("Content-Length", QByteArray::number(m_text.size()));
request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str());
qDebug() << m_key.toStdString().c_str();
QNetworkReply *rep = ENV.qnam().post(request, buf); QNetworkReply *rep = ENV.qnam().post(request, m_json);
m_reply = std::shared_ptr<QNetworkReply>(rep); m_reply = std::shared_ptr<QNetworkReply>(rep);
setStatus(tr("Uploading to paste.ee")); setStatus(tr("Uploading to paste.ee"));
@ -85,14 +89,15 @@ void PasteUpload::downloadFinished()
bool PasteUpload::parseResult(QJsonDocument doc) bool PasteUpload::parseResult(QJsonDocument doc)
{ {
auto object = doc.object(); auto object = doc.object();
auto status = object.value("status").toString("error"); auto status = object.value("success").toBool();
if (status == "error") if (!status)
{ {
qCritical() << "paste.ee reported error:" << QString(object.value("error").toString()); qCritical() << "paste.ee reported error:" << QString(object.value("error").toString());
return false; return false;
} }
m_pasteLink = object.value("paste").toObject().value("link").toString(); m_pasteLink = object.value("link").toString();
m_pasteID = object.value("paste").toObject().value("id").toString(); m_pasteID = object.value("id").toString();
qDebug() << m_pasteLink;
return true; return true;
} }

View File

@ -42,6 +42,7 @@ private:
QString m_key; QString m_key;
int m_textSize = 0; int m_textSize = 0;
QBuffer * buf = nullptr; QBuffer * buf = nullptr;
QByteArray m_json;
std::shared_ptr<QNetworkReply> m_reply; std::shared_ptr<QNetworkReply> m_reply;
public public
slots: slots: