diff options
author | Alexia <alexia@robotbrain.info> | 2016-05-12 16:51:25 -0400 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2016-05-15 16:01:05 +0200 |
commit | 377316999ea9ef6adbc75c52a858eb8e526616e7 (patch) | |
tree | 1ad7ba47c89c69d0e25f7e6e0f592782e75b81d1 /api/logic/minecraft/SkinUpload.cpp | |
parent | f9791a5cc80b8e99eebd68176c33fdc1c377af86 (diff) | |
download | MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.gz MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.lz MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.tar.xz MultiMC-377316999ea9ef6adbc75c52a858eb8e526616e7.zip |
GH-767 Basic skin upload
Diffstat (limited to 'api/logic/minecraft/SkinUpload.cpp')
-rw-r--r-- | api/logic/minecraft/SkinUpload.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/api/logic/minecraft/SkinUpload.cpp b/api/logic/minecraft/SkinUpload.cpp new file mode 100644 index 00000000..2c67c7aa --- /dev/null +++ b/api/logic/minecraft/SkinUpload.cpp @@ -0,0 +1,69 @@ +#include "SkinUpload.h" +#include <QNetworkRequest> +#include <QHttpMultiPart> +#include <Env.h> + +QByteArray getModelString(SkinUpload::Model model) { + switch (model) { + case SkinUpload::STEVE: + return "steve"; + case SkinUpload::ALEX: + return "alex"; + default: + qDebug() << "Unknown skin type!"; + return ""; + } +} + +SkinUpload::SkinUpload(QObject *parent, AuthSessionPtr session, QByteArray skin, SkinUpload::Model model) + : Task(parent), m_model(model), m_skin(skin), m_session(session) +{ +} + +void SkinUpload::executeTask() +{ + QNetworkRequest request(QUrl(QString("https://api.mojang.com/user/profile/%1/skin").arg(m_session->uuid))); + request.setRawHeader("Authorization", QString("Bearer: %1").arg(m_session->access_token).toLocal8Bit()); + + QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); + + QHttpPart model; + model.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"model\"")); + model.setBody(getModelString(m_model)); + + QHttpPart skin; + skin.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); + skin.setHeader(QNetworkRequest::ContentDispositionHeader, + QVariant("form-data; name=\"file\"; filename=\"skin.png\"")); + skin.setBody(m_skin); + + multiPart->append(model); + multiPart->append(skin); + + QNetworkReply *rep = ENV.qnam()->put(request, multiPart); + m_reply = std::shared_ptr<QNetworkReply>(rep); + + setStatus(tr("Uploading skin")); + connect(rep, &QNetworkReply::uploadProgress, this, &Task::setProgress); + connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError))); + connect(rep, SIGNAL(finished()), this, SLOT(downloadFinished())); +} + +void SkinUpload::downloadError(QNetworkReply::NetworkError error) +{ + // error happened during download. + qCritical() << "Network error: " << error; + emitFailed(m_reply->errorString()); +} + +void SkinUpload::downloadFinished() +{ + // if the download failed + if (m_reply->error() != QNetworkReply::NetworkError::NoError) + { + emitFailed(QString("Network error: %1").arg(m_reply->errorString())); + m_reply.reset(); + return; + } + emitSucceeded(); +} |