Imgur album creation
This commit is contained in:
@ -10,7 +10,7 @@ public:
|
||||
QDateTime timestamp;
|
||||
QString file;
|
||||
QString url;
|
||||
QString imgurIndex;
|
||||
QString imgurId;
|
||||
};
|
||||
|
||||
class ScreenshotList : public QAbstractListModel
|
||||
|
90
logic/net/ImgurAlbumCreation.cpp
Normal file
90
logic/net/ImgurAlbumCreation.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include "ImgurAlbumCreation.h"
|
||||
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QUrl>
|
||||
|
||||
#include "logic/lists/ScreenshotList.h"
|
||||
#include "URLConstants.h"
|
||||
#include "MultiMC.h"
|
||||
#include "logger/QsLog.h"
|
||||
|
||||
ImgurAlbumCreation::ImgurAlbumCreation(QList<ScreenShot *> screenshots) : NetAction(), m_screenshots(screenshots)
|
||||
{
|
||||
m_url = URLConstants::IMGUR_BASE_URL + "album.json";
|
||||
m_status = Job_NotStarted;
|
||||
}
|
||||
|
||||
void ImgurAlbumCreation::start()
|
||||
{
|
||||
m_status = Job_InProgress;
|
||||
QNetworkRequest request(m_url);
|
||||
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3");
|
||||
request.setRawHeader("Accept", "application/json");
|
||||
|
||||
QStringList ids;
|
||||
for (auto shot : m_screenshots)
|
||||
{
|
||||
ids.append(shot->imgurId);
|
||||
}
|
||||
|
||||
const QByteArray data = "ids=" + ids.join(',').toUtf8() + "&title=Minecraft%20Screenshots&privacy=hidden";
|
||||
|
||||
auto worker = MMC->qnam();
|
||||
QNetworkReply *rep = worker->post(request, data);
|
||||
|
||||
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
||||
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurAlbumCreation::downloadProgress);
|
||||
connect(rep, &QNetworkReply::finished, this, &ImgurAlbumCreation::downloadFinished);
|
||||
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
|
||||
SLOT(downloadError(QNetworkReply::NetworkError)));
|
||||
}
|
||||
void ImgurAlbumCreation::downloadError(QNetworkReply::NetworkError error)
|
||||
{
|
||||
QLOG_DEBUG() << 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)
|
||||
{
|
||||
QLOG_DEBUG() << jsonError.errorString();
|
||||
emit failed(m_index_within_job);
|
||||
return;
|
||||
}
|
||||
auto object = doc.object();
|
||||
if (!object.value("success").toBool())
|
||||
{
|
||||
QLOG_DEBUG() << 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
|
||||
{
|
||||
QLOG_DEBUG() << 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 progress(m_index_within_job, bytesReceived, bytesTotal);
|
||||
}
|
42
logic/net/ImgurAlbumCreation.h
Normal file
42
logic/net/ImgurAlbumCreation.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "NetAction.h"
|
||||
|
||||
class ScreenShot;
|
||||
typedef std::shared_ptr<class ImgurAlbumCreation> ImgurAlbumCreationPtr;
|
||||
class ImgurAlbumCreation : public NetAction
|
||||
{
|
||||
public:
|
||||
explicit ImgurAlbumCreation(QList<ScreenShot *> screenshots);
|
||||
static ImgurAlbumCreationPtr make(QList<ScreenShot *> 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 start();
|
||||
|
||||
private:
|
||||
QList<ScreenShot *> m_screenshots;
|
||||
|
||||
QString m_deleteHash;
|
||||
QString m_id;
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
#include "ScreenshotUploader.h"
|
||||
#include "ImgurUpload.h"
|
||||
|
||||
#include <QNetworkRequest>
|
||||
#include <QHttpMultiPart>
|
||||
@ -13,13 +13,13 @@
|
||||
#include "MultiMC.h"
|
||||
#include "logger/QsLog.h"
|
||||
|
||||
ScreenShotUpload::ScreenShotUpload(ScreenShot *shot) : NetAction(), m_shot(shot)
|
||||
ImgurUpload::ImgurUpload(ScreenShot *shot) : NetAction(), m_shot(shot)
|
||||
{
|
||||
m_url = URLConstants::IMGUR_UPLOAD_URL;
|
||||
m_url = URLConstants::IMGUR_BASE_URL + "upload.json";
|
||||
m_status = Job_NotStarted;
|
||||
}
|
||||
|
||||
void ScreenShotUpload::start()
|
||||
void ImgurUpload::start()
|
||||
{
|
||||
m_status = Job_InProgress;
|
||||
QNetworkRequest request(m_url);
|
||||
@ -53,17 +53,17 @@ void ScreenShotUpload::start()
|
||||
QNetworkReply *rep = worker->post(request, multipart);
|
||||
|
||||
m_reply = std::shared_ptr<QNetworkReply>(rep);
|
||||
connect(rep, &QNetworkReply::uploadProgress, this, &ScreenShotUpload::downloadProgress);
|
||||
connect(rep, &QNetworkReply::finished, this, &ScreenShotUpload::downloadFinished);
|
||||
connect(rep, &QNetworkReply::uploadProgress, this, &ImgurUpload::downloadProgress);
|
||||
connect(rep, &QNetworkReply::finished, this, &ImgurUpload::downloadFinished);
|
||||
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)),
|
||||
SLOT(downloadError(QNetworkReply::NetworkError)));
|
||||
}
|
||||
void ScreenShotUpload::downloadError(QNetworkReply::NetworkError error)
|
||||
void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
|
||||
{
|
||||
QLOG_DEBUG() << m_reply->errorString();
|
||||
m_status = Job_Failed;
|
||||
}
|
||||
void ScreenShotUpload::downloadFinished()
|
||||
void ImgurUpload::downloadFinished()
|
||||
{
|
||||
if (m_status != Job_Failed)
|
||||
{
|
||||
@ -84,7 +84,7 @@ void ScreenShotUpload::downloadFinished()
|
||||
emit failed(m_index_within_job);
|
||||
return;
|
||||
}
|
||||
m_shot->imgurIndex = object.value("data").toObject().value("id").toString();
|
||||
m_shot->imgurId = object.value("data").toObject().value("id").toString();
|
||||
m_shot->url = object.value("data").toObject().value("link").toString();
|
||||
m_status = Job_Finished;
|
||||
emit succeeded(m_index_within_job);
|
||||
@ -98,7 +98,7 @@ void ScreenShotUpload::downloadFinished()
|
||||
return;
|
||||
}
|
||||
}
|
||||
void ScreenShotUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
||||
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
||||
{
|
||||
m_total_progress = bytesTotal;
|
||||
m_progress = bytesReceived;
|
@ -2,15 +2,14 @@
|
||||
#include "NetAction.h"
|
||||
|
||||
class ScreenShot;
|
||||
typedef std::shared_ptr<class ScreenShotUpload> ScreenShotUploadPtr;
|
||||
typedef std::shared_ptr<class ScreenShotGet> ScreenShotGetPtr;
|
||||
class ScreenShotUpload : public NetAction
|
||||
typedef std::shared_ptr<class ImgurUpload> ImgurUploadPtr;
|
||||
class ImgurUpload : public NetAction
|
||||
{
|
||||
public:
|
||||
explicit ScreenShotUpload(ScreenShot *shot);
|
||||
static ScreenShotUploadPtr make(ScreenShot *shot)
|
||||
explicit ImgurUpload(ScreenShot *shot);
|
||||
static ImgurUploadPtr make(ScreenShot *shot)
|
||||
{
|
||||
return ScreenShotUploadPtr(new ScreenShotUpload(shot));
|
||||
return ImgurUploadPtr(new ImgurUpload(shot));
|
||||
}
|
||||
|
||||
protected
|
@ -33,5 +33,5 @@ const QString FORGE_LEGACY_URL("http://files.minecraftforge.net/minecraftforge/j
|
||||
const QString FORGE_GRADLE_URL("http://files.minecraftforge.net/maven/net/minecraftforge/forge/json");
|
||||
const QString MOJANG_STATUS_URL("http://status.mojang.com/check");
|
||||
const QString MOJANG_STATUS_NEWS_URL("http://status.mojang.com/news");
|
||||
const QString IMGUR_UPLOAD_URL("https://api.imgur.com/3/upload.json");
|
||||
const QString IMGUR_BASE_URL("https://api.imgur.com/3/");
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ void SequentialTask::getProgress(qint64 ¤t, qint64 &total)
|
||||
}
|
||||
}
|
||||
|
||||
void SequentialTask::addTask(std::shared_ptr<Task> task)
|
||||
void SequentialTask::addTask(std::shared_ptr<ProgressProvider> task)
|
||||
{
|
||||
m_queue.append(task);
|
||||
}
|
||||
@ -43,7 +43,7 @@ void SequentialTask::startNext()
|
||||
{
|
||||
if (m_currentIndex != -1)
|
||||
{
|
||||
std::shared_ptr<Task> previous = m_queue[m_currentIndex];
|
||||
std::shared_ptr<ProgressProvider> previous = m_queue[m_currentIndex];
|
||||
disconnect(previous.get(), 0, this, 0);
|
||||
}
|
||||
m_currentIndex++;
|
||||
@ -52,7 +52,7 @@ void SequentialTask::startNext()
|
||||
emitSucceeded();
|
||||
return;
|
||||
}
|
||||
std::shared_ptr<Task> next = m_queue[m_currentIndex];
|
||||
std::shared_ptr<ProgressProvider> next = m_queue[m_currentIndex];
|
||||
connect(next.get(), SIGNAL(failed(QString)), this, SLOT(subTaskFailed(QString)));
|
||||
connect(next.get(), SIGNAL(status(QString)), this, SLOT(subTaskStatus(QString)));
|
||||
connect(next.get(), SIGNAL(progress(qint64,qint64)), this, SLOT(subTaskProgress()));
|
||||
@ -73,5 +73,12 @@ void SequentialTask::subTaskProgress()
|
||||
{
|
||||
qint64 current, total;
|
||||
getProgress(current, total);
|
||||
setProgress(100 * current / total);
|
||||
if (total == 0)
|
||||
{
|
||||
setProgress(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
setProgress(100 * current / total);
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public:
|
||||
virtual QString getStatus() const;
|
||||
virtual void getProgress(qint64 ¤t, qint64 &total);
|
||||
|
||||
void addTask(std::shared_ptr<Task> task);
|
||||
void addTask(std::shared_ptr<ProgressProvider> task);
|
||||
|
||||
protected:
|
||||
void executeTask();
|
||||
@ -27,6 +27,6 @@ slots:
|
||||
void subTaskProgress();
|
||||
|
||||
private:
|
||||
QQueue<std::shared_ptr<Task> > m_queue;
|
||||
QQueue<std::shared_ptr<ProgressProvider> > m_queue;
|
||||
int m_currentIndex;
|
||||
};
|
||||
|
Reference in New Issue
Block a user