feat(uploader): extend JSON selectors (#6193)
This commit is contained in:
@@ -44,8 +44,57 @@ std::optional<QByteArray> convertToPng(const QImage &image)
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace chatterino::imageuploader::detail {
|
||||
|
||||
// extracting link to either image or its deletion from response body
|
||||
QString getJSONValue(QJsonValue responseJson, QStringView jsonPattern)
|
||||
{
|
||||
for (auto key : jsonPattern.tokenize(u'.'))
|
||||
{
|
||||
if (responseJson.isObject())
|
||||
{
|
||||
responseJson = responseJson[key];
|
||||
}
|
||||
else if (responseJson.isArray())
|
||||
{
|
||||
bool ok = false;
|
||||
auto idx = key.toLongLong(&ok);
|
||||
if (ok)
|
||||
{
|
||||
responseJson = responseJson[idx];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// we reached a scalar value, no need to continue
|
||||
break;
|
||||
}
|
||||
}
|
||||
return responseJson.toString();
|
||||
}
|
||||
|
||||
QString getLinkFromResponse(const NetworkResult &response, QString pattern)
|
||||
{
|
||||
QRegularExpression regExp("{(.+)}",
|
||||
QRegularExpression::InvertedGreedinessOption);
|
||||
auto match = regExp.match(pattern);
|
||||
|
||||
auto jsonRoot = response.parseJsonValue();
|
||||
while (match.hasMatch())
|
||||
{
|
||||
pattern.replace(match.captured(0),
|
||||
getJSONValue(jsonRoot, match.capturedView(1)));
|
||||
match = regExp.match(pattern);
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
} // namespace chatterino::imageuploader::detail
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
using namespace imageuploader::detail;
|
||||
|
||||
// logging information on successful uploads to a json file
|
||||
void ImageUploader::logToFile(const QString &originalFilePath,
|
||||
const QString &imageLink,
|
||||
@@ -97,31 +146,6 @@ void ImageUploader::logToFile(const QString &originalFilePath,
|
||||
logSaveFile.commit();
|
||||
}
|
||||
|
||||
// extracting link to either image or its deletion from response body
|
||||
QString getJSONValue(QJsonValue responseJson, QString jsonPattern)
|
||||
{
|
||||
for (const QString &key : jsonPattern.split("."))
|
||||
{
|
||||
responseJson = responseJson[key];
|
||||
}
|
||||
return responseJson.toString();
|
||||
}
|
||||
|
||||
QString getLinkFromResponse(NetworkResult response, QString pattern)
|
||||
{
|
||||
QRegularExpression regExp("{(.+)}",
|
||||
QRegularExpression::InvertedGreedinessOption);
|
||||
auto match = regExp.match(pattern);
|
||||
|
||||
while (match.hasMatch())
|
||||
{
|
||||
pattern.replace(match.captured(0),
|
||||
getJSONValue(response.parseJson(), match.captured(1)));
|
||||
match = regExp.match(pattern);
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
void ImageUploader::sendImageUploadRequest(RawImageData imageData,
|
||||
ChannelPtr channel,
|
||||
QPointer<ResizingTextEdit> textEdit)
|
||||
|
||||
Reference in New Issue
Block a user