Refactor ImgurUpload
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
1c3402d081
commit
25ffc4c4b0
@ -139,6 +139,7 @@ set(NET_SOURCES
|
|||||||
net/HeaderProxy.h
|
net/HeaderProxy.h
|
||||||
net/RawHeaderProxy.h
|
net/RawHeaderProxy.h
|
||||||
net/ApiHeaderProxy.h
|
net/ApiHeaderProxy.h
|
||||||
|
net/StaticHeaderProxy.h
|
||||||
net/ApiDownload.h
|
net/ApiDownload.h
|
||||||
net/ApiDownload.cpp
|
net/ApiDownload.cpp
|
||||||
net/ApiUpload.cpp
|
net/ApiUpload.cpp
|
||||||
|
@ -111,6 +111,8 @@ void NetRequest::executeTask()
|
|||||||
m_last_progress_bytes = 0;
|
m_last_progress_bytes = 0;
|
||||||
|
|
||||||
QNetworkReply* rep = getReply(request);
|
QNetworkReply* rep = getReply(request);
|
||||||
|
if (rep == nullptr) // it failed
|
||||||
|
return;
|
||||||
m_reply.reset(rep);
|
m_reply.reset(rep);
|
||||||
connect(rep, &QNetworkReply::downloadProgress, this, &NetRequest::downloadProgress);
|
connect(rep, &QNetworkReply::downloadProgress, this, &NetRequest::downloadProgress);
|
||||||
connect(rep, &QNetworkReply::finished, this, &NetRequest::downloadFinished);
|
connect(rep, &QNetworkReply::finished, this, &NetRequest::downloadFinished);
|
||||||
|
39
launcher/net/StaticHeaderProxy.h
Normal file
39
launcher/net/StaticHeaderProxy.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
/*
|
||||||
|
* Prism Launcher - Minecraft Launcher
|
||||||
|
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "net/HeaderProxy.h"
|
||||||
|
|
||||||
|
namespace Net {
|
||||||
|
|
||||||
|
class StaticHeaderProxy : public HeaderProxy {
|
||||||
|
public:
|
||||||
|
StaticHeaderProxy(QList<HeaderPair> hdrs = {}) : HeaderProxy(), m_hdrs(hdrs){};
|
||||||
|
virtual ~StaticHeaderProxy() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual QList<HeaderPair> headers(const QNetworkRequest&) const override { return m_hdrs; };
|
||||||
|
void setHeaders(QList<HeaderPair> hdrs) { m_hdrs = hdrs; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<HeaderPair> m_hdrs;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Net
|
@ -36,96 +36,79 @@
|
|||||||
|
|
||||||
#include "ImgurAlbumCreation.h"
|
#include "ImgurAlbumCreation.h"
|
||||||
|
|
||||||
#include <QNetworkRequest>
|
#include <QDebug>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QUrl>
|
#include <QList>
|
||||||
|
#include <QNetworkRequest>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QDebug>
|
#include <QUrl>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "BuildConfig.h"
|
#include "BuildConfig.h"
|
||||||
#include "Application.h"
|
#include "net/StaticHeaderProxy.h"
|
||||||
|
|
||||||
ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots) : NetAction(), m_screenshots(screenshots)
|
Net::NetRequest::Ptr ImgurAlbumCreation::make(std::shared_ptr<ImgurAlbumCreation::Result> output, QList<ScreenShot::Ptr> screenshots)
|
||||||
{
|
{
|
||||||
m_url = BuildConfig.IMGUR_BASE_URL + "album.json";
|
auto up = makeShared<ImgurAlbumCreation>();
|
||||||
m_state = State::Inactive;
|
up->m_url = BuildConfig.IMGUR_BASE_URL + "album.json";
|
||||||
|
up->m_sink.reset(new Sink(output));
|
||||||
|
up->m_screenshots = screenshots;
|
||||||
|
return up;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurAlbumCreation::executeTask()
|
QNetworkReply* ImgurAlbumCreation::getReply(QNetworkRequest& request)
|
||||||
{
|
{
|
||||||
m_state = State::Running;
|
|
||||||
QNetworkRequest request(m_url);
|
|
||||||
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
|
|
||||||
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;
|
QStringList hashes;
|
||||||
for (auto shot : m_screenshots)
|
for (auto shot : m_screenshots) {
|
||||||
{
|
|
||||||
hashes.append(shot->m_imgurDeleteHash);
|
hashes.append(shot->m_imgurDeleteHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
const QByteArray data = "deletehashes=" + hashes.join(',').toUtf8() + "&title=Minecraft%20Screenshots&privacy=hidden";
|
const QByteArray data = "deletehashes=" + hashes.join(',').toUtf8() + "&title=Minecraft%20Screenshots&privacy=hidden";
|
||||||
|
return m_network->post(request, data);
|
||||||
|
};
|
||||||
|
|
||||||
QNetworkReply *rep = APPLICATION->network()->post(request, data);
|
void ImgurAlbumCreation::init()
|
||||||
|
{
|
||||||
m_reply.reset(rep);
|
qDebug() << "Setting up imgur upload";
|
||||||
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurAlbumCreation::downloadProgress);
|
auto api_headers = new Net::StaticHeaderProxy(
|
||||||
connect(rep, &QNetworkReply::finished, this, &ImgurAlbumCreation::downloadFinished);
|
QList<Net::HeaderPair>{ { "Content-Type", "application/x-www-form-urlencoded" },
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
|
{ "Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str() },
|
||||||
connect(rep, &QNetworkReply::errorOccurred, this, &ImgurAlbumCreation::downloadError);
|
{ "Accept", "application/json" } });
|
||||||
#else
|
addHeaderProxy(api_headers);
|
||||||
connect(rep, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &ImgurAlbumCreation::downloadError);
|
|
||||||
#endif
|
|
||||||
connect(rep, &QNetworkReply::sslErrors, this, &ImgurAlbumCreation::sslErrors);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error)
|
auto ImgurAlbumCreation::Sink::init(QNetworkRequest& request) -> Task::State
|
||||||
{
|
{
|
||||||
qDebug() << m_reply->errorString();
|
m_output.clear();
|
||||||
m_state = State::Failed;
|
return Task::State::Running;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto ImgurAlbumCreation::Sink::write(QByteArray& data) -> Task::State
|
||||||
|
{
|
||||||
|
m_output.append(data);
|
||||||
|
return Task::State::Running;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurAlbumCreation::downloadFinished()
|
auto ImgurAlbumCreation::Sink::abort() -> Task::State
|
||||||
|
{
|
||||||
|
m_output.clear();
|
||||||
|
return Task::State::Failed;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ImgurAlbumCreation::Sink::finalize(QNetworkReply&) -> Task::State
|
||||||
{
|
{
|
||||||
if (m_state != State::Failed)
|
|
||||||
{
|
|
||||||
QByteArray data = m_reply->readAll();
|
|
||||||
m_reply.reset();
|
|
||||||
QJsonParseError jsonError;
|
QJsonParseError jsonError;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
|
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonError.error != QJsonParseError::NoError)
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
{
|
|
||||||
qDebug() << jsonError.errorString();
|
qDebug() << jsonError.errorString();
|
||||||
emitFailed();
|
return Task::State::Failed;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
auto object = doc.object();
|
auto object = doc.object();
|
||||||
if (!object.value("success").toBool())
|
if (!object.value("success").toBool()) {
|
||||||
{
|
|
||||||
qDebug() << doc.toJson();
|
qDebug() << doc.toJson();
|
||||||
emitFailed();
|
return Task::State::Failed;
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_deleteHash = object.value("data").toObject().value("deletehash").toString();
|
|
||||||
m_id = object.value("data").toObject().value("id").toString();
|
|
||||||
m_state = State::Succeeded;
|
|
||||||
emit succeeded();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qDebug() << m_reply->readAll();
|
|
||||||
m_reply.reset();
|
|
||||||
emitFailed();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
m_result->deleteHash = object.value("data").toObject().value("deletehash").toString();
|
||||||
|
m_result->id = object.value("data").toObject().value("id").toString();
|
||||||
void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
return Task::State::Succeeded;
|
||||||
{
|
|
||||||
setProgress(bytesReceived, bytesTotal);
|
|
||||||
emit progress(bytesReceived, bytesTotal);
|
|
||||||
}
|
}
|
@ -35,44 +35,40 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "net/NetAction.h"
|
|
||||||
#include "Screenshot.h"
|
#include "Screenshot.h"
|
||||||
|
#include "net/NetRequest.h"
|
||||||
|
|
||||||
typedef shared_qobject_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr;
|
class ImgurAlbumCreation : public Net::NetRequest {
|
||||||
class ImgurAlbumCreation : public NetAction
|
public:
|
||||||
{
|
virtual ~ImgurAlbumCreation() = default;
|
||||||
public:
|
|
||||||
explicit ImgurAlbumCreation(QList<ScreenShot::Ptr> screenshots);
|
|
||||||
static ImgurAlbumCreationPtr make(QList<ScreenShot::Ptr> screenshots)
|
|
||||||
{
|
|
||||||
return ImgurAlbumCreationPtr(new ImgurAlbumCreation(screenshots));
|
|
||||||
}
|
|
||||||
|
|
||||||
QString deleteHash() const
|
struct Result {
|
||||||
{
|
QString deleteHash;
|
||||||
return m_deleteHash;
|
QString id;
|
||||||
}
|
};
|
||||||
QString id() const
|
|
||||||
{
|
|
||||||
return m_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init() override {};
|
class Sink : public Net::Sink {
|
||||||
|
public:
|
||||||
|
Sink(std::shared_ptr<Result> res) : m_result(res){};
|
||||||
|
virtual ~Sink() = default;
|
||||||
|
|
||||||
protected
|
public:
|
||||||
slots:
|
auto init(QNetworkRequest& request) -> Task::State override;
|
||||||
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
|
auto write(QByteArray& data) -> Task::State override;
|
||||||
void downloadError(QNetworkReply::NetworkError error) override;
|
auto abort() -> Task::State override;
|
||||||
void downloadFinished() override;
|
auto finalize(QNetworkReply& reply) -> Task::State override;
|
||||||
void downloadReadyRead() override {}
|
auto hasLocalData() -> bool override { return false; }
|
||||||
|
|
||||||
public
|
private:
|
||||||
slots:
|
std::shared_ptr<Result> m_result;
|
||||||
void executeTask() override;
|
QByteArray m_output;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
static NetRequest::Ptr make(std::shared_ptr<Result> output, QList<ScreenShot::Ptr> screenshots);
|
||||||
|
QNetworkReply* getReply(QNetworkRequest& request) override;
|
||||||
|
|
||||||
|
void init() override;
|
||||||
|
|
||||||
|
private:
|
||||||
QList<ScreenShot::Ptr> m_screenshots;
|
QList<ScreenShot::Ptr> m_screenshots;
|
||||||
|
|
||||||
QString m_deleteHash;
|
|
||||||
QString m_id;
|
|
||||||
};
|
};
|
||||||
|
@ -36,120 +36,95 @@
|
|||||||
|
|
||||||
#include "ImgurUpload.h"
|
#include "ImgurUpload.h"
|
||||||
#include "BuildConfig.h"
|
#include "BuildConfig.h"
|
||||||
#include "Application.h"
|
#include "net/StaticHeaderProxy.h"
|
||||||
|
|
||||||
#include <QNetworkRequest>
|
#include <QDebug>
|
||||||
|
#include <QFile>
|
||||||
#include <QHttpMultiPart>
|
#include <QHttpMultiPart>
|
||||||
|
#include <QHttpPart>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QHttpPart>
|
#include <QNetworkRequest>
|
||||||
#include <QFile>
|
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QDebug>
|
|
||||||
|
|
||||||
ImgurUpload::ImgurUpload(ScreenShot::Ptr shot) : NetAction(), m_shot(shot)
|
void ImgurUpload::init()
|
||||||
{
|
{
|
||||||
m_url = BuildConfig.IMGUR_BASE_URL + "upload.json";
|
qDebug() << "Setting up imgur upload";
|
||||||
m_state = State::Inactive;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurUpload::executeTask()
|
QNetworkReply* ImgurUpload::getReply(QNetworkRequest& request)
|
||||||
{
|
{
|
||||||
finished = false;
|
auto file = new QFile(m_fileInfo.absoluteFilePath());
|
||||||
m_state = Task::State::Running;
|
|
||||||
QNetworkRequest request(m_url);
|
|
||||||
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
|
|
||||||
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 (!file->open(QFile::ReadOnly)) {
|
||||||
if (!f.open(QFile::ReadOnly))
|
|
||||||
{
|
|
||||||
emitFailed();
|
emitFailed();
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
QHttpMultiPart* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||||
|
file->setParent(multipart);
|
||||||
QHttpPart filePart;
|
QHttpPart filePart;
|
||||||
filePart.setBody(f.readAll().toBase64());
|
filePart.setBodyDevice(file);
|
||||||
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png");
|
filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png");
|
||||||
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"image\"");
|
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"image\"");
|
||||||
multipart->append(filePart);
|
multipart->append(filePart);
|
||||||
QHttpPart typePart;
|
QHttpPart typePart;
|
||||||
typePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"type\"");
|
typePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"type\"");
|
||||||
typePart.setBody("base64");
|
typePart.setBody("file");
|
||||||
multipart->append(typePart);
|
multipart->append(typePart);
|
||||||
QHttpPart namePart;
|
QHttpPart namePart;
|
||||||
namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\"");
|
namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\"");
|
||||||
namePart.setBody(m_shot->m_file.baseName().toUtf8());
|
namePart.setBody(m_fileInfo.baseName().toUtf8());
|
||||||
multipart->append(namePart);
|
multipart->append(namePart);
|
||||||
|
|
||||||
QNetworkReply *rep = m_network->post(request, multipart);
|
return m_network->post(request, multipart);
|
||||||
|
};
|
||||||
|
|
||||||
m_reply.reset(rep);
|
auto ImgurUpload::Sink::init(QNetworkRequest& request) -> Task::State
|
||||||
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurUpload::downloadProgress);
|
{
|
||||||
connect(rep, &QNetworkReply::finished, this, &ImgurUpload::downloadFinished);
|
m_output.clear();
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
|
return Task::State::Running;
|
||||||
connect(rep, &QNetworkReply::errorOccurred, this, &ImgurUpload::downloadError);
|
};
|
||||||
#else
|
|
||||||
connect(rep, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &ImgurUpload::downloadError);
|
auto ImgurUpload::Sink::write(QByteArray& data) -> Task::State
|
||||||
#endif
|
{
|
||||||
connect(rep, &QNetworkReply::sslErrors, this, &ImgurUpload::sslErrors);
|
m_output.append(data);
|
||||||
|
return Task::State::Running;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
|
auto ImgurUpload::Sink::abort() -> Task::State
|
||||||
{
|
{
|
||||||
qCritical() << "ImgurUpload failed with error" << m_reply->errorString() << "Server reply:\n" << m_reply->readAll();
|
m_output.clear();
|
||||||
if(finished)
|
return Task::State::Failed;
|
||||||
{
|
|
||||||
qCritical() << "Double finished ImgurUpload!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_state = Task::State::Failed;
|
|
||||||
finished = true;
|
|
||||||
m_reply.reset();
|
|
||||||
emitFailed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurUpload::downloadFinished()
|
auto ImgurUpload::Sink::finalize(QNetworkReply&) -> Task::State
|
||||||
{
|
{
|
||||||
if(finished)
|
|
||||||
{
|
|
||||||
qCritical() << "Double finished ImgurUpload!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
QByteArray data = m_reply->readAll();
|
|
||||||
m_reply.reset();
|
|
||||||
QJsonParseError jsonError;
|
QJsonParseError jsonError;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
|
QJsonDocument doc = QJsonDocument::fromJson(m_output, &jsonError);
|
||||||
if (jsonError.error != QJsonParseError::NoError)
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
{
|
|
||||||
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
|
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
|
||||||
finished = true;
|
return Task::State::Failed;
|
||||||
m_reply.reset();
|
|
||||||
emitFailed();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
auto object = doc.object();
|
auto object = doc.object();
|
||||||
if (!object.value("success").toBool())
|
if (!object.value("success").toBool()) {
|
||||||
{
|
|
||||||
qDebug() << "Screenshot upload not successful:" << doc.toJson();
|
qDebug() << "Screenshot upload not successful:" << doc.toJson();
|
||||||
finished = true;
|
return Task::State::Failed;
|
||||||
m_reply.reset();
|
|
||||||
emitFailed();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
|
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_url = object.value("data").toObject().value("link").toString();
|
||||||
m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString();
|
m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString();
|
||||||
m_state = Task::State::Succeeded;
|
return Task::State::Succeeded;
|
||||||
finished = true;
|
|
||||||
emit succeeded();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
Net::NetRequest::Ptr ImgurUpload::make(ScreenShot::Ptr m_shot)
|
||||||
{
|
{
|
||||||
setProgress(bytesReceived, bytesTotal);
|
auto up = makeShared<ImgurUpload>(m_shot->m_file);
|
||||||
emit progress(bytesReceived, bytesTotal);
|
up->m_url = std::move(BuildConfig.IMGUR_BASE_URL + "upload.json");
|
||||||
|
up->m_sink.reset(new Sink(m_shot));
|
||||||
|
return up;
|
||||||
}
|
}
|
||||||
|
@ -35,31 +35,36 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "net/NetAction.h"
|
#include <QFileInfo>
|
||||||
#include "Screenshot.h"
|
#include "Screenshot.h"
|
||||||
|
#include "net/NetRequest.h"
|
||||||
|
|
||||||
class ImgurUpload : public NetAction {
|
class ImgurUpload : public Net::NetRequest {
|
||||||
public:
|
public:
|
||||||
using Ptr = shared_qobject_ptr<ImgurUpload>;
|
class Sink : public Net::Sink {
|
||||||
|
public:
|
||||||
|
Sink(ScreenShot::Ptr shot) : m_shot(shot){};
|
||||||
|
virtual ~Sink() = default;
|
||||||
|
|
||||||
explicit ImgurUpload(ScreenShot::Ptr shot);
|
public:
|
||||||
static Ptr make(ScreenShot::Ptr shot) {
|
auto init(QNetworkRequest& request) -> Task::State override;
|
||||||
return Ptr(new ImgurUpload(shot));
|
auto write(QByteArray& data) -> Task::State override;
|
||||||
}
|
auto abort() -> Task::State override;
|
||||||
void init() override {};
|
auto finalize(QNetworkReply& reply) -> Task::State override;
|
||||||
|
auto hasLocalData() -> bool override { return false; }
|
||||||
|
|
||||||
protected
|
private:
|
||||||
slots:
|
|
||||||
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
|
|
||||||
void downloadError(QNetworkReply::NetworkError error) override;
|
|
||||||
void downloadFinished() override;
|
|
||||||
void downloadReadyRead() override {}
|
|
||||||
|
|
||||||
public
|
|
||||||
slots:
|
|
||||||
void executeTask() override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
ScreenShot::Ptr m_shot;
|
ScreenShot::Ptr m_shot;
|
||||||
bool finished = true;
|
QByteArray m_output;
|
||||||
|
};
|
||||||
|
ImgurUpload(QFileInfo info) : m_fileInfo(info) {}
|
||||||
|
virtual ~ImgurUpload() = default;
|
||||||
|
|
||||||
|
static NetRequest::Ptr make(ScreenShot::Ptr m_shot);
|
||||||
|
|
||||||
|
void init() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual QNetworkReply* getReply(QNetworkRequest&) override;
|
||||||
|
const QFileInfo m_fileInfo;
|
||||||
};
|
};
|
||||||
|
@ -403,41 +403,37 @@ void ScreenshotsPage::on_actionUpload_triggered()
|
|||||||
|
|
||||||
QList<ScreenShot::Ptr> uploaded;
|
QList<ScreenShot::Ptr> uploaded;
|
||||||
auto job = NetJob::Ptr(new NetJob("Screenshot Upload", APPLICATION->network()));
|
auto job = NetJob::Ptr(new NetJob("Screenshot Upload", APPLICATION->network()));
|
||||||
if(selection.size() < 2)
|
|
||||||
{
|
ProgressDialog dialog(this);
|
||||||
|
dialog.setSkipButton(true, tr("Abort"));
|
||||||
|
|
||||||
|
if (selection.size() < 2) {
|
||||||
auto item = selection.at(0);
|
auto item = selection.at(0);
|
||||||
auto info = m_model->fileInfo(item);
|
auto info = m_model->fileInfo(item);
|
||||||
auto screenshot = std::make_shared<ScreenShot>(info);
|
auto screenshot = std::make_shared<ScreenShot>(info);
|
||||||
job->addNetAction(ImgurUpload::make(screenshot));
|
job->addNetAction(ImgurUpload::make(screenshot));
|
||||||
|
|
||||||
m_uploadActive = true;
|
m_uploadActive = true;
|
||||||
ProgressDialog dialog(this);
|
|
||||||
|
|
||||||
if(dialog.execWithTask(job.get()) != QDialog::Accepted)
|
if (dialog.execWithTask(job.get()) != QDialog::Accepted) {
|
||||||
{
|
CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), tr("Unknown error"), QMessageBox::Warning)->exec();
|
||||||
CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"),
|
} else {
|
||||||
tr("Unknown error"), QMessageBox::Warning)->exec();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto link = screenshot->m_url;
|
auto link = screenshot->m_url;
|
||||||
QClipboard *clipboard = QApplication::clipboard();
|
QClipboard* clipboard = QApplication::clipboard();
|
||||||
|
qDebug() << "ImgurUpload link" << link;
|
||||||
clipboard->setText(link);
|
clipboard->setText(link);
|
||||||
CustomMessageBox::selectable(
|
CustomMessageBox::selectable(
|
||||||
this,
|
this, tr("Upload finished"),
|
||||||
tr("Upload finished"),
|
tr("The <a href=\"%1\">link to the uploaded screenshot</a> has been placed in your clipboard.").arg(link),
|
||||||
tr("The <a href=\"%1\">link to the uploaded screenshot</a> has been placed in your clipboard.")
|
QMessageBox::Information)
|
||||||
.arg(link),
|
->exec();
|
||||||
QMessageBox::Information
|
|
||||||
)->exec();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_uploadActive = false;
|
m_uploadActive = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto item : selection)
|
for (auto item : selection) {
|
||||||
{
|
|
||||||
auto info = m_model->fileInfo(item);
|
auto info = m_model->fileInfo(item);
|
||||||
auto screenshot = std::make_shared<ScreenShot>(info);
|
auto screenshot = std::make_shared<ScreenShot>(info);
|
||||||
uploaded.push_back(screenshot);
|
uploaded.push_back(screenshot);
|
||||||
@ -445,32 +441,23 @@ void ScreenshotsPage::on_actionUpload_triggered()
|
|||||||
}
|
}
|
||||||
SequentialTask task;
|
SequentialTask task;
|
||||||
auto albumTask = NetJob::Ptr(new NetJob("Imgur Album Creation", APPLICATION->network()));
|
auto albumTask = NetJob::Ptr(new NetJob("Imgur Album Creation", APPLICATION->network()));
|
||||||
auto imgurAlbum = ImgurAlbumCreation::make(uploaded);
|
auto imgurResult = std::make_shared<ImgurAlbumCreation::Result>();
|
||||||
|
auto imgurAlbum = ImgurAlbumCreation::make(imgurResult, uploaded);
|
||||||
albumTask->addNetAction(imgurAlbum);
|
albumTask->addNetAction(imgurAlbum);
|
||||||
task.addTask(job);
|
task.addTask(job);
|
||||||
task.addTask(albumTask);
|
task.addTask(albumTask);
|
||||||
m_uploadActive = true;
|
m_uploadActive = true;
|
||||||
ProgressDialog prog(this);
|
if (dialog.execWithTask(&task) != QDialog::Accepted || imgurResult->id.isEmpty()) {
|
||||||
if (prog.execWithTask(&task) != QDialog::Accepted)
|
CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), tr("Unknown error"), QMessageBox::Warning)->exec();
|
||||||
{
|
} else {
|
||||||
CustomMessageBox::selectable(
|
auto link = QString("https://imgur.com/a/%1").arg(imgurResult->id);
|
||||||
this,
|
qDebug() << "ImgurUpload link" << link;
|
||||||
tr("Failed to upload screenshots!"),
|
QClipboard* clipboard = QApplication::clipboard();
|
||||||
tr("Unknown error"),
|
|
||||||
QMessageBox::Warning
|
|
||||||
)->exec();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto link = QString("https://imgur.com/a/%1").arg(imgurAlbum->id());
|
|
||||||
QClipboard *clipboard = QApplication::clipboard();
|
|
||||||
clipboard->setText(link);
|
clipboard->setText(link);
|
||||||
CustomMessageBox::selectable(
|
CustomMessageBox::selectable(this, tr("Upload finished"),
|
||||||
this,
|
tr("The <a href=\"%1\">link to the uploaded album</a> has been placed in your clipboard.").arg(link),
|
||||||
tr("Upload finished"),
|
QMessageBox::Information)
|
||||||
tr("The <a href=\"%1\">link to the uploaded album</a> has been placed in your clipboard.") .arg(link),
|
->exec();
|
||||||
QMessageBox::Information
|
|
||||||
)->exec();
|
|
||||||
}
|
}
|
||||||
m_uploadActive = false;
|
m_uploadActive = false;
|
||||||
}
|
}
|
||||||
@ -478,8 +465,7 @@ void ScreenshotsPage::on_actionUpload_triggered()
|
|||||||
void ScreenshotsPage::on_actionCopy_Image_triggered()
|
void ScreenshotsPage::on_actionCopy_Image_triggered()
|
||||||
{
|
{
|
||||||
auto selection = ui->listView->selectionModel()->selectedRows();
|
auto selection = ui->listView->selectionModel()->selectedRows();
|
||||||
if(selection.size() < 1)
|
if (selection.size() < 1) {
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user