* added result url to history
* added context menu to history * added example config that work with Gurkengewuerz/php_filehoster and is ShareX compatible * added filename as parameter to custom uploader
This commit is contained in:
parent
828c9542ad
commit
3c9a87583a
15
examples/php_filehost.uploader
Normal file
15
examples/php_filehost.uploader
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Gurkengewuerz Share",
|
||||
"target": "https://share.example.com/index.php?token=XXXXXXXXXXXXXXXXX",
|
||||
"format": "multipart-form-data",
|
||||
"body": [
|
||||
{
|
||||
"__Content-Type": "/%contenttype/",
|
||||
"filename": "/%filename/",
|
||||
"name": "file",
|
||||
"body": "/%imagedata/"
|
||||
}
|
||||
],
|
||||
"return": "|"
|
||||
}
|
||||
|
@ -8,11 +8,12 @@
|
||||
|
||||
QNetworkAccessManager ioutils::networkManager;
|
||||
|
||||
void addLogEntry(QNetworkReply *reply, QByteArray data, QString filename) {
|
||||
void ioutils::addLogEntry(QNetworkReply* reply, QByteArray data, QString result, QString filename) {
|
||||
requestlogging::RequestContext ctx;
|
||||
|
||||
ctx.reply = reply;
|
||||
ctx.response = data;
|
||||
ctx.result = result;
|
||||
ctx.filename = filename;
|
||||
|
||||
requestlogging::addEntry(ctx);
|
||||
@ -31,7 +32,6 @@ void removeTask() {
|
||||
void ioutils::postMultipart(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QHttpMultiPart *body,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
@ -39,10 +39,9 @@ void ioutils::postMultipart(QUrl target,
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(QJsonDocument::fromJson(data), data, reply);
|
||||
delete reply;
|
||||
});
|
||||
@ -51,7 +50,6 @@ void ioutils::postMultipart(QUrl target,
|
||||
void ioutils::postMultipartData(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QHttpMultiPart *body,
|
||||
QString filename,
|
||||
std::function<void(QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
@ -59,10 +57,9 @@ void ioutils::postMultipartData(QUrl target,
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(data, reply);
|
||||
delete reply;
|
||||
});
|
||||
@ -70,7 +67,6 @@ void ioutils::postMultipartData(QUrl target,
|
||||
|
||||
void ioutils::getJson(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
@ -78,10 +74,9 @@ void ioutils::getJson(QUrl target,
|
||||
}
|
||||
QNetworkReply *reply = networkManager.get(req);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(QJsonDocument::fromJson(data), data, reply);
|
||||
reply->deleteLater();
|
||||
});
|
||||
@ -90,7 +85,6 @@ void ioutils::getJson(QUrl target,
|
||||
void ioutils::postJson(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QByteArray body,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
@ -98,26 +92,24 @@ void ioutils::postJson(QUrl target,
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(QJsonDocument::fromJson(data), data, reply);
|
||||
delete reply;
|
||||
});
|
||||
}
|
||||
|
||||
void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback) {
|
||||
void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, std::function<void(QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.get(req);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(data, reply);
|
||||
delete reply;
|
||||
});
|
||||
@ -126,7 +118,6 @@ void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, QStri
|
||||
void ioutils::postData(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QByteArray body,
|
||||
QString filename,
|
||||
std::function<void(QByteArray, QNetworkReply *)> callback) {
|
||||
QNetworkRequest req(target);
|
||||
for (auto header : headers) {
|
||||
@ -134,10 +125,9 @@ void ioutils::postData(QUrl target,
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data, filename);
|
||||
callback(data, reply);
|
||||
delete reply;
|
||||
});
|
||||
|
@ -9,26 +9,23 @@
|
||||
|
||||
namespace ioutils {
|
||||
extern QNetworkAccessManager networkManager;
|
||||
void addLogEntry(QNetworkReply* reply, QByteArray data, QString result, QString filename);
|
||||
void getJson(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
|
||||
void postJson(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QByteArray body,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
|
||||
void getData(QUrl target, QList<QPair<QString, QString>> headers, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback);
|
||||
void postData(QUrl target, QList<QPair<QString, QString>> headers, QByteArray body, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback);
|
||||
void getData(QUrl target, QList<QPair<QString, QString>> headers, std::function<void(QByteArray, QNetworkReply *)> callback);
|
||||
void postData(QUrl target, QList<QPair<QString, QString>> headers, QByteArray body, std::function<void(QByteArray, QNetworkReply *)> callback);
|
||||
void postMultipart(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QHttpMultiPart *body,
|
||||
QString filename,
|
||||
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
|
||||
void postMultipartData(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
QHttpMultiPart *body,
|
||||
QString filename,
|
||||
std::function<void(QByteArray, QNetworkReply *)> callback);
|
||||
QString methodString(QNetworkAccessManager::Operation operation);
|
||||
QString httpString(int responseCode);
|
||||
|
@ -39,6 +39,7 @@ void requestlogging::addEntry(RequestContext context) {
|
||||
|
||||
QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type
|
||||
<< context.reply->url().toString().replace(" ", "%20") << " " // $url
|
||||
<< context.result.replace(" ", "%20") << " " // $result
|
||||
<< context.filename.replace(" ", "_") << " " // $filename
|
||||
<< context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << " " // $status
|
||||
<< timeNow.replace(" ", "_") << endl
|
||||
@ -48,6 +49,7 @@ void requestlogging::addEntry(RequestContext context) {
|
||||
MainWindow::inst()->addResponse(
|
||||
context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
|
||||
context.filename,
|
||||
context.result,
|
||||
context.reply->url().toString(),
|
||||
timeNow.replace("_", " "));
|
||||
}
|
||||
@ -66,6 +68,7 @@ QList<LoggedRequest> requestlogging::getRequests() {
|
||||
QTextStream stream(&line);
|
||||
stream >> r.type;
|
||||
stream >> r.url;
|
||||
stream >> r.result;
|
||||
stream >> r.filename;
|
||||
stream >> r.responseCode;
|
||||
stream >> r.time;
|
||||
|
@ -12,6 +12,7 @@ namespace requestlogging {
|
||||
QByteArray response;
|
||||
QNetworkReply *reply;
|
||||
QString filename;
|
||||
QString result;
|
||||
};
|
||||
|
||||
class LoggedRequest {
|
||||
@ -27,6 +28,9 @@ namespace requestlogging {
|
||||
QString getType() {
|
||||
return type;
|
||||
}
|
||||
QString getResult() {
|
||||
return result;
|
||||
}
|
||||
QString getTime() {
|
||||
return time;
|
||||
}
|
||||
@ -40,6 +44,7 @@ namespace requestlogging {
|
||||
private:
|
||||
QString url;
|
||||
QString filename;
|
||||
QString result;
|
||||
QString type;
|
||||
QString time;
|
||||
int responseCode;
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include "settingsdialog.hpp"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "utils.hpp"
|
||||
#include <QApplication>
|
||||
#include <QClipboard>
|
||||
#include <QMessageBox>
|
||||
#include <QShortcut>
|
||||
#include <colorpicker/colorpickerscene.hpp>
|
||||
@ -113,6 +115,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
||||
connect(ui->clipboardButton, &QPushButton::clicked, this, &MainWindow::openScreenshotFolder);
|
||||
connect(ui->colorPickerButton, &QPushButton::clicked, this, [] { ColorPickerScene::showPicker(); });
|
||||
|
||||
ui->treeWidget->addAction(ui->actionOpenURL);
|
||||
ui->treeWidget->addAction(ui->actionOpenLocalFile);
|
||||
ui->treeWidget->addAction(ui->actionOpenRequest);
|
||||
ui->treeWidget->addAction(ui->actionCopyLinktoClipboard);
|
||||
|
||||
ui->aboutButton->setFocus();
|
||||
|
||||
tray->setContextMenu(menu);
|
||||
@ -132,7 +139,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
||||
|
||||
QList<LoggedRequest> requests = requestlogging::getRequests();
|
||||
for (LoggedRequest req : requests) {
|
||||
addResponse(req.getResponseCode(), req.getFilename(), req.getUrl(), req.getTime());
|
||||
addResponse(req.getResponseCode(), req.getFilename(), req.getResult(), req.getUrl(), req.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,14 +180,6 @@ void MainWindow::on_actionQuit_triggered() {
|
||||
quit();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionFullscreen_triggered() {
|
||||
screenshotter::fullscreenDelayed();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionArea_triggered() {
|
||||
screenshotter::areaDelayed();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionStart_triggered() {
|
||||
rec();
|
||||
}
|
||||
@ -189,10 +188,6 @@ void MainWindow::on_actionStop_triggered() {
|
||||
controller->end();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionColor_Picker_triggered() {
|
||||
ColorPickerScene::showPicker();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSettings_triggered() {
|
||||
SettingsDialog *dialog = new SettingsDialog(this);
|
||||
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
||||
@ -205,16 +200,12 @@ void MainWindow::on_actionAbout_triggered() {
|
||||
box->show();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionActive_window_triggered() {
|
||||
screenshotter::activeDelayed();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionAbort_triggered() {
|
||||
controller->abort();
|
||||
}
|
||||
|
||||
void MainWindow::on_treeWidget_doubleClicked(const QModelIndex &) {
|
||||
QString file = ui->treeWidget->currentItem()->text(3);
|
||||
void MainWindow::on_actionOpenRequest_triggered() {
|
||||
QString file = ui->treeWidget->currentItem()->text(4);
|
||||
file = settings::dir().absoluteFilePath("responses/" + file.left(file.length() - 4));
|
||||
|
||||
QFile dataFile(file);
|
||||
@ -228,6 +219,25 @@ void MainWindow::on_treeWidget_doubleClicked(const QModelIndex &) {
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
void MainWindow::on_actionOpenURL_triggered() {
|
||||
QDesktopServices::openUrl(QUrl(ui->treeWidget->currentItem()->text(2)));
|
||||
}
|
||||
|
||||
void MainWindow::on_actionOpenLocalFile_triggered() {
|
||||
QString file = ui->treeWidget->currentItem()->text(1);
|
||||
file = settings::dir().absoluteFilePath("responses/" + file.left(file.length() - 4));
|
||||
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(file));
|
||||
}
|
||||
|
||||
void MainWindow::on_actionCopyLinktoClipboard_triggered() {
|
||||
QApplication::clipboard()->setText(ui->treeWidget->currentItem()->text(2));
|
||||
}
|
||||
|
||||
void MainWindow::on_treeWidget_doubleClicked(const QModelIndex &) {
|
||||
on_actionOpenURL_triggered();
|
||||
}
|
||||
|
||||
void MainWindow::openScreenshotFolder() {
|
||||
QDir saveDir;
|
||||
switch (settings::settings().value("saveLocation", 1).toInt()) {
|
||||
@ -258,9 +268,9 @@ void MainWindow::setTrayIcon(QIcon icon) {
|
||||
tray->setIcon(icon);
|
||||
}
|
||||
|
||||
void MainWindow::addResponse(int httpCode, QString filename, QString url, QString time) {
|
||||
void MainWindow::addResponse(int httpCode, QString filename, QString result, QString url, QString time) {
|
||||
QString httpStatus = ioutils::httpString(httpCode);
|
||||
QTreeWidgetItem* tw = new QTreeWidgetItem({ QString::number(httpCode) + " " + httpStatus, filename, url, time + " UTC" });
|
||||
QTreeWidgetItem* tw = new QTreeWidgetItem({ QString::number(httpCode) + " " + httpStatus, filename, result, url, time + " UTC" });
|
||||
|
||||
if(httpCode >= 200 && httpCode < 300) {
|
||||
tw->setIcon(0, *(new QIcon(":/icons/checked.png")));
|
||||
|
@ -21,15 +21,15 @@ private slots:
|
||||
void toggleVisible();
|
||||
|
||||
void on_actionQuit_triggered();
|
||||
void on_actionFullscreen_triggered();
|
||||
void on_actionArea_triggered();
|
||||
void on_actionStart_triggered();
|
||||
void on_actionStop_triggered();
|
||||
void on_actionSettings_triggered();
|
||||
void on_actionColor_Picker_triggered();
|
||||
void on_actionAbout_triggered();
|
||||
void on_actionActive_window_triggered();
|
||||
void on_actionAbort_triggered();
|
||||
void on_actionOpenRequest_triggered();
|
||||
void on_actionOpenURL_triggered();
|
||||
void on_actionOpenLocalFile_triggered();
|
||||
void on_actionCopyLinktoClipboard_triggered();
|
||||
void openScreenshotFolder();
|
||||
void on_treeWidget_doubleClicked(const QModelIndex &);
|
||||
|
||||
@ -39,7 +39,7 @@ public:
|
||||
~MainWindow();
|
||||
bool valid();
|
||||
void setTrayIcon(QIcon icon);
|
||||
void addResponse(int httpCode, QString filename, QString url, QString time);
|
||||
void addResponse(int httpCode, QString filename, QString result, QString url, QString time);
|
||||
RecordingController *controller = new RecordingController;
|
||||
|
||||
QSystemTrayIcon *tray;
|
||||
|
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>819</width>
|
||||
<height>388</height>
|
||||
<width>931</width>
|
||||
<height>386</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
@ -129,6 +129,9 @@
|
||||
<layout class="QGridLayout" name="gridLayout_5">
|
||||
<item row="0" column="0">
|
||||
<widget class="QTreeWidget" name="treeWidget">
|
||||
<property name="contextMenuPolicy">
|
||||
<enum>Qt::ActionsContextMenu</enum>
|
||||
</property>
|
||||
<property name="indentation">
|
||||
<number>0</number>
|
||||
</property>
|
||||
@ -156,6 +159,11 @@
|
||||
<string>URL</string>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Request</string>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Time</string>
|
||||
@ -195,7 +203,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>819</width>
|
||||
<width>931</width>
|
||||
<height>29</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -227,21 +235,6 @@
|
||||
<string notr="true">Ctrl+Q</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionFullscreen">
|
||||
<property name="text">
|
||||
<string>&Fullscreen</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionArea">
|
||||
<property name="text">
|
||||
<string>&Area</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionColor_Picker">
|
||||
<property name="text">
|
||||
<string>&Color Picker</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionStart">
|
||||
<property name="text">
|
||||
<string>&Start</string>
|
||||
@ -262,16 +255,31 @@
|
||||
<string>&About</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionActive_window">
|
||||
<property name="text">
|
||||
<string>Active &window</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionAbort">
|
||||
<property name="text">
|
||||
<string>&Abort</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionOpenURL">
|
||||
<property name="text">
|
||||
<string>Open URL</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionOpenLocalFile">
|
||||
<property name="text">
|
||||
<string>Open Local File</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionOpenRequest">
|
||||
<property name="text">
|
||||
<string>Open Request</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionCopyLinktoClipboard">
|
||||
<property name="text">
|
||||
<string>Copy Link to Clipboard</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources>
|
||||
|
@ -210,7 +210,7 @@ QString parsePathspec(QJsonDocument &response, QString &pathspec) {
|
||||
return "";
|
||||
}
|
||||
|
||||
void CustomUploader::parseResult(QJsonDocument result, QByteArray data, QString returnPathspec, QString name) {
|
||||
void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename) {
|
||||
if (result.isObject()) {
|
||||
QString url
|
||||
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
|
||||
@ -218,19 +218,22 @@ void CustomUploader::parseResult(QJsonDocument result, QByteArray data, QString
|
||||
if (!url.isEmpty()) {
|
||||
QApplication::clipboard()->setText(url);
|
||||
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!"));
|
||||
ioutils::addLogEntry(r, data, url, filename);
|
||||
} else {
|
||||
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Upload done, but result empty!"));
|
||||
QApplication::clipboard()->setText(data);
|
||||
ioutils::addLogEntry(r, data, "", filename);
|
||||
}
|
||||
} else {
|
||||
playErrorSound();
|
||||
notifications::notify(tr("KShare Custom Uploader ") + name,
|
||||
tr("Upload done, but result is not JSON Object! Result in clipboard."));
|
||||
QApplication::clipboard()->setText(data);
|
||||
ioutils::addLogEntry(r, data, "", filename);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray substituteArgs(QByteArray arr, QString format, QByteArray imgData = QByteArray()) {
|
||||
QByteArray substituteArgs(QByteArray arr, QString format, QString filename, QByteArray imgData = QByteArray()) {
|
||||
QString mime = normalFormatMIME(normalFormatFromName(format));
|
||||
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
|
||||
if (arr.startsWith("/") && arr.endsWith("/")) {
|
||||
@ -240,6 +243,9 @@ QByteArray substituteArgs(QByteArray arr, QString format, QByteArray imgData = Q
|
||||
{ { "format", format.toLower() }, { "FORMAT", format }, { "contenttype", mime } })
|
||||
.toUtf8();
|
||||
|
||||
QByteArray fA = filename.toLocal8Bit();
|
||||
arr.replace("%filename", fA.data());
|
||||
|
||||
if (imgData.isNull()) return arr;
|
||||
return arr.replace("%imagedata", imgData);
|
||||
} else
|
||||
@ -247,17 +253,17 @@ QByteArray substituteArgs(QByteArray arr, QString format, QByteArray imgData = Q
|
||||
}
|
||||
|
||||
|
||||
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString format) {
|
||||
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString format, QString filename) {
|
||||
QJsonObject o;
|
||||
for (QString s : body.keys()) {
|
||||
QJsonValue v = body[s];
|
||||
if (v.isObject()) {
|
||||
QJsonObject vo = v.toObject();
|
||||
o.insert(s, recurseAndReplace(vo, data, format));
|
||||
o.insert(s, recurseAndReplace(vo, data, format, filename));
|
||||
} else if (v.isString()) {
|
||||
QString str = v.toString();
|
||||
if (str.startsWith("/") && str.endsWith("/")) {
|
||||
o.insert(s, QString::fromUtf8(substituteArgs(str.toUtf8(), format, data)));
|
||||
o.insert(s, QString::fromUtf8(substituteArgs(str.toUtf8(), format, filename, data)));
|
||||
} else
|
||||
o.insert(s, v);
|
||||
} else
|
||||
@ -277,10 +283,10 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
} break;
|
||||
case RequestFormat::JSON: {
|
||||
if (body.isString()) {
|
||||
data = substituteArgs(body.toString().toUtf8(), format, imgData);
|
||||
data = substituteArgs(body.toString().toUtf8(), format, filename, imgData);
|
||||
} else {
|
||||
QJsonObject vo = body.toObject();
|
||||
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format).toVariantMap()).toJson();
|
||||
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format, filename).toVariantMap()).toJson();
|
||||
}
|
||||
} break;
|
||||
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
||||
@ -288,7 +294,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
for (QString key : body.keys()) {
|
||||
QJsonValue val = body[key];
|
||||
if (val.isString()) {
|
||||
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, imgData));
|
||||
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, filename, imgData));
|
||||
} else {
|
||||
if (!data.isEmpty()) data.append('&');
|
||||
data.append(QUrl::toPercentEncoding(key))
|
||||
@ -307,7 +313,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
QHttpPart part;
|
||||
QJsonValue bd = valo["body"];
|
||||
if (bd.isString()) {
|
||||
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, imgData);
|
||||
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, filename, imgData);
|
||||
QByteArray *bodyHeap = new QByteArray;
|
||||
body.swap(*bodyHeap);
|
||||
QBuffer *buffer = new QBuffer(bodyHeap);
|
||||
@ -317,7 +323,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
arraysToDelete.append(bodyHeap);
|
||||
} else {
|
||||
auto bdo = bd.toObject();
|
||||
QJsonObject result = recurseAndReplace(bdo, imgData, format);
|
||||
QJsonObject result = recurseAndReplace(bdo, imgData, format, filename);
|
||||
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
|
||||
}
|
||||
QByteArray cdh("form-data");
|
||||
@ -325,11 +331,11 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
if (headerVal.startsWith("__")) {
|
||||
headerVal = headerVal.mid(2);
|
||||
QByteArray str = valo["__" + headerVal].toString().toUtf8();
|
||||
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format);
|
||||
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format, filename);
|
||||
part.setRawHeader(headerVal.toLatin1(), str);
|
||||
} else if (headerVal != "body")
|
||||
cdh += "; " + headerVal + "=\""
|
||||
+ substituteArgs(valo[headerVal].toString().toUtf8(), format).replace("\"", "\\\"") + "\"";
|
||||
+ substituteArgs(valo[headerVal].toString().toUtf8(), format, filename).replace("\"", "\\\"") + "\"";
|
||||
}
|
||||
part.setHeader(QNetworkRequest::ContentDispositionHeader, cdh);
|
||||
multipart->append(part);
|
||||
@ -337,8 +343,9 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
switch (method) {
|
||||
case HttpMethod::POST:
|
||||
if (returnPathspec == "|") {
|
||||
ioutils::postMultipartData(target, h, multipart, filename,
|
||||
[&, buffersToDelete, arraysToDelete](QByteArray result, QNetworkReply *) {
|
||||
ioutils::postMultipartData(target, h, multipart,
|
||||
[&, buffersToDelete, arraysToDelete, filename](QByteArray result, QNetworkReply *r) {
|
||||
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
|
||||
QApplication::clipboard()->setText(QString::fromUtf8(result));
|
||||
for (auto buffer : buffersToDelete) buffer->deleteLater();
|
||||
for (auto arr : arraysToDelete) delete arr;
|
||||
@ -347,11 +354,11 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
tr("Copied upload result to clipboard!"));
|
||||
});
|
||||
} else {
|
||||
ioutils::postMultipart(target, h, multipart, filename,
|
||||
[&, buffersToDelete, arraysToDelete](QJsonDocument result, QByteArray data, QNetworkReply *) {
|
||||
ioutils::postMultipart(target, h, multipart,
|
||||
[&, buffersToDelete, arraysToDelete, filename](QJsonDocument result, QByteArray data, QNetworkReply *r) {
|
||||
for (auto buffer : buffersToDelete) buffer->deleteLater();
|
||||
for (auto arr : arraysToDelete) delete arr;
|
||||
parseResult(result, data, returnPathspec, name());
|
||||
parseResult(r, result, data, returnPathspec, name(), filename);
|
||||
});
|
||||
}
|
||||
break;
|
||||
@ -367,14 +374,15 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
|
||||
switch (method) {
|
||||
case HttpMethod::POST:
|
||||
if (returnPathspec == "|") {
|
||||
ioutils::postData(target, h, data, filename, [&](QByteArray result, QNetworkReply *) {
|
||||
ioutils::postData(target, h, data, [&, filename](QByteArray result, QNetworkReply *r) {
|
||||
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
|
||||
QApplication::clipboard()->setText(QString::fromUtf8(result));
|
||||
playSuccessSound();
|
||||
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
|
||||
});
|
||||
} else {
|
||||
ioutils::postJson(target, h, data, filename, [&](QJsonDocument result, QByteArray data, QNetworkReply *) {
|
||||
parseResult(result, data, returnPathspec, name());
|
||||
ioutils::postJson(target, h, data, [&, filename](QJsonDocument result, QByteArray data, QNetworkReply *r) {
|
||||
parseResult(r, result, data, returnPathspec, name(), filename);
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <QJsonObject>
|
||||
#include <QMap>
|
||||
#include <QUrl>
|
||||
#include <QNetworkReply>
|
||||
|
||||
enum class HttpMethod { POST };
|
||||
|
||||
@ -32,7 +33,7 @@ private:
|
||||
bool base64 = false;
|
||||
QString returnPathspec;
|
||||
QString urlPrepend, urlAppend;
|
||||
void parseResult(QJsonDocument result, QByteArray data, QString returnPathspec, QString name);
|
||||
void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename);
|
||||
void playSuccessSound();
|
||||
void playErrorSound();
|
||||
};
|
||||
|
@ -45,7 +45,6 @@ void ImgurSettingsDialog::on_authorize_clicked() {
|
||||
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) {
|
||||
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
|
||||
ui->buttonBox->setEnabled(true);
|
||||
|
@ -26,15 +26,15 @@ struct SegfaultWorkaround { // I'm a scrub for doing this
|
||||
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) {
|
||||
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, "");
|
||||
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, "");
|
||||
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ struct SegfaultWorkaround { // I'm a scrub for doing this
|
||||
settings::settings().setValue("imgur/refresh", res["refresh_token"].toString());
|
||||
settings::settings().setValue("imgur/access", token);
|
||||
|
||||
dis->handleSend(token.prepend("Bearer "), mime, byteArray, "");
|
||||
dis->handleSend(token.prepend("Bearer "), mime, byteArray);
|
||||
QScopedPointer<SegfaultWorkaround>(this);
|
||||
});
|
||||
}
|
||||
@ -84,13 +84,14 @@ 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),
|
||||
byteArray, filename, [byteArray, this, mime](QJsonDocument res, QByteArray, QNetworkReply *r) {
|
||||
byteArray, [byteArray, this, mime, filename](QJsonDocument res, QByteArray data, QNetworkReply *r) {
|
||||
QString result = res.object()["data"].toObject()["link"].toString();
|
||||
if (r->error() == QNetworkReply::ContentAccessDenied) {
|
||||
new SegfaultWorkaround(byteArray, this, mime);
|
||||
return;
|
||||
}
|
||||
if (!result.isEmpty()) {
|
||||
ioutils::addLogEntry(r, data, result, filename);
|
||||
utils::toClipboard(result);
|
||||
notifications::notify(tr("KShare imgur Uploader"), tr("Uploaded to imgur!"));
|
||||
playSuccessSound();
|
||||
@ -104,6 +105,10 @@ void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray,
|
||||
});
|
||||
}
|
||||
|
||||
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
|
||||
handleSend(auth, mime, byteArray);
|
||||
}
|
||||
|
||||
void ImgurUploader::playSuccessSound() {
|
||||
QMediaPlayer* mediaPlayer = new QMediaPlayer(MainWindow::inst());
|
||||
mediaPlayer->setMedia(QUrl("qrc:/successsound.wav"));
|
||||
|
@ -20,6 +20,7 @@ public:
|
||||
|
||||
private:
|
||||
void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename);
|
||||
void handleSend(QString auth, QString mime, QByteArray byteArray);
|
||||
void playSuccessSound();
|
||||
void playErrorSound();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user