diff options
author | Andres Salomon <dilinger@queued.net> | 2021-01-18 04:00:14 -0500 |
---|---|---|
committer | Andres Salomon <dilinger@queued.net> | 2021-01-18 04:00:14 -0500 |
commit | 09eb39236666e81d5da014acea011dcd74d0999b (patch) | |
tree | 52876d96be71be1a39d5d935c1295a51995e8949 /lib/csapi/content-repo.cpp | |
parent | f1788ee27f33e9339334e0d79bde9a27d9ce2e44 (diff) | |
parent | a4e78956f105875625b572d8b98459ffa86fafe5 (diff) | |
download | libquotient-09eb39236666e81d5da014acea011dcd74d0999b.tar.gz libquotient-09eb39236666e81d5da014acea011dcd74d0999b.zip |
Update upstream source from tag 'upstream/0.6.4'
Update to upstream version '0.6.4'
with Debian dir aa8705fd74743e79c043bc9e3e425d5064404cfe
Diffstat (limited to 'lib/csapi/content-repo.cpp')
-rw-r--r-- | lib/csapi/content-repo.cpp | 310 |
1 files changed, 75 insertions, 235 deletions
diff --git a/lib/csapi/content-repo.cpp b/lib/csapi/content-repo.cpp index 22223985..7ae89739 100644 --- a/lib/csapi/content-repo.cpp +++ b/lib/csapi/content-repo.cpp @@ -4,184 +4,89 @@ #include "content-repo.h" -#include "converters.h" - -#include <QtNetwork/QNetworkReply> #include <QtCore/QStringBuilder> -using namespace QMatrixClient; - -static const auto basePath = QStringLiteral("/_matrix/media/r0"); - -class UploadContentJob::Private -{ - public: - QString contentUri; -}; +using namespace Quotient; -BaseJob::Query queryToUploadContent(const QString& filename) +auto queryToUploadContent(const QString& filename) { BaseJob::Query _q; addParam<IfNotEmpty>(_q, QStringLiteral("filename"), filename); return _q; } -static const auto UploadContentJobName = QStringLiteral("UploadContentJob"); - -UploadContentJob::UploadContentJob(QIODevice* content, const QString& filename, const QString& contentType) - : BaseJob(HttpVerb::Post, UploadContentJobName, - basePath % "/upload", - queryToUploadContent(filename)) - , d(new Private) +UploadContentJob::UploadContentJob(QIODevice* content, const QString& filename, + const QString& contentType) + : BaseJob(HttpVerb::Post, QStringLiteral("UploadContentJob"), + QStringLiteral("/_matrix/media/r0") % "/upload", + queryToUploadContent(filename)) { setRequestHeader("Content-Type", contentType.toLatin1()); - setRequestData(Data(content)); + addExpectedKey("content_uri"); } -UploadContentJob::~UploadContentJob() = default; - -const QString& UploadContentJob::contentUri() const -{ - return d->contentUri; -} - -BaseJob::Status UploadContentJob::parseJson(const QJsonDocument& data) -{ - auto json = data.object(); - if (!json.contains("content_uri"_ls)) - return { JsonParseError, - "The key 'content_uri' not found in the response" }; - fromJson(json.value("content_uri"_ls), d->contentUri); - return Success; -} - -class GetContentJob::Private -{ - public: - QString contentType; - QString contentDisposition; - QIODevice* data; -}; - -BaseJob::Query queryToGetContent(bool allowRemote) +auto queryToGetContent(bool allowRemote) { BaseJob::Query _q; addParam<IfNotEmpty>(_q, QStringLiteral("allow_remote"), allowRemote); return _q; } -QUrl GetContentJob::makeRequestUrl(QUrl baseUrl, const QString& serverName, const QString& mediaId, bool allowRemote) +QUrl GetContentJob::makeRequestUrl(QUrl baseUrl, const QString& serverName, + const QString& mediaId, bool allowRemote) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/download/" % serverName % "/" % mediaId, - queryToGetContent(allowRemote)); + QStringLiteral("/_matrix/media/r0") + % "/download/" % serverName % "/" + % mediaId, + queryToGetContent(allowRemote)); } -static const auto GetContentJobName = QStringLiteral("GetContentJob"); - -GetContentJob::GetContentJob(const QString& serverName, const QString& mediaId, bool allowRemote) - : BaseJob(HttpVerb::Get, GetContentJobName, - basePath % "/download/" % serverName % "/" % mediaId, - queryToGetContent(allowRemote), - {}, false) - , d(new Private) +GetContentJob::GetContentJob(const QString& serverName, const QString& mediaId, + bool allowRemote) + : BaseJob(HttpVerb::Get, QStringLiteral("GetContentJob"), + QStringLiteral("/_matrix/media/r0") % "/download/" % serverName + % "/" % mediaId, + queryToGetContent(allowRemote), {}, false) { setExpectedContentTypes({ "*/*" }); } -GetContentJob::~GetContentJob() = default; - -const QString& GetContentJob::contentType() const -{ - return d->contentType; -} - -const QString& GetContentJob::contentDisposition() const -{ - return d->contentDisposition; -} - -QIODevice* GetContentJob::data() const -{ - return d->data; -} - -BaseJob::Status GetContentJob::parseReply(QNetworkReply* reply) -{ - d->contentType = reply->rawHeader("Content-Type"); - d->contentDisposition = reply->rawHeader("Content-Disposition"); - d->data = reply; - return Success; -} - -class GetContentOverrideNameJob::Private -{ - public: - QString contentType; - QString contentDisposition; - QIODevice* data; -}; - -BaseJob::Query queryToGetContentOverrideName(bool allowRemote) +auto queryToGetContentOverrideName(bool allowRemote) { BaseJob::Query _q; addParam<IfNotEmpty>(_q, QStringLiteral("allow_remote"), allowRemote); return _q; } -QUrl GetContentOverrideNameJob::makeRequestUrl(QUrl baseUrl, const QString& serverName, const QString& mediaId, const QString& fileName, bool allowRemote) +QUrl GetContentOverrideNameJob::makeRequestUrl(QUrl baseUrl, + const QString& serverName, + const QString& mediaId, + const QString& fileName, + bool allowRemote) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/download/" % serverName % "/" % mediaId % "/" % fileName, - queryToGetContentOverrideName(allowRemote)); + QStringLiteral("/_matrix/media/r0") + % "/download/" % serverName % "/" + % mediaId % "/" % fileName, + queryToGetContentOverrideName(allowRemote)); } -static const auto GetContentOverrideNameJobName = QStringLiteral("GetContentOverrideNameJob"); - -GetContentOverrideNameJob::GetContentOverrideNameJob(const QString& serverName, const QString& mediaId, const QString& fileName, bool allowRemote) - : BaseJob(HttpVerb::Get, GetContentOverrideNameJobName, - basePath % "/download/" % serverName % "/" % mediaId % "/" % fileName, - queryToGetContentOverrideName(allowRemote), - {}, false) - , d(new Private) +GetContentOverrideNameJob::GetContentOverrideNameJob(const QString& serverName, + const QString& mediaId, + const QString& fileName, + bool allowRemote) + : BaseJob(HttpVerb::Get, QStringLiteral("GetContentOverrideNameJob"), + QStringLiteral("/_matrix/media/r0") % "/download/" % serverName + % "/" % mediaId % "/" % fileName, + queryToGetContentOverrideName(allowRemote), {}, false) { setExpectedContentTypes({ "*/*" }); } -GetContentOverrideNameJob::~GetContentOverrideNameJob() = default; - -const QString& GetContentOverrideNameJob::contentType() const -{ - return d->contentType; -} - -const QString& GetContentOverrideNameJob::contentDisposition() const -{ - return d->contentDisposition; -} - -QIODevice* GetContentOverrideNameJob::data() const -{ - return d->data; -} - -BaseJob::Status GetContentOverrideNameJob::parseReply(QNetworkReply* reply) -{ - d->contentType = reply->rawHeader("Content-Type"); - d->contentDisposition = reply->rawHeader("Content-Disposition"); - d->data = reply; - return Success; -} - -class GetContentThumbnailJob::Private -{ - public: - QString contentType; - QIODevice* data; -}; - -BaseJob::Query queryToGetContentThumbnail(int width, int height, const QString& method, bool allowRemote) +auto queryToGetContentThumbnail(int width, int height, const QString& method, + bool allowRemote) { BaseJob::Query _q; addParam<>(_q, QStringLiteral("width"), width); @@ -191,52 +96,33 @@ BaseJob::Query queryToGetContentThumbnail(int width, int height, const QString& return _q; } -QUrl GetContentThumbnailJob::makeRequestUrl(QUrl baseUrl, const QString& serverName, const QString& mediaId, int width, int height, const QString& method, bool allowRemote) +QUrl GetContentThumbnailJob::makeRequestUrl(QUrl baseUrl, + const QString& serverName, + const QString& mediaId, int width, + int height, const QString& method, + bool allowRemote) { - return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/thumbnail/" % serverName % "/" % mediaId, - queryToGetContentThumbnail(width, height, method, allowRemote)); + return BaseJob::makeRequestUrl( + std::move(baseUrl), + QStringLiteral("/_matrix/media/r0") % "/thumbnail/" % serverName % "/" + % mediaId, + queryToGetContentThumbnail(width, height, method, allowRemote)); } -static const auto GetContentThumbnailJobName = QStringLiteral("GetContentThumbnailJob"); - -GetContentThumbnailJob::GetContentThumbnailJob(const QString& serverName, const QString& mediaId, int width, int height, const QString& method, bool allowRemote) - : BaseJob(HttpVerb::Get, GetContentThumbnailJobName, - basePath % "/thumbnail/" % serverName % "/" % mediaId, - queryToGetContentThumbnail(width, height, method, allowRemote), - {}, false) - , d(new Private) +GetContentThumbnailJob::GetContentThumbnailJob(const QString& serverName, + const QString& mediaId, int width, + int height, const QString& method, + bool allowRemote) + : BaseJob(HttpVerb::Get, QStringLiteral("GetContentThumbnailJob"), + QStringLiteral("/_matrix/media/r0") % "/thumbnail/" % serverName + % "/" % mediaId, + queryToGetContentThumbnail(width, height, method, allowRemote), + {}, false) { setExpectedContentTypes({ "image/jpeg", "image/png" }); } -GetContentThumbnailJob::~GetContentThumbnailJob() = default; - -const QString& GetContentThumbnailJob::contentType() const -{ - return d->contentType; -} - -QIODevice* GetContentThumbnailJob::data() const -{ - return d->data; -} - -BaseJob::Status GetContentThumbnailJob::parseReply(QNetworkReply* reply) -{ - d->contentType = reply->rawHeader("Content-Type"); - d->data = reply; - return Success; -} - -class GetUrlPreviewJob::Private -{ - public: - Omittable<qint64> matrixImageSize; - QString ogImage; -}; - -BaseJob::Query queryToGetUrlPreview(const QString& url, Omittable<qint64> ts) +auto queryToGetUrlPreview(const QString& url, Omittable<qint64> ts) { BaseJob::Query _q; addParam<>(_q, QStringLiteral("url"), url); @@ -244,75 +130,29 @@ BaseJob::Query queryToGetUrlPreview(const QString& url, Omittable<qint64> ts) return _q; } -QUrl GetUrlPreviewJob::makeRequestUrl(QUrl baseUrl, const QString& url, Omittable<qint64> ts) +QUrl GetUrlPreviewJob::makeRequestUrl(QUrl baseUrl, const QString& url, + Omittable<qint64> ts) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/preview_url", - queryToGetUrlPreview(url, ts)); + QStringLiteral("/_matrix/media/r0") + % "/preview_url", + queryToGetUrlPreview(url, ts)); } -static const auto GetUrlPreviewJobName = QStringLiteral("GetUrlPreviewJob"); - GetUrlPreviewJob::GetUrlPreviewJob(const QString& url, Omittable<qint64> ts) - : BaseJob(HttpVerb::Get, GetUrlPreviewJobName, - basePath % "/preview_url", - queryToGetUrlPreview(url, ts)) - , d(new Private) -{ -} - -GetUrlPreviewJob::~GetUrlPreviewJob() = default; - -Omittable<qint64> GetUrlPreviewJob::matrixImageSize() const -{ - return d->matrixImageSize; -} - -const QString& GetUrlPreviewJob::ogImage() const -{ - return d->ogImage; -} - -BaseJob::Status GetUrlPreviewJob::parseJson(const QJsonDocument& data) -{ - auto json = data.object(); - fromJson(json.value("matrix:image:size"_ls), d->matrixImageSize); - fromJson(json.value("og:image"_ls), d->ogImage); - return Success; -} - -class GetConfigJob::Private -{ - public: - Omittable<qint64> uploadSize; -}; + : BaseJob(HttpVerb::Get, QStringLiteral("GetUrlPreviewJob"), + QStringLiteral("/_matrix/media/r0") % "/preview_url", + queryToGetUrlPreview(url, ts)) +{} QUrl GetConfigJob::makeRequestUrl(QUrl baseUrl) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/config"); + QStringLiteral("/_matrix/media/r0") + % "/config"); } -static const auto GetConfigJobName = QStringLiteral("GetConfigJob"); - GetConfigJob::GetConfigJob() - : BaseJob(HttpVerb::Get, GetConfigJobName, - basePath % "/config") - , d(new Private) -{ -} - -GetConfigJob::~GetConfigJob() = default; - -Omittable<qint64> GetConfigJob::uploadSize() const -{ - return d->uploadSize; -} - -BaseJob::Status GetConfigJob::parseJson(const QJsonDocument& data) -{ - auto json = data.object(); - fromJson(json.value("m.upload.size"_ls), d->uploadSize); - return Success; -} - + : BaseJob(HttpVerb::Get, QStringLiteral("GetConfigJob"), + QStringLiteral("/_matrix/media/r0") % "/config") +{} |