PrismLauncher/launcher/screenshots/ImgurUpload.cpp

131 lines
4.5 KiB
C++
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
*
* 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, version 3.
*
* 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/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2014-02-24 10:30:27 +00:00
#include "ImgurUpload.h"
#include "BuildConfig.h"
#include "net/StaticHeaderProxy.h"
#include <QDebug>
#include <QFile>
#include <QHttpMultiPart>
#include <QHttpPart>
2014-02-23 21:14:24 +00:00
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkRequest>
#include <QUrl>
2014-02-23 21:14:24 +00:00
void ImgurUpload::init()
2014-02-23 21:14:24 +00:00
{
qDebug() << "Setting up imgur upload";
auto api_headers = new Net::StaticHeaderProxy(
QList<Net::HeaderPair>{ { "Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str() },
{ "Accept", "application/json" } });
addHeaderProxy(api_headers);
2014-02-23 21:14:24 +00:00
}
QNetworkReply* ImgurUpload::getReply(QNetworkRequest& request)
2014-02-23 21:14:24 +00:00
{
auto file = new QFile(m_fileInfo.absoluteFilePath());
2014-02-23 21:14:24 +00:00
if (!file->open(QFile::ReadOnly)) {
emitFailed();
return nullptr;
2018-07-15 13:51:05 +01:00
}
QHttpMultiPart* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
file->setParent(multipart);
2018-07-15 13:51:05 +01:00
QHttpPart filePart;
filePart.setBodyDevice(file);
2018-07-15 13:51:05 +01:00
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("file");
2018-07-15 13:51:05 +01:00
multipart->append(typePart);
QHttpPart namePart;
namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\"");
namePart.setBody(m_fileInfo.baseName().toUtf8());
2018-07-15 13:51:05 +01:00
multipart->append(namePart);
2014-02-23 21:14:24 +00:00
return m_network->post(request, multipart);
};
2014-02-23 21:14:24 +00:00
auto ImgurUpload::Sink::init(QNetworkRequest& request) -> Task::State
{
m_output.clear();
return Task::State::Running;
};
auto ImgurUpload::Sink::write(QByteArray& data) -> Task::State
{
m_output.append(data);
return Task::State::Running;
2014-02-23 21:14:24 +00:00
}
auto ImgurUpload::Sink::abort() -> Task::State
2014-02-23 21:14:24 +00:00
{
m_output.clear();
return Task::State::Failed;
2014-02-23 21:14:24 +00:00
}
auto ImgurUpload::Sink::finalize(QNetworkReply&) -> Task::State
2014-02-23 21:14:24 +00:00
{
2018-07-15 13:51:05 +01:00
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
2018-07-15 13:51:05 +01:00
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
return Task::State::Failed;
2018-07-15 13:51:05 +01:00
}
auto object = doc.object();
if (!object.value("success").toBool()) {
2018-07-15 13:51:05 +01:00
qDebug() << "Screenshot upload not successful:" << doc.toJson();
return Task::State::Failed;
2018-07-15 13:51:05 +01:00
}
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();
return Task::State::Succeeded;
2014-02-23 21:14:24 +00:00
}
Net::NetRequest::Ptr ImgurUpload::make(ScreenShot::Ptr m_shot)
2014-02-23 21:14:24 +00:00
{
auto up = makeShared<ImgurUpload>(m_shot->m_file);
up->m_url = std::move(BuildConfig.IMGUR_BASE_URL + "upload.json");
up->m_sink.reset(new Sink(m_shot));
return up;
2014-02-23 21:14:24 +00:00
}