feat(uploader): extend JSON selectors (#6193)
This commit is contained in:
@@ -42,6 +42,25 @@ QJsonArray NetworkResult::parseJsonArray() const
|
||||
return jsonDoc.array();
|
||||
}
|
||||
|
||||
QJsonValue NetworkResult::parseJsonValue() const
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
|
||||
return QJsonValue::fromJson(this->data_);
|
||||
#else
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(this->data_));
|
||||
if (jsonDoc.isArray())
|
||||
{
|
||||
return jsonDoc.array();
|
||||
}
|
||||
if (jsonDoc.isObject())
|
||||
{
|
||||
return jsonDoc.object();
|
||||
}
|
||||
|
||||
return {}; // undefined
|
||||
#endif
|
||||
}
|
||||
|
||||
rapidjson::Document NetworkResult::parseRapidJson() const
|
||||
{
|
||||
rapidjson::Document ret(rapidjson::kObjectType);
|
||||
|
||||
@@ -23,6 +23,13 @@ public:
|
||||
/// Parses the result as json and returns the root as an array.
|
||||
/// Returns empty object if parsing failed.
|
||||
QJsonArray parseJsonArray() const;
|
||||
|
||||
/// Parses the result as json and returns the root as a generic value.
|
||||
/// Returns empty object if parsing failed.
|
||||
///
|
||||
/// In Qt 6.9+, this will parse scalar values as well.
|
||||
QJsonValue parseJsonValue() const;
|
||||
|
||||
/// Parses the result as json and returns the document.
|
||||
rapidjson::Document parseRapidJson() const;
|
||||
const QByteArray &getData() const;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <QMimeData>
|
||||
#include <QMutex>
|
||||
#include <QString>
|
||||
#include <QStringView>
|
||||
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
@@ -14,6 +15,33 @@ class Channel;
|
||||
class NetworkResult;
|
||||
using ChannelPtr = std::shared_ptr<Channel>;
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
namespace chatterino::imageuploader::detail {
|
||||
|
||||
/// Traverses the JSON value with a pattern where each key is separated by dots.
|
||||
///
|
||||
/// If the pattern doesn't match, an empty string is returned.
|
||||
///
|
||||
/// **Example**:
|
||||
///
|
||||
/// - JSON: `{"foo": {"bar": [1, "baz"]}}`
|
||||
/// - pattern: `foo.bar.1`
|
||||
/// - return value: `"baz"`
|
||||
QString getJSONValue(QJsonValue responseJson, QStringView jsonPattern);
|
||||
|
||||
/// Interpolates `pattern` with the JSON response.
|
||||
/// **Example**:
|
||||
///
|
||||
/// - response: `{"foo": {"bar": [1, "baz", "qox"]}}`
|
||||
/// - pattern: `https://example.com/{foo.bar.1}.{foo.bar.2}`
|
||||
/// - return value: `"https://example.com/baz.qox"`
|
||||
QString getLinkFromResponse(const NetworkResult &response, QString pattern);
|
||||
|
||||
} // namespace chatterino::imageuploader::detail
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
struct RawImageData {
|
||||
QByteArray data;
|
||||
QString format;
|
||||
|
||||
Reference in New Issue
Block a user