Test it now Max

This commit is contained in:
ArsenArsen 2017-06-23 16:56:15 +02:00
parent db5e82cca8
commit 2929d48f98
3 changed files with 57 additions and 5 deletions

View File

@ -4,8 +4,37 @@
#include <QNetworkReply>
#include <QNetworkRequest>
namespace ioutils {
QNetworkAccessManager networkManager;
QNetworkAccessManager ioutils::networkManager;
void ioutils::postMultipart(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QByteArray data = reply->readAll();
callback(QJsonDocument::fromJson(data), data, reply);
delete reply;
});
}
void ioutils::postMultipartData(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
callback(reply->readAll(), reply);
delete reply;
});
}
void ioutils::getJson(QUrl target,

View File

@ -16,6 +16,14 @@ void postJson(QUrl target,
std::function<void(QJsonDocument, 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,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
void postMultipartData(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
std::function<void(QByteArray, QNetworkReply *)> callback);
}
#endif // IOUTILS_HPP

View File

@ -291,7 +291,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
}
} break;
case RequestFormat::MULTIPART_FORM_DATA: {
QHttpMultiPart multipart(QHttpMultiPart::FormDataType);
QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
auto arr = body.toArray();
for (QJsonValue val : arr) {
auto valo = val.toObject();
@ -311,12 +311,12 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
QBuffer *buffer = new QBuffer(&imgData);
buffer->open(QIODevice::ReadOnly);
part.setBodyDevice(buffer);
multipart.append(part);
multipart->append(part);
} else {
auto bdo = bd.toObject();
QJsonObject result = recurseAndReplace(bdo, imgData, mime);
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
multipart.append(part);
multipart->append(part);
}
for (QString headerVal : valo.keys()) {
QString str = valo[headerVal].toString();
@ -325,6 +325,21 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
part.setRawHeader(headerVal.toLatin1(), str.toLatin1());
}
}
switch (method) {
case HttpMethod::POST:
if (returnPathspec == "|") {
ioutils::postMultipartData(target, h, multipart, [&](QByteArray result, QNetworkReply *) {
QApplication::clipboard()->setText(QString::fromUtf8(result));
notifications::notify("KShare Custom Uploader " + name(), "Copied upload result to clipboard!");
});
} else {
ioutils::postMultipart(target, h, multipart, [&](QJsonDocument result, QByteArray data, QNetworkReply *) {
parseResult(result, data, returnPathspec, name());
});
}
break;
}
return;
} break;
}
if (limit > 0 && data.size() > limit) {