added Google Drive API

This commit is contained in:
Niklas 2020-02-22 14:17:14 +01:00
parent d75f993ef8
commit b63b82528f
7 changed files with 575 additions and 0 deletions

View File

@ -34,6 +34,7 @@ SOURCES += main.cpp\
screenshotter.cpp \ screenshotter.cpp \
utils.cpp \ utils.cpp \
uploaders/default/imguruploader.cpp \ uploaders/default/imguruploader.cpp \
uploaders/default/gdriveuploader.cpp \
io/ioutils.cpp \ io/ioutils.cpp \
settings.cpp \ settings.cpp \
uploaders/default/clipboarduploader.cpp \ uploaders/default/clipboarduploader.cpp \
@ -65,6 +66,7 @@ SOURCES += main.cpp\
hotkeyinputdialog.cpp \ hotkeyinputdialog.cpp \
cropeditor/drawing/arrowitem.cpp \ cropeditor/drawing/arrowitem.cpp \
uploaders/default/imgursettingsdialog.cpp \ uploaders/default/imgursettingsdialog.cpp \
uploaders/default/gdrivesettingsdialog.cpp \
filenamevalidator.cpp \ filenamevalidator.cpp \
logs/requestlogging.cpp \ logs/requestlogging.cpp \
monospacetextdialog.cpp \ monospacetextdialog.cpp \
@ -85,6 +87,7 @@ HEADERS += mainwindow.hpp \
screenshotter.hpp \ screenshotter.hpp \
utils.hpp \ utils.hpp \
uploaders/default/imguruploader.hpp \ uploaders/default/imguruploader.hpp \
uploaders/default/gdriveuploader.hpp \
io/ioutils.hpp \ io/ioutils.hpp \
settings.hpp \ settings.hpp \
uploaders/default/clipboarduploader.hpp \ uploaders/default/clipboarduploader.hpp \
@ -118,6 +121,7 @@ HEADERS += mainwindow.hpp \
hotkeyinputdialog.hpp \ hotkeyinputdialog.hpp \
cropeditor/drawing/arrowitem.hpp \ cropeditor/drawing/arrowitem.hpp \
uploaders/default/imgursettingsdialog.hpp \ uploaders/default/imgursettingsdialog.hpp \
uploaders/default/gdrivesettingsdialog.hpp \
filenamevalidator.hpp \ filenamevalidator.hpp \
logs/requestlogging.hpp \ logs/requestlogging.hpp \
monospacetextdialog.hpp \ monospacetextdialog.hpp \
@ -189,6 +193,7 @@ FORMS += mainwindow.ui \
aboutbox.ui \ aboutbox.ui \
hotkeyinputdialog.ui \ hotkeyinputdialog.ui \
uploaders/default/imgursettingsdialog.ui \ uploaders/default/imgursettingsdialog.ui \
uploaders/default/gdrivesettingsdialog.ui \
monospacetextdialog.ui \ monospacetextdialog.ui \
screenoverlay/screenoverlaysettings.ui screenoverlay/screenoverlaysettings.ui

View File

@ -0,0 +1,177 @@
#include "gdrivesettingsdialog.hpp"
#include "ui_gdrivesettingsdialog.h"
#include <QDesktopServices>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
#include <QNetworkReply>
#include <QPushButton>
#include <QUrl>
#include <io/ioutils.hpp>
#include <settings.hpp>
GDriveSettingsDialog::GDriveSettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::GDriveSettingsDialog) {
ui->setupUi(this);
connect(this, &GDriveSettingsDialog::accepted, this, &GDriveSettingsDialog::deleteLater);
ui->clientId->setText(settings::settings().value("google/cid").toString());
ui->clientSecret->setText(settings::settings().value("google/csecret").toString());
ui->folderId->setText(settings::settings().value("google/folder").toString().toUtf8());
ui->isPublic->setChecked(settings::settings().value("google/public").toBool());
}
GDriveSettingsDialog::~GDriveSettingsDialog() {
delete ui;
}
void GDriveSettingsDialog::on_addApp_clicked() {
QDesktopServices::openUrl(
QUrl(QString("https://accounts.google.com/o/oauth2/auth?client_id=%1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive").arg(ui->clientId->text())));
}
void GDriveSettingsDialog::on_isPublic_clicked(bool checked) {
settings::settings().setValue("google/public", checked);
}
void GDriveSettingsDialog::on_folderId_textChanged(QString arg1) {
settings::settings().setValue("google/folder", arg1);
}
bool GDriveSettingsDialog::checkAuthorization () {
if (ui->folderId->text().isEmpty()) return false;
settings::settings().setValue("google/folder", ui->folderId->text());
if (settings::settings().contains("google/expire") //
&& settings::settings().contains("google/refresh") //
&& settings::settings().contains("google/access")) {
QDateTime expireTime = settings::settings().value("google/expire").toDateTime();
if (QDateTime::currentDateTimeUtc() > expireTime) {
// Token expired
ui->status->setText(tr("Token expired"));
} else {
ui->buttonBox->setEnabled(false);
ui->testButton->setEnabled(false);
ioutils::getData(QUrl(QString("https://www.googleapis.com/drive/v3/files?q='%1'+in+parents&fields=files(md5Checksum,+originalFilename)").arg(ui->folderId->text())),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Authorization", settings::settings().value("google/access").toString().prepend("Bearer ")) }),
[&](QByteArray a, QNetworkReply *r) {
QVariant statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isValid()) return;
ui->testButton->setEnabled(true);
int status = statusCode.toInt();
if (status != 200 && status != 400)
{
QString reason = r->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
qDebug() << reason;
ui->buttonBox->setEnabled(true);
ui->status->setText(reason);
return;
}
QJsonDocument response = QJsonDocument::fromJson(a);
if (!response.isObject()) {
ui->buttonBox->setEnabled(true);
ui->status->setText("Invalid JSON");
return;
}
QJsonObject res = response.object();
if (res.contains("error")) {
ui->buttonBox->setEnabled(true);
ui->status->setText(res.value("error_description").toString().toUtf8());
ui->status->setStyleSheet("* { color: red; }");
return;
}
ui->buttonBox->setEnabled(true);
ui->testButton->hide();
ui->status->setText(tr("It works!"));
ui->status->setStyleSheet("* { color: green; }");
});
}
} else {
// No Token set
ui->status->setText(tr("Login with Google needed"));
}
}
void GDriveSettingsDialog::on_testButton_clicked() {
GDriveSettingsDialog::checkAuthorization();
}
void GDriveSettingsDialog::on_authorize_clicked() {
if (ui->pin->text().isEmpty() || ui->folderId->text().isEmpty()) return;
ui->buttonBox->setEnabled(false);
QJsonObject object;
object.insert("client_id", ui->clientId->text());
object.insert("client_secret", ui->clientSecret->text());
object.insert("grant_type", "authorization_code");
object.insert("code", ui->pin->text());
object.insert("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
settings::settings().setValue("google/cid", ui->clientId->text());
settings::settings().setValue("google/csecret", ui->clientSecret->text());
settings::settings().setValue("google/folder", ui->folderId->text());
settings::settings().setValue("google/public", ui->isPublic->isChecked());
ioutils::postJson(QUrl("https://accounts.google.com/o/oauth2/token"),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
QJsonDocument::fromVariant(object.toVariantMap()).toJson(),
[&](QJsonDocument response, QByteArray, QNetworkReply *r) {
QVariant statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isValid()) return;
int status = statusCode.toInt();
if (status != 200 && status != 400)
{
QString reason = r->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
qDebug() << reason;
ui->buttonBox->setEnabled(true);
return;
}
if (!response.isObject()) {
ui->buttonBox->setEnabled(true);
return;
}
QJsonObject res = response.object();
if (res.contains("error")) {
ui->buttonBox->setEnabled(true);
ui->status->setText(res.value("error_description").toString().toUtf8());
ui->status->setStyleSheet("* { color: red; }");
return;
}
if (res.value("success").toBool()) {
ui->buttonBox->setEnabled(true);
return;
}
settings::settings().setValue("google/expire",
QDateTime::currentDateTimeUtc().addSecs(res["expires_in"].toInt()));
settings::settings().setValue("google/refresh", res["refresh_token"].toString().toUtf8());
settings::settings().setValue("google/access", res["access_token"].toString().toUtf8());
ui->status->setText(tr("It works!"));
ui->status->setStyleSheet("* { color: green; }");
ui->authorize->setEnabled(false);
ui->addApp->setEnabled(false);
ui->clientSecret->setEnabled(false);
ui->clientId->setEnabled(false);
ui->buttonBox->setEnabled(true);
ui->testButton->hide();
});
}

View File

@ -0,0 +1,29 @@
#ifndef GDRIVESETTINGSDIALOG_HPP
#define GDRIVESETTINGSDIALOG_HPP
#include <QDialog>
namespace Ui {
class GDriveSettingsDialog;
}
class GDriveSettingsDialog : public QDialog {
Q_OBJECT
public:
explicit GDriveSettingsDialog(QWidget *parent = 0);
~GDriveSettingsDialog();
private slots:
void on_addApp_clicked();
void on_authorize_clicked();
void on_testButton_clicked();
bool checkAuthorization();
void on_isPublic_clicked(bool);
void on_folderId_textChanged(QString);
private:
Ui::GDriveSettingsDialog *ui;
};
#endif // GRDIVESETTINGSDIALOG_HPP

View File

@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GDriveSettingsDialog</class>
<widget class="QDialog" name="GDriveSettingsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>287</width>
<height>389</height>
</rect>
</property>
<property name="windowTitle">
<string>GDrive auth</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>OAuth2</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Noto Sans'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:10pt;&quot;&gt;Authentication needed&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<widget class="QPushButton" name="addApp">
<property name="text">
<string>Login with Google</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Enter verification code&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QLineEdit" name="pin">
<property name="placeholderText">
<string>Authorization code</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="QPushButton" name="authorize">
<property name="text">
<string>Authorize</string>
</property>
</widget>
</item>
<item row="14" column="0" colspan="2">
<widget class="QLabel" name="status">
<property name="styleSheet">
<string notr="true">* { color: red; }</string>
</property>
<property name="text">
<string>Not working</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Client ID/Client Secret</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="clientSecret">
<property name="placeholderText">
<string>Client Secret</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="clientId">
<property name="placeholderText">
<string>Client ID</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Upload Public</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Folder ID</string>
</property>
</widget>
</item>
<item row="15" column="0" colspan="2">
<widget class="QPushButton" name="testButton">
<property name="text">
<string>Check Authorization</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLineEdit" name="folderId"/>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="isPublic">
<property name="text">
<string>Is Public</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>GDriveSettingsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>GDriveSettingsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -0,0 +1,161 @@
#include "gdriveuploader.hpp"
#include <QBuffer>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkReply>
#include <QHttpMultiPart>
#include <QHttpPart>
#include <QString>
#include <formats.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <settings.hpp>
#include <utils.hpp>
#include <logs/screenshotfile.h>
#include <uploaders/default/gdrivesettingsdialog.hpp>
struct SegfaultWorkaround { // I'm a scrub for doing this
SegfaultWorkaround(QByteArray a, GDriveUploader *u, QString m, bool isPublic, ScreenshotFile sf) : byteArray(), dis(u), mime(m) {
a.swap(byteArray);
QJsonObject object;
object.insert("client_id", settings::settings().value("google/cid").toString());
object.insert("client_secret", settings::settings().value("google/csecret").toString());
object.insert("grant_type", "refresh_token");
object.insert("refresh_token", settings::settings().value("google/refresh").toString());
ioutils::postJson(
QUrl("https://accounts.google.com/o/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()) {
notifications::notify(QObject::tr("KShare Google Uploader"),
QString(QObject::tr("Failed upload! Error in refresh token")));
notifications::playSound(notifications::Sound::ERROR);
return;
}
QJsonObject res = response.object();
QString token = res["access_token"].toString();
settings::settings().setValue("google/expire", QDateTime::currentDateTimeUtc().addSecs(res["expires_in"].toInt()));
settings::settings().setValue("google/refresh", res["refresh_token"].toString());
settings::settings().setValue("google/access", token);
dis->handleSend(token.prepend("Bearer "), isPublic, mime, byteArray, sf);
QScopedPointer<SegfaultWorkaround>(this);
});
}
private:
QByteArray byteArray;
GDriveUploader *dis;
QString mime;
bool isPublic;
ScreenshotFile sf;
}; // I feel terrible for making this. I am sorry, reader
void GDriveUploader::doUpload(QByteArray byteArray, QString format, ScreenshotFile sf) {
if (byteArray.size() > 1e+7) {
notifications::notify(tr("KShare imgur Uploader"), tr("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("google/expire") //
&& settings::settings().contains("google/refresh") //
&& settings::settings().contains("google/access")) {
QDateTime expireTime = settings::settings().value("google/expire").toDateTime();
bool isPublic = settings::settings().value("google/public", false).toBool();
if (QDateTime::currentDateTimeUtc() > expireTime) {
qDebug() << "Need to refresh Google Access Token";
new SegfaultWorkaround(byteArray, this, mime, isPublic, sf);
} else handleSend("Bearer " + settings::settings().value("google/access", "").toString().toUtf8(), isPublic, mime, byteArray, sf);
}
}
void GDriveUploader::showSettings() {
(new GDriveSettingsDialog())->show();
}
void GDriveUploader::setPermission(QString fileID, QString auth, QString role, QString type, bool allowFileDiscovery) {
QJsonObject object;
object.insert("role", role);
object.insert("type", type);
object.insert("allowFileDiscovery", QString(allowFileDiscovery));
QJsonDocument doc(object);
ioutils::postData(QUrl(QString("https://www.googleapis.com/drive/v3/files/%1/permissions").arg(fileID)),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "application/json"), QPair<QString, QString>("Authorization", auth) }),
QString(doc.toJson(QJsonDocument::Compact)).toUtf8(),
[&](QByteArray , QNetworkReply *) {
});
}
void GDriveUploader::handleSend(QString auth, bool isPublic, QString mime, QByteArray byteArray, ScreenshotFile sf) {
QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::RelatedType);
QHttpPart metaPart;
metaPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
metaPart.setHeader(
QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"metadata\"")
);
QString data("{name: '"+sf.getFilename()+"', parents: ['"+settings::settings().value("google/folder").toString().toUtf8()+"']}");
metaPart.setBody(data.toUtf8());
multipart->append(metaPart);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime.toUtf8()));
imagePart.setHeader(
QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file\"; filename=\""+sf.getFilename()+"\"")
);
imagePart.setBody(byteArray);
multipart->append(imagePart);
ioutils::postMultipartData(QUrl("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink"),
QList<QPair<QString, QString>>() << QPair<QString, QString>("Authorization", auth),
multipart, [byteArray, this, mime, sf, auth, isPublic](QByteArray res, QNetworkReply *r) {
qDebug() << QString(res).toUtf8();
if (r->error() == QNetworkReply::ContentAccessDenied) {
(new GDriveSettingsDialog())->show();
return;
}
QString result = QString(res).toUtf8();
if (!result.isEmpty()) {
QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
if(!doc.isObject()) {
notifications::notify(tr("KShare Google Uploader "),
QString(tr("Failed upload! Invalid JSON")));
notifications::playSound(notifications::Sound::ERROR);
return;
}
if(isPublic) {
setPermission(doc.object()["id"].toString(), auth);
}
result = doc.object()["webViewLink"].toString();
ioutils::addLogEntry(r, res, result, sf);
notifications::notify(tr("KShare Google Uploader"), tr("Uploaded to Google!"));
notifications::playSound(notifications::Sound::SUCCESS);
utils::toClipboard(result);
} else {
ioutils::addLogEntry(r, res, result, sf);
notifications::notify(tr("KShare Google Uploader "),
QString(tr("Failed upload! Google said: HTTP %1: %2"))
.arg(r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt())
.arg(r->errorString()));
notifications::playSound(notifications::Sound::ERROR);
}
});
}

View File

@ -0,0 +1,27 @@
#ifndef GDRIVEUPLOADER_HPP
#define GRIVEUPLOADER_HPP
#include "../uploader.hpp"
#include <QApplication>
#include <logs/screenshotfile.h>
class GDriveUploader : public Uploader {
Q_DECLARE_TR_FUNCTIONS(GDriveUploader)
friend struct SegfaultWorkaround;
public:
QString name() override {
return "Google Drive";
}
QString description() override {
return "drive.google.com uploader";
}
void doUpload(QByteArray byteArray, QString, ScreenshotFile sf) override;
void showSettings() override;
void setPermission(QString, QString, QString = "reader", QString = "anyone", bool = false);
private:
void handleSend(QString auth, bool isPublic, QString mime, QByteArray byteArray, ScreenshotFile sf);
};
#endif // GDRIVEUPLOADER_HPP

View File

@ -2,6 +2,7 @@
#include "customuploader.hpp" #include "customuploader.hpp"
#include "default/clipboarduploader.hpp" #include "default/clipboarduploader.hpp"
#include "default/imguruploader.hpp" #include "default/imguruploader.hpp"
#include "default/gdriveuploader.hpp"
#include <QBuffer> #include <QBuffer>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
@ -33,6 +34,7 @@ UploaderSingleton::UploaderSingleton() : QObject() {
// UPLOADERS // UPLOADERS
registerUploader(new ImgurUploader); registerUploader(new ImgurUploader);
registerUploader(new GDriveUploader);
registerUploader(new ClipboardUploader); registerUploader(new ClipboardUploader);
// --------- // ---------