KShare/uploaders/default/imguruploader.cpp

97 lines
4.5 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "imguruploader.hpp"
#include "imgursettingsdialog.hpp"
2017-04-23 15:05:48 +02:00
#include <QBuffer>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkReply>
#include <formats.hpp>
2017-04-23 15:05:48 +02:00
#include <io/ioutils.hpp>
2017-04-25 22:17:36 +02:00
#include <notifications.hpp>
2017-04-23 15:05:48 +02:00
#include <screenshotutil.hpp>
#include <settings.hpp>
struct SegfaultWorkaround {
SegfaultWorkaround(QByteArray a, ImgurUploader *u, QString m) : byteArray(), dis(u), mime(m) {
a.swap(byteArray);
QJsonObject object;
object.insert("client_id", settings::settings().value("imgur/cid").toString());
object.insert("client_secret", settings::settings().value("imgur/csecret").toString());
object.insert("grant_type", "refresh_token");
object.insert("refresh_token", settings::settings().value("imgur/refresh").toString());
ioutils::postJson(
QUrl("https://api.imgur.com/oauth2/token"),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
QJsonDocument::fromVariant(object.toVariantMap()).toJson(), [&](QJsonDocument response, QByteArray, QNetworkReply *r) {
qDebug() << response;
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
return;
}
QJsonObject res = response.object();
if (res.value("success").toBool()) {
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
return;
}
QString token = res["access_token"].toString();
settings::settings().setValue("imgur/expire", QDateTime::currentDateTimeUtc().addSecs(res["expires_in"].toInt()));
settings::settings().setValue("imgur/refresh", res["refresh_token"].toString());
settings::settings().setValue("imgur/access", token);
dis->handleSend(token.prepend("Bearer "), mime, byteArray);
QScopedPointer<SegfaultWorkaround>(this);
});
}
private:
QByteArray byteArray;
ImgurUploader *dis;
QString mime;
};
2017-04-23 15:05:48 +02:00
void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
if (byteArray.size() > 1e+7) {
2017-05-17 22:59:33 +02:00
notifications::notify("KShare imgur Uploader ", "Failed upload! Image too big");
return;
}
QString mime;
if (formats::normalFormatFromName(format) != formats::Normal::None)
mime = formats::normalFormatMIME(formats::normalFormatFromName(format));
else
mime = formats::recordingFormatMIME(formats::recordingFormatFromName(format));
if (settings::settings().contains("imgur/expire") //
&& settings::settings().contains("imgur/refresh") //
&& settings::settings().contains("imgur/access")) {
QDateTime expireTime = settings::settings().value("imgur/expire").toDateTime();
if (QDateTime::currentDateTimeUtc() > expireTime) {
new SegfaultWorkaround(byteArray, this, mime);
} else
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray);
} else
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
}
void ImgurUploader::showSettings() {
(new ImgurSettingsDialog())->show();
}
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
ioutils::postJson(QUrl("https://api.imgur.com/3/image"),
QList<QPair<QString, QString>>() << QPair<QString, QString>("Content-Type", mime.toUtf8())
<< QPair<QString, QString>("Authorization", auth),
2017-05-13 22:39:05 +02:00
byteArray, [](QJsonDocument res, QByteArray, QNetworkReply *) {
QString result = res.object()["data"].toObject()["link"].toString();
if (!result.isEmpty()) {
screenshotutil::toClipboard(result);
notifications::notify("KShare imgur Uploader ", "Uploaded to imgur!");
} else {
notifications::notify("KShare imgur Uploader ",
QString("Failed upload! imgur said: HTTP %2: %1")
.arg(res.object()["data"].toObject()["error"].toString())
.arg(QString::number(res.object()["status"].toInt())));
}
});
2017-04-23 15:05:48 +02:00
}