2017-04-24 23:14:01 +02:00
|
|
|
#include "customuploader.hpp"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QFile>
|
2017-06-23 16:41:21 +02:00
|
|
|
#include <QHttpMultiPart>
|
|
|
|
#include <QJsonArray>
|
2017-04-24 23:14:01 +02:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QNetworkReply>
|
2017-06-06 17:05:34 +02:00
|
|
|
#include <formats.hpp>
|
2017-04-24 23:14:01 +02:00
|
|
|
#include <io/ioutils.hpp>
|
2017-04-25 22:17:36 +02:00
|
|
|
#include <notifications.hpp>
|
2017-04-24 23:14:01 +02:00
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
using formats::normalFormatFromName;
|
|
|
|
using formats::normalFormatMIME;
|
2017-06-13 01:15:57 +02:00
|
|
|
using formats::recordingFormatFromName;
|
|
|
|
using formats::recordingFormatMIME;
|
2017-04-24 23:14:01 +02:00
|
|
|
using std::runtime_error;
|
|
|
|
|
2017-07-29 17:22:17 +02:00
|
|
|
[[noreturn]] void error(QString absFilePath, QString err) {
|
|
|
|
throw runtime_error((QObject::tr("Invalid file: ").append(absFilePath) + ": " + err).toStdString());
|
2017-04-25 16:04:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
CustomUploader::CustomUploader(QString absFilePath) {
|
2017-05-09 17:26:00 +02:00
|
|
|
// Let's go
|
|
|
|
QFile file(absFilePath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) error(absFilePath, file.errorString());
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
|
|
|
if (!doc.isObject()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("Root not an object"));
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
if (!obj["name"].isString())
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("name is not a string"));
|
2017-04-24 23:14:01 +02:00
|
|
|
else
|
2017-05-09 17:26:00 +02:00
|
|
|
uName = obj["name"].toString();
|
2017-07-20 00:43:07 +02:00
|
|
|
if (obj.contains("desc")) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (!obj["desc"].isString())
|
2017-07-29 17:22:17 +02:00
|
|
|
/*t*/ error(absFilePath, tr("desc not a string"));
|
2017-05-09 17:26:00 +02:00
|
|
|
else
|
2017-05-24 16:51:14 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
desc = obj["desc"].toString();
|
|
|
|
} else
|
|
|
|
desc = absFilePath;
|
|
|
|
QJsonValue m = obj["method"];
|
|
|
|
if (!m.isUndefined() && !m.isNull()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
if (!m.isString()) error(absFilePath, tr("method not a string"));
|
2017-05-09 17:26:00 +02:00
|
|
|
QString toCheck = m.toString().toLower();
|
|
|
|
if (toCheck == "post")
|
|
|
|
method = HttpMethod::POST;
|
|
|
|
else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("method invalid"));
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
|
|
|
QJsonValue url = obj["target"];
|
|
|
|
if (!url.isString()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("target missing"));
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
|
|
|
QUrl target(url.toString());
|
2017-07-29 17:22:17 +02:00
|
|
|
if (!target.isValid()) error(absFilePath, tr("target not URL"));
|
2017-05-09 17:26:00 +02:00
|
|
|
this->target = target;
|
|
|
|
QJsonValue formatValue = obj["format"];
|
|
|
|
if (!formatValue.isUndefined() && !formatValue.isNull()) {
|
|
|
|
if (formatValue.isString()) {
|
|
|
|
QString formatString = formatValue.toString().toLower();
|
|
|
|
if (formatString == "x-www-form-urlencoded")
|
2017-06-04 22:58:29 +02:00
|
|
|
rFormat = RequestFormat::X_WWW_FORM_URLENCODED;
|
2017-05-09 17:26:00 +02:00
|
|
|
else if (formatString == "json")
|
2017-06-04 22:58:29 +02:00
|
|
|
rFormat = RequestFormat::JSON;
|
2017-05-09 17:26:00 +02:00
|
|
|
else if (formatString == "plain")
|
2017-06-04 22:58:29 +02:00
|
|
|
rFormat = RequestFormat::PLAIN;
|
2017-06-23 16:41:21 +02:00
|
|
|
else if (formatString == "multipart-form-data")
|
|
|
|
rFormat = RequestFormat::MULTIPART_FORM_DATA;
|
2017-05-09 17:26:00 +02:00
|
|
|
else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("format invalid"));
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
|
|
|
} else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("format provided but not string"));
|
2017-05-09 17:26:00 +02:00
|
|
|
QJsonValue bodyValue = obj["body"];
|
2017-06-04 22:58:29 +02:00
|
|
|
if (rFormat != RequestFormat::PLAIN) {
|
2017-07-29 17:22:17 +02:00
|
|
|
if (bodyValue.isUndefined()) error(absFilePath, tr("body not set"));
|
2017-06-23 16:41:21 +02:00
|
|
|
if (rFormat == RequestFormat::MULTIPART_FORM_DATA) {
|
|
|
|
if (bodyValue.isArray()) {
|
|
|
|
for (QJsonValue val : bodyValue.toArray()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
if (!val.isObject()) error(absFilePath, tr("all elements of body must be objects"));
|
2017-06-23 16:41:21 +02:00
|
|
|
if (!val.toObject()["body"].isObject() && !val.toObject().value("body").isString())
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("all parts must have a body which is object or string!"));
|
2017-06-23 16:41:21 +02:00
|
|
|
QJsonObject vo = val.toObject();
|
|
|
|
for (auto v : vo["body"].toObject())
|
|
|
|
if (!v.isObject() && !v.isString())
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("all parts of body must be string or object"));
|
2017-06-23 16:41:21 +02:00
|
|
|
for (auto v : vo.keys())
|
|
|
|
if (v.startsWith("__") && !vo[v].isString())
|
2017-07-29 17:22:17 +02:00
|
|
|
//: __<whatever is the word for header>
|
|
|
|
error(absFilePath, tr("all __headers must be strings"));
|
2017-06-23 16:41:21 +02:00
|
|
|
}
|
|
|
|
body = bodyValue;
|
|
|
|
} else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("body not array (needed for multipart)"));
|
2017-06-23 16:41:21 +02:00
|
|
|
} else {
|
|
|
|
if (bodyValue.isObject())
|
|
|
|
body = bodyValue;
|
|
|
|
else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("body not object"));
|
2017-06-23 16:41:21 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
} else {
|
|
|
|
if (bodyValue.isString()) {
|
|
|
|
body = bodyValue;
|
|
|
|
} else
|
2017-07-29 17:22:17 +02:00
|
|
|
//: `format: PLAIN` should stay the same
|
|
|
|
error(absFilePath, tr("body not string (reason: format: PLAIN)"));
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
|
|
|
QJsonValue headerVal = obj["headers"];
|
|
|
|
if (!(headerVal.isUndefined() || headerVal.isNull())) {
|
2017-07-29 17:22:17 +02:00
|
|
|
if (!headerVal.isObject()) error(absFilePath, tr("headers must be object"));
|
2017-05-09 17:26:00 +02:00
|
|
|
headers = headerVal.toObject();
|
|
|
|
} else
|
|
|
|
headers = QJsonObject();
|
|
|
|
QJsonValue returnPsVal = obj["return"];
|
|
|
|
if (returnPsVal.isString()) {
|
|
|
|
returnPathspec = returnPsVal.toString();
|
|
|
|
} else
|
2017-07-29 17:22:17 +02:00
|
|
|
error(absFilePath, tr("return invalid"));
|
2017-05-17 22:59:33 +02:00
|
|
|
QJsonValue fileLimit = obj["fileLimit"];
|
|
|
|
if (!fileLimit.isNull() && !fileLimit.isUndefined()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
//: fileLimit stays English
|
|
|
|
if (!fileLimit.isDouble()) error(absFilePath, tr("fileLimit not decimal"));
|
2017-05-17 22:59:33 +02:00
|
|
|
limit = fileLimit.toDouble();
|
|
|
|
}
|
2017-06-06 17:05:34 +02:00
|
|
|
QJsonValue bool64 = obj["base64"];
|
|
|
|
if (!bool64.isNull() && !bool64.isUndefined()) {
|
2017-07-29 17:22:17 +02:00
|
|
|
if (!bool64.isBool()) error(absFilePath, tr("base64 must be boolean"));
|
2017-06-06 17:05:34 +02:00
|
|
|
base64 = bool64.toBool();
|
2017-07-29 17:22:17 +02:00
|
|
|
if (rFormat == RequestFormat::JSON && !base64) error(absFilePath, tr("base64 required with json"));
|
2017-06-06 17:05:34 +02:00
|
|
|
}
|
2017-07-20 00:40:39 +02:00
|
|
|
urlPrepend = obj["return_prepend"].toString();
|
|
|
|
urlAppend = obj["return_append"].toString();
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QString CustomUploader::name() {
|
|
|
|
return uName;
|
|
|
|
}
|
2017-04-24 23:14:01 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QString CustomUploader::description() {
|
|
|
|
return desc;
|
|
|
|
}
|
2017-04-24 23:14:01 +02:00
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
QString getCType(RequestFormat format, QString plainType) {
|
2017-05-09 17:26:00 +02:00
|
|
|
switch (format) {
|
|
|
|
case RequestFormat::X_WWW_FORM_URLENCODED:
|
|
|
|
return "application/x-www-form-urlencoded";
|
|
|
|
case RequestFormat::JSON:
|
|
|
|
return "application/json";
|
2017-06-23 16:41:21 +02:00
|
|
|
case RequestFormat::MULTIPART_FORM_DATA:
|
|
|
|
return "multipart/form-data";
|
2017-05-09 17:26:00 +02:00
|
|
|
case RequestFormat::PLAIN:
|
|
|
|
return plainType;
|
|
|
|
}
|
2017-04-24 23:14:01 +02:00
|
|
|
return plainType;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
QList<QPair<QString, QString>> getHeaders(QJsonObject h, QString imageFormat, RequestFormat format) {
|
2017-05-09 17:26:00 +02:00
|
|
|
QList<QPair<QString, QString>> headers;
|
|
|
|
for (QString s : h.keys()) {
|
|
|
|
if (s.toLower() == "content-type") continue;
|
|
|
|
QJsonValue v = h[s];
|
|
|
|
if (!v.isString())
|
|
|
|
headers << QPair<QString, QString>(s, QJsonDocument::fromVariant(v.toVariant()).toJson());
|
|
|
|
else
|
|
|
|
headers << QPair<QString, QString>(s, v.toString());
|
|
|
|
}
|
2017-06-06 17:05:34 +02:00
|
|
|
headers << QPair<QString, QString>("Content-Type", getCType(format, normalFormatMIME(normalFormatFromName(imageFormat))));
|
2017-05-09 17:26:00 +02:00
|
|
|
return headers;
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
QString parsePathspec(QJsonDocument &response, QString &pathspec) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (!pathspec.startsWith(".")) {
|
|
|
|
// Does not point to anything
|
2017-05-06 13:21:12 +02:00
|
|
|
return "";
|
2017-05-09 17:26:00 +02:00
|
|
|
} else {
|
|
|
|
if (!response.isObject()) return "";
|
|
|
|
QStringList fields = pathspec.right(pathspec.length() - 1).split('.', QString::SkipEmptyParts);
|
|
|
|
QJsonObject o = response.object();
|
|
|
|
if (pathspec == ".") {
|
|
|
|
return QString::fromUtf8(response.toJson());
|
|
|
|
}
|
|
|
|
QJsonValue val = o[fields.at(0)];
|
|
|
|
for (int i = 1; i < fields.size(); i++) {
|
|
|
|
if (val.isUndefined() || val.isNull())
|
|
|
|
return "";
|
|
|
|
else if (val.isString())
|
|
|
|
return val.toString();
|
2017-07-20 00:40:39 +02:00
|
|
|
else if (val.isArray())
|
|
|
|
val = val.toArray()[fields.at(i).toInt()];
|
2017-05-09 17:26:00 +02:00
|
|
|
else if (!val.isObject())
|
|
|
|
return QString::fromUtf8(QJsonDocument::fromVariant(val.toVariant()).toJson());
|
|
|
|
else
|
|
|
|
val = val.toObject()[fields.at(i)];
|
|
|
|
}
|
|
|
|
if (val.isUndefined() || val.isNull())
|
|
|
|
return "";
|
|
|
|
else if (val.isString())
|
|
|
|
return val.toString();
|
|
|
|
else if (!val.isObject())
|
|
|
|
return QString::fromUtf8(QJsonDocument::fromVariant(val.toVariant()).toJson());
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
return "";
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 17:22:17 +02:00
|
|
|
void CustomUploader::parseResult(QJsonDocument result, QByteArray data, QString returnPathspec, QString name) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (result.isObject()) {
|
2017-07-20 00:40:39 +02:00
|
|
|
QString url = urlPrepend + parsePathspec(result, returnPathspec) + urlAppend;
|
2017-05-09 17:26:00 +02:00
|
|
|
if (!url.isEmpty()) {
|
|
|
|
QApplication::clipboard()->setText(url);
|
2017-07-29 17:22:17 +02:00
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!"));
|
2017-06-13 18:58:24 +02:00
|
|
|
} else {
|
2017-07-29 17:22:17 +02:00
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Upload done, but result empty!"));
|
2017-06-13 18:58:24 +02:00
|
|
|
QApplication::clipboard()->setText(data);
|
|
|
|
}
|
2017-05-13 22:47:01 +02:00
|
|
|
} else {
|
2017-07-29 17:22:17 +02:00
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name,
|
|
|
|
tr("Upload done, but result is not JSON Object! Result in clipboard."));
|
2017-05-13 22:47:01 +02:00
|
|
|
QApplication::clipboard()->setText(data);
|
|
|
|
}
|
2017-04-25 22:17:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-16 19:26:41 +02:00
|
|
|
QByteArray substituteArgs(QByteArray arr, QString format, QByteArray imgData = QByteArray()) {
|
|
|
|
QString mime = normalFormatMIME(normalFormatFromName(format));
|
|
|
|
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
|
|
|
|
if (arr.startsWith("/") && arr.endsWith("/")) {
|
|
|
|
arr = arr.mid(1, arr.length() - 2);
|
|
|
|
|
|
|
|
arr = arr.replace("%contenttype", mime.toUtf8());
|
|
|
|
arr = arr.replace("%FORMAT", format.toUpper().toUtf8());
|
|
|
|
arr = arr.replace("%format", format.toLower().toUtf8());
|
|
|
|
|
|
|
|
if (imgData.isNull()) return arr;
|
|
|
|
return arr.replace("%imagedata", imgData);
|
|
|
|
} else
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString format) {
|
|
|
|
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));
|
|
|
|
} else if (v.isString()) {
|
|
|
|
QString str = v.toString();
|
|
|
|
if (str.startsWith("/") && str.endsWith("/")) {
|
2017-07-16 19:50:28 +02:00
|
|
|
o.insert(s, QString::fromUtf8(substituteArgs(str.toUtf8(), format, data)));
|
2017-07-16 19:26:41 +02:00
|
|
|
} else
|
|
|
|
o.insert(s, v);
|
|
|
|
} else
|
|
|
|
o.insert(s, v);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|
|
|
auto h = getHeaders(headers, format, this->rFormat);
|
2017-05-09 17:26:00 +02:00
|
|
|
QByteArray data;
|
2017-06-06 17:05:34 +02:00
|
|
|
if (base64) imgData = imgData.toBase64();
|
2017-06-13 01:15:57 +02:00
|
|
|
|
2017-06-04 22:58:29 +02:00
|
|
|
switch (this->rFormat) {
|
2017-05-09 17:26:00 +02:00
|
|
|
case RequestFormat::PLAIN: {
|
|
|
|
data = imgData;
|
|
|
|
} break;
|
|
|
|
case RequestFormat::JSON: {
|
|
|
|
if (body.isString()) {
|
2017-07-16 19:26:41 +02:00
|
|
|
data = substituteArgs(body.toString().toUtf8(), format, imgData);
|
2017-05-09 17:26:00 +02:00
|
|
|
} else {
|
|
|
|
QJsonObject vo = body.toObject();
|
2017-07-16 19:26:41 +02:00
|
|
|
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format).toVariantMap()).toJson();
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
} break;
|
|
|
|
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
|
|
|
QJsonObject body = this->body.toObject();
|
|
|
|
for (QString key : body.keys()) {
|
|
|
|
QJsonValue val = body[key];
|
|
|
|
if (val.isString()) {
|
2017-07-16 19:26:41 +02:00
|
|
|
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, imgData));
|
2017-05-09 17:26:00 +02:00
|
|
|
} else {
|
|
|
|
if (!data.isEmpty()) data.append('&');
|
|
|
|
data.append(QUrl::toPercentEncoding(key))
|
|
|
|
.append('=')
|
|
|
|
.append(QUrl::toPercentEncoding(QJsonDocument::fromVariant(body[key].toVariant()).toJson()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
2017-06-23 16:41:21 +02:00
|
|
|
case RequestFormat::MULTIPART_FORM_DATA: {
|
2017-06-23 16:56:15 +02:00
|
|
|
QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
2017-06-23 16:41:21 +02:00
|
|
|
auto arr = body.toArray();
|
2017-07-14 18:49:00 +02:00
|
|
|
QList<QBuffer *> buffersToDelete;
|
|
|
|
QList<QByteArray *> arraysToDelete;
|
2017-06-23 16:41:21 +02:00
|
|
|
for (QJsonValue val : arr) {
|
|
|
|
auto valo = val.toObject();
|
|
|
|
QHttpPart part;
|
|
|
|
QJsonValue bd = valo["body"];
|
|
|
|
if (bd.isString()) {
|
2017-07-16 19:26:41 +02:00
|
|
|
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, imgData);
|
2017-07-12 22:59:26 +02:00
|
|
|
QByteArray *bodyHeap = new QByteArray;
|
|
|
|
body.swap(*bodyHeap);
|
|
|
|
QBuffer *buffer = new QBuffer(bodyHeap);
|
2017-06-23 16:41:21 +02:00
|
|
|
buffer->open(QIODevice::ReadOnly);
|
|
|
|
part.setBodyDevice(buffer);
|
2017-07-14 18:49:00 +02:00
|
|
|
buffersToDelete.append(buffer);
|
|
|
|
arraysToDelete.append(bodyHeap);
|
2017-06-23 16:41:21 +02:00
|
|
|
} else {
|
|
|
|
auto bdo = bd.toObject();
|
2017-07-16 19:26:41 +02:00
|
|
|
QJsonObject result = recurseAndReplace(bdo, imgData, format);
|
2017-06-23 16:41:21 +02:00
|
|
|
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
|
|
|
|
}
|
2017-07-12 20:12:17 +02:00
|
|
|
QByteArray cdh("form-data");
|
2017-06-23 16:41:21 +02:00
|
|
|
for (QString headerVal : valo.keys()) {
|
2017-06-24 19:58:42 +02:00
|
|
|
if (headerVal.startsWith("__")) {
|
|
|
|
headerVal = headerVal.mid(2);
|
2017-07-20 18:04:20 +02:00
|
|
|
QByteArray str = valo["__" + headerVal].toString().toUtf8();
|
2017-07-16 19:26:41 +02:00
|
|
|
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format);
|
2017-07-16 19:50:28 +02:00
|
|
|
part.setRawHeader(headerVal.toLatin1(), str);
|
2017-07-14 18:49:00 +02:00
|
|
|
} else if (headerVal != "body")
|
2017-07-20 01:03:56 +02:00
|
|
|
cdh += "; " + headerVal + "=\""
|
|
|
|
+ substituteArgs(valo[headerVal].toString().toUtf8(), format).replace("\"", "\\\"") + "\"";
|
2017-06-23 16:41:21 +02:00
|
|
|
}
|
2017-07-12 20:24:16 +02:00
|
|
|
part.setHeader(QNetworkRequest::ContentDispositionHeader, cdh);
|
2017-07-14 18:49:00 +02:00
|
|
|
multipart->append(part);
|
2017-06-23 16:41:21 +02:00
|
|
|
}
|
2017-06-23 16:56:15 +02:00
|
|
|
switch (method) {
|
|
|
|
case HttpMethod::POST:
|
|
|
|
if (returnPathspec == "|") {
|
2017-07-29 17:22:17 +02:00
|
|
|
ioutils::postMultipartData(target, h, multipart,
|
|
|
|
[&, buffersToDelete, arraysToDelete](QByteArray result, QNetworkReply *) {
|
|
|
|
QApplication::clipboard()->setText(QString::fromUtf8(result));
|
|
|
|
for (auto buffer : buffersToDelete) buffer->deleteLater();
|
|
|
|
for (auto arr : arraysToDelete) delete arr;
|
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name(),
|
|
|
|
tr("Copied upload result to clipboard!"));
|
|
|
|
});
|
2017-06-23 16:56:15 +02:00
|
|
|
} else {
|
2017-07-14 18:49:00 +02:00
|
|
|
ioutils::postMultipart(target, h, multipart,
|
|
|
|
[&, buffersToDelete, arraysToDelete](QJsonDocument result, QByteArray data, QNetworkReply *) {
|
|
|
|
for (auto buffer : buffersToDelete) buffer->deleteLater();
|
|
|
|
for (auto arr : arraysToDelete) delete arr;
|
2017-07-29 17:22:17 +02:00
|
|
|
parseResult(result, data, returnPathspec, name());
|
2017-07-14 18:49:00 +02:00
|
|
|
});
|
2017-06-23 16:56:15 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
2017-07-20 00:40:39 +02:00
|
|
|
}
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
2017-05-21 11:16:00 +02:00
|
|
|
if (limit > 0 && data.size() > limit) {
|
2017-07-29 17:22:17 +02:00
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("File limit exceeded!"));
|
2017-05-17 22:59:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
switch (method) {
|
|
|
|
case HttpMethod::POST:
|
|
|
|
if (returnPathspec == "|") {
|
|
|
|
ioutils::postData(target, h, data, [&](QByteArray result, QNetworkReply *) {
|
|
|
|
QApplication::clipboard()->setText(QString::fromUtf8(result));
|
2017-07-29 17:22:17 +02:00
|
|
|
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
|
2017-05-09 17:26:00 +02:00
|
|
|
});
|
|
|
|
} else {
|
2017-05-13 22:47:01 +02:00
|
|
|
ioutils::postJson(target, h, data, [&](QJsonDocument result, QByteArray data, QNetworkReply *) {
|
2017-07-29 17:22:17 +02:00
|
|
|
parseResult(result, data, returnPathspec, name());
|
2017-05-13 22:39:05 +02:00
|
|
|
});
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
|
|
|
}
|