summaryrefslogtreecommitdiff
path: root/meshmc/launcher/net/PasteUpload.cpp
diff options
context:
space:
mode:
authorMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:45:07 +0300
committerMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:45:07 +0300
commit31b9a8949ed0a288143e23bf739f2eb64fdc63be (patch)
tree8a984fa143c38fccad461a77792d6864f3e82cd3 /meshmc/launcher/net/PasteUpload.cpp
parent934382c8a1ce738589dee9ee0f14e1cec812770e (diff)
parentfad6a1066616b69d7f5fef01178efdf014c59537 (diff)
downloadProject-Tick-31b9a8949ed0a288143e23bf739f2eb64fdc63be.tar.gz
Project-Tick-31b9a8949ed0a288143e23bf739f2eb64fdc63be.zip
Add 'meshmc/' from commit 'fad6a1066616b69d7f5fef01178efdf014c59537'
git-subtree-dir: meshmc git-subtree-mainline: 934382c8a1ce738589dee9ee0f14e1cec812770e git-subtree-split: fad6a1066616b69d7f5fef01178efdf014c59537
Diffstat (limited to 'meshmc/launcher/net/PasteUpload.cpp')
-rw-r--r--meshmc/launcher/net/PasteUpload.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/meshmc/launcher/net/PasteUpload.cpp b/meshmc/launcher/net/PasteUpload.cpp
new file mode 100644
index 0000000000..099453c87b
--- /dev/null
+++ b/meshmc/launcher/net/PasteUpload.cpp
@@ -0,0 +1,124 @@
+/* SPDX-FileCopyrightText: 2026 Project Tick
+ * SPDX-FileContributor: Project Tick
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * MeshMC - A Custom Launcher for Minecraft
+ * Copyright (C) 2026 Project Tick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "PasteUpload.h"
+#include "BuildConfig.h"
+#include "Application.h"
+
+#include <QDebug>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QFile>
+
+PasteUpload::PasteUpload(QWidget* window, QString text, QString key)
+ : m_window(window)
+{
+ m_key = key;
+ QByteArray temp;
+ QJsonObject topLevelObj;
+ QJsonObject sectionObject;
+ sectionObject.insert("contents", text);
+ QJsonArray sectionArray;
+ sectionArray.append(sectionObject);
+ topLevelObj.insert("description", "Log Upload");
+ topLevelObj.insert("sections", sectionArray);
+ QJsonDocument docOut;
+ docOut.setObject(topLevelObj);
+ m_jsonContent = docOut.toJson();
+}
+
+PasteUpload::~PasteUpload() {}
+
+bool PasteUpload::validateText()
+{
+ return m_jsonContent.size() <= maxSize();
+}
+
+void PasteUpload::executeTask()
+{
+ QNetworkRequest request(QUrl("https://api.paste.ee/v1/pastes"));
+ request.setHeader(QNetworkRequest::UserAgentHeader,
+ BuildConfig.USER_AGENT_UNCACHED);
+
+ 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());
+
+ QNetworkReply* rep = APPLICATION->network()->post(request, m_jsonContent);
+
+ m_reply = std::shared_ptr<QNetworkReply>(rep);
+ setStatus(tr("Uploading to paste.ee"));
+ connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress);
+ connect(rep, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this,
+ SLOT(downloadError(QNetworkReply::NetworkError)));
+ connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished()));
+}
+
+void PasteUpload::downloadError(QNetworkReply::NetworkError error)
+{
+ // error happened during download.
+ qCritical() << "Network error: " << error;
+ emitFailed(m_reply->errorString());
+}
+
+void PasteUpload::downloadFinished()
+{
+ 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();
+}
+
+bool PasteUpload::parseResult(QJsonDocument doc)
+{
+ 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;
+}