Implement %FORMAT and %format
This commit is contained in:
parent
9baadf63e9
commit
7f9db557f9
1
.gdb_history
Normal file
1
.gdb_history
Normal file
@ -0,0 +1 @@
|
|||||||
|
bt
|
@ -168,25 +168,6 @@ QList<QPair<QString, QString>> getHeaders(QJsonObject h, QString imageFormat, Re
|
|||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString contentType) {
|
|
||||||
QJsonObject o;
|
|
||||||
for (QString s : body.keys()) {
|
|
||||||
QJsonValue v = body[s];
|
|
||||||
if (v.isObject()) {
|
|
||||||
QJsonObject vo = v.toObject();
|
|
||||||
o.insert(s, recurseAndReplace(vo, data, contentType));
|
|
||||||
} else if (v.isString()) {
|
|
||||||
QString str = v.toString();
|
|
||||||
if (str.startsWith("/") && str.endsWith("/")) {
|
|
||||||
o.insert(s, str.mid(1, str.length() - 2).replace("%imagedata", data).replace("%contenttype", contentType));
|
|
||||||
} else
|
|
||||||
o.insert(s, v);
|
|
||||||
} else
|
|
||||||
o.insert(s, v);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString parsePathspec(QJsonDocument &response, QString &pathspec) {
|
QString parsePathspec(QJsonDocument &response, QString &pathspec) {
|
||||||
if (!pathspec.startsWith(".")) {
|
if (!pathspec.startsWith(".")) {
|
||||||
// Does not point to anything
|
// Does not point to anything
|
||||||
@ -243,12 +224,46 @@ void parseResult(QJsonDocument result, QByteArray data, QString returnPathspec,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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("/")) {
|
||||||
|
o.insert(s, substituteArgs(str.toUtf8(), format, data));
|
||||||
|
} else
|
||||||
|
o.insert(s, v);
|
||||||
|
} else
|
||||||
|
o.insert(s, v);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
||||||
auto h = getHeaders(headers, format, this->rFormat);
|
auto h = getHeaders(headers, format, this->rFormat);
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
if (base64) imgData = imgData.toBase64();
|
if (base64) imgData = imgData.toBase64();
|
||||||
QString mime = normalFormatMIME(normalFormatFromName(format));
|
|
||||||
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
|
|
||||||
|
|
||||||
switch (this->rFormat) {
|
switch (this->rFormat) {
|
||||||
case RequestFormat::PLAIN: {
|
case RequestFormat::PLAIN: {
|
||||||
@ -256,14 +271,10 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
} break;
|
} break;
|
||||||
case RequestFormat::JSON: {
|
case RequestFormat::JSON: {
|
||||||
if (body.isString()) {
|
if (body.isString()) {
|
||||||
QStringList split = body.toString().replace("%contenttype", mime).split("%imagedata");
|
data = substituteArgs(body.toString().toUtf8(), format, imgData);
|
||||||
for (int i = 0; i < split.size(); i++) {
|
|
||||||
data.append(split[i]);
|
|
||||||
if (i < split.size() - 1) data.append(imgData);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
QJsonObject vo = body.toObject();
|
QJsonObject vo = body.toObject();
|
||||||
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, mime).toVariantMap()).toJson();
|
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format).toVariantMap()).toJson();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
||||||
@ -271,17 +282,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
for (QString key : body.keys()) {
|
for (QString key : body.keys()) {
|
||||||
QJsonValue val = body[key];
|
QJsonValue val = body[key];
|
||||||
if (val.isString()) {
|
if (val.isString()) {
|
||||||
QString str = val.toString();
|
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, imgData));
|
||||||
QByteArray strB;
|
|
||||||
if (str.startsWith("/") && str.endsWith("/")) {
|
|
||||||
str = str.mid(1, str.length() - 2);
|
|
||||||
QStringList split = str.replace("%contenttype", mime).split("%imagedata");
|
|
||||||
for (int i = 0; i < split.size(); i++) {
|
|
||||||
strB.append(split[i]);
|
|
||||||
if (i < split.size() - 1) strB.append(imgData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data.append(QUrl::toPercentEncoding(key)).append('=').append(strB);
|
|
||||||
} else {
|
} else {
|
||||||
if (!data.isEmpty()) data.append('&');
|
if (!data.isEmpty()) data.append('&');
|
||||||
data.append(QUrl::toPercentEncoding(key))
|
data.append(QUrl::toPercentEncoding(key))
|
||||||
@ -300,17 +301,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
QHttpPart part;
|
QHttpPart part;
|
||||||
QJsonValue bd = valo["body"];
|
QJsonValue bd = valo["body"];
|
||||||
if (bd.isString()) {
|
if (bd.isString()) {
|
||||||
QString s = bd.toString();
|
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, imgData);
|
||||||
QByteArray body;
|
|
||||||
if (s.startsWith("/") && s.endsWith("/")) {
|
|
||||||
s = s.mid(1, s.length() - 1);
|
|
||||||
QStringList split = s.replace("%contenttype", mime).split("%imagedata");
|
|
||||||
for (int i = 0; i < split.size(); i++) {
|
|
||||||
body.append(split[i]);
|
|
||||||
if (i < split.size() - 1) body.append(imgData);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
body = s.toUtf8();
|
|
||||||
QByteArray *bodyHeap = new QByteArray;
|
QByteArray *bodyHeap = new QByteArray;
|
||||||
body.swap(*bodyHeap);
|
body.swap(*bodyHeap);
|
||||||
QBuffer *buffer = new QBuffer(bodyHeap);
|
QBuffer *buffer = new QBuffer(bodyHeap);
|
||||||
@ -320,17 +311,16 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
arraysToDelete.append(bodyHeap);
|
arraysToDelete.append(bodyHeap);
|
||||||
} else {
|
} else {
|
||||||
auto bdo = bd.toObject();
|
auto bdo = bd.toObject();
|
||||||
QJsonObject result = recurseAndReplace(bdo, imgData, mime);
|
QJsonObject result = recurseAndReplace(bdo, imgData, format);
|
||||||
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
|
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
|
||||||
}
|
}
|
||||||
QByteArray cdh("form-data");
|
QByteArray cdh("form-data");
|
||||||
for (QString headerVal : valo.keys()) {
|
for (QString headerVal : valo.keys()) {
|
||||||
if (headerVal.startsWith("__")) {
|
if (headerVal.startsWith("__")) {
|
||||||
headerVal = headerVal.mid(2);
|
headerVal = headerVal.mid(2);
|
||||||
QString str = valo[headerVal].toString();
|
QByteArray str = valo[headerVal].toString().toUtf8();
|
||||||
if (str.startsWith("/") && str.endsWith("/"))
|
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format);
|
||||||
str = str.mid(1, str.length() - 1).replace("%contenttype", mime);
|
part.setRawHeader(headerVal.toLatin1(), str;
|
||||||
part.setRawHeader(headerVal.toLatin1(), str.toLatin1());
|
|
||||||
} else if (headerVal != "body")
|
} else if (headerVal != "body")
|
||||||
cdh += "; " + headerVal + "=\"" + valo[headerVal].toString().replace("\"", "\\\"") + "\"";
|
cdh += "; " + headerVal + "=\"" + valo[headerVal].toString().replace("\"", "\\\"") + "\"";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user