diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:45:07 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:45:07 +0300 |
| commit | 31b9a8949ed0a288143e23bf739f2eb64fdc63be (patch) | |
| tree | 8a984fa143c38fccad461a77792d6864f3e82cd3 /meshmc/launcher/screenshots | |
| parent | 934382c8a1ce738589dee9ee0f14e1cec812770e (diff) | |
| parent | fad6a1066616b69d7f5fef01178efdf014c59537 (diff) | |
| download | Project-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/screenshots')
| -rw-r--r-- | meshmc/launcher/screenshots/ImgurAlbumCreation.cpp | 115 | ||||
| -rw-r--r-- | meshmc/launcher/screenshots/ImgurAlbumCreation.h | 60 | ||||
| -rw-r--r-- | meshmc/launcher/screenshots/ImgurUpload.cpp | 142 | ||||
| -rw-r--r-- | meshmc/launcher/screenshots/ImgurUpload.h | 50 | ||||
| -rw-r--r-- | meshmc/launcher/screenshots/Screenshot.h | 40 |
5 files changed, 407 insertions, 0 deletions
diff --git a/meshmc/launcher/screenshots/ImgurAlbumCreation.cpp b/meshmc/launcher/screenshots/ImgurAlbumCreation.cpp new file mode 100644 index 0000000000..726fea0f39 --- /dev/null +++ b/meshmc/launcher/screenshots/ImgurAlbumCreation.cpp @@ -0,0 +1,115 @@ +/* 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 "ImgurAlbumCreation.h" + +#include <QNetworkRequest> +#include <QJsonDocument> +#include <QJsonObject> +#include <QUrl> +#include <QStringList> +#include <QDebug> + +#include "BuildConfig.h" +#include "Application.h" + +ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots) + : NetAction(), m_screenshots(screenshots) +{ + m_url = BuildConfig.IMGUR_BASE_URL + "album.json"; + m_status = Job_NotStarted; +} + +void ImgurAlbumCreation::startImpl() +{ + m_status = Job_InProgress; + QNetworkRequest request(m_url); + request.setHeader(QNetworkRequest::UserAgentHeader, + BuildConfig.USER_AGENT_UNCACHED); + request.setHeader(QNetworkRequest::ContentTypeHeader, + "application/x-www-form-urlencoded"); + request.setRawHeader("Authorization", QString("Client-ID %1") + .arg(BuildConfig.IMGUR_CLIENT_ID) + .toStdString() + .c_str()); + request.setRawHeader("Accept", "application/json"); + + QStringList hashes; + for (auto shot : m_screenshots) { + hashes.append(shot->m_imgurDeleteHash); + } + + const QByteArray data = "deletehashes=" + hashes.join(',').toUtf8() + + "&title=Minecraft%20Screenshots&privacy=hidden"; + + QNetworkReply* rep = APPLICATION->network()->post(request, data); + + m_reply.reset(rep); + connect(rep, &QNetworkReply::uploadProgress, this, + &ImgurAlbumCreation::downloadProgress); + connect(rep, &QNetworkReply::finished, this, + &ImgurAlbumCreation::downloadFinished); + connect(rep, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), + SLOT(downloadError(QNetworkReply::NetworkError))); +} +void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error) +{ + qDebug() << m_reply->errorString(); + m_status = Job_Failed; +} +void ImgurAlbumCreation::downloadFinished() +{ + if (m_status != Job_Failed) { + QByteArray data = m_reply->readAll(); + m_reply.reset(); + QJsonParseError jsonError; + QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError) { + qDebug() << jsonError.errorString(); + emit failed(m_index_within_job); + return; + } + auto object = doc.object(); + if (!object.value("success").toBool()) { + qDebug() << doc.toJson(); + emit failed(m_index_within_job); + return; + } + m_deleteHash = + object.value("data").toObject().value("deletehash").toString(); + m_id = object.value("data").toObject().value("id").toString(); + m_status = Job_Finished; + emit succeeded(m_index_within_job); + return; + } else { + qDebug() << m_reply->readAll(); + m_reply.reset(); + emit failed(m_index_within_job); + return; + } +} +void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, + qint64 bytesTotal) +{ + m_total_progress = bytesTotal; + m_progress = bytesReceived; + emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); +} diff --git a/meshmc/launcher/screenshots/ImgurAlbumCreation.h b/meshmc/launcher/screenshots/ImgurAlbumCreation.h new file mode 100644 index 0000000000..4b670564ab --- /dev/null +++ b/meshmc/launcher/screenshots/ImgurAlbumCreation.h @@ -0,0 +1,60 @@ +/* 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/>. + */ + +#pragma once +#include "net/NetAction.h" +#include "Screenshot.h" +#include "QObjectPtr.h" + +typedef shared_qobject_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr; +class ImgurAlbumCreation : public NetAction +{ + public: + explicit ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots); + static ImgurAlbumCreationPtr make(QList<ScreenShot::Ptr> screenshots) + { + return ImgurAlbumCreationPtr(new ImgurAlbumCreation(screenshots)); + } + + QString deleteHash() const + { + return m_deleteHash; + } + QString id() const + { + return m_id; + } + + protected slots: + virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); + virtual void downloadError(QNetworkReply::NetworkError error); + virtual void downloadFinished(); + virtual void downloadReadyRead() {} + + public slots: + virtual void startImpl(); + + private: + QList<ScreenShot::Ptr> m_screenshots; + + QString m_deleteHash; + QString m_id; +}; diff --git a/meshmc/launcher/screenshots/ImgurUpload.cpp b/meshmc/launcher/screenshots/ImgurUpload.cpp new file mode 100644 index 0000000000..a2e68fde31 --- /dev/null +++ b/meshmc/launcher/screenshots/ImgurUpload.cpp @@ -0,0 +1,142 @@ +/* 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 "ImgurUpload.h" +#include "BuildConfig.h" + +#include <QNetworkRequest> +#include <QHttpMultiPart> +#include <QJsonDocument> +#include <QJsonObject> +#include <QHttpPart> +#include <QFile> +#include <QUrl> +#include <QDebug> + +ImgurUpload::ImgurUpload(ScreenShot::Ptr shot) : NetAction(), m_shot(shot) +{ + m_url = BuildConfig.IMGUR_BASE_URL + "upload.json"; + m_status = Job_NotStarted; +} + +void ImgurUpload::startImpl() +{ + finished = false; + m_status = Job_InProgress; + QNetworkRequest request(m_url); + request.setHeader(QNetworkRequest::UserAgentHeader, + BuildConfig.USER_AGENT_UNCACHED); + request.setRawHeader("Authorization", QString("Client-ID %1") + .arg(BuildConfig.IMGUR_CLIENT_ID) + .toStdString() + .c_str()); + request.setRawHeader("Accept", "application/json"); + + QFile f(m_shot->m_file.absoluteFilePath()); + if (!f.open(QFile::ReadOnly)) { + emit failed(m_index_within_job); + return; + } + + QHttpMultiPart* multipart = + new QHttpMultiPart(QHttpMultiPart::FormDataType); + QHttpPart filePart; + filePart.setBody(f.readAll().toBase64()); + filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png"); + filePart.setHeader(QNetworkRequest::ContentDispositionHeader, + "form-data; name=\"image\""); + multipart->append(filePart); + QHttpPart typePart; + typePart.setHeader(QNetworkRequest::ContentDispositionHeader, + "form-data; name=\"type\""); + typePart.setBody("base64"); + multipart->append(typePart); + QHttpPart namePart; + namePart.setHeader(QNetworkRequest::ContentDispositionHeader, + "form-data; name=\"name\""); + namePart.setBody(m_shot->m_file.baseName().toUtf8()); + multipart->append(namePart); + + QNetworkReply* rep = m_network->post(request, multipart); + + m_reply.reset(rep); + connect(rep, &QNetworkReply::uploadProgress, this, + &ImgurUpload::downloadProgress); + connect(rep, &QNetworkReply::finished, this, + &ImgurUpload::downloadFinished); + connect(rep, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), + SLOT(downloadError(QNetworkReply::NetworkError))); +} +void ImgurUpload::downloadError(QNetworkReply::NetworkError error) +{ + qCritical() << "ImgurUpload failed with error" << m_reply->errorString() + << "Server reply:\n" + << m_reply->readAll(); + if (finished) { + qCritical() << "Double finished ImgurUpload!"; + return; + } + m_status = Job_Failed; + finished = true; + m_reply.reset(); + emit failed(m_index_within_job); +} +void ImgurUpload::downloadFinished() +{ + if (finished) { + qCritical() << "Double finished ImgurUpload!"; + return; + } + QByteArray data = m_reply->readAll(); + m_reply.reset(); + QJsonParseError jsonError; + QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); + if (jsonError.error != QJsonParseError::NoError) { + qDebug() << "imgur server did not reply with JSON" + << jsonError.errorString(); + finished = true; + m_reply.reset(); + emit failed(m_index_within_job); + return; + } + auto object = doc.object(); + if (!object.value("success").toBool()) { + qDebug() << "Screenshot upload not successful:" << doc.toJson(); + finished = true; + m_reply.reset(); + emit failed(m_index_within_job); + return; + } + m_shot->m_imgurId = object.value("data").toObject().value("id").toString(); + m_shot->m_url = object.value("data").toObject().value("link").toString(); + m_shot->m_imgurDeleteHash = + object.value("data").toObject().value("deletehash").toString(); + m_status = Job_Finished; + finished = true; + emit succeeded(m_index_within_job); + return; +} +void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + m_total_progress = bytesTotal; + m_progress = bytesReceived; + emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); +} diff --git a/meshmc/launcher/screenshots/ImgurUpload.h b/meshmc/launcher/screenshots/ImgurUpload.h new file mode 100644 index 0000000000..4aebab44de --- /dev/null +++ b/meshmc/launcher/screenshots/ImgurUpload.h @@ -0,0 +1,50 @@ +/* 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/>. + */ + +#pragma once +#include "QObjectPtr.h" +#include "net/NetAction.h" +#include "Screenshot.h" + +class ImgurUpload : public NetAction +{ + public: + using Ptr = shared_qobject_ptr<ImgurUpload>; + + explicit ImgurUpload(ScreenShot::Ptr shot); + static Ptr make(ScreenShot::Ptr shot) + { + return Ptr(new ImgurUpload(shot)); + } + + protected slots: + void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override; + void downloadError(QNetworkReply::NetworkError error) override; + void downloadFinished() override; + void downloadReadyRead() override {} + + public slots: + void startImpl() override; + + private: + ScreenShot::Ptr m_shot; + bool finished = true; +}; diff --git a/meshmc/launcher/screenshots/Screenshot.h b/meshmc/launcher/screenshots/Screenshot.h new file mode 100644 index 0000000000..eb09cef2fb --- /dev/null +++ b/meshmc/launcher/screenshots/Screenshot.h @@ -0,0 +1,40 @@ +/* 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/>. + */ + +#pragma once + +#include <QDateTime> +#include <QString> +#include <QFileInfo> +#include <memory> + +struct ScreenShot { + using Ptr = std::shared_ptr<ScreenShot>; + + ScreenShot(QFileInfo file) + { + m_file = file; + } + QFileInfo m_file; + QString m_url; + QString m_imgurId; + QString m_imgurDeleteHash; +}; |
