Skip to content
Snippets Groups Projects
Commit e1450e55 authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Add method to download data through matrix content URIs

parent 597e1a15
No related branches found
No related tags found
No related merge requests found
......@@ -172,6 +172,11 @@ public:
const std::string &filename,
Callback<mtx::responses::ContentURI> cb);
//! Retrieve data from the content repository.
void download(const std::string &mxc_url,
std::function<void(const std::string &data,
const std::string &content_type,
const std::string &original_filename,
RequestErr err)> cb);
void download(const std::string &server,
const std::string &media_id,
std::function<void(const std::string &data,
......
......@@ -10,6 +10,19 @@ namespace mtx {
namespace client {
namespace utils {
//! Representation of Matrix Content (MXC) URIs.
struct MxcUrl
{
//! The name of the homeserver where the content originated.
std::string server;
//! An opaque ID which identifies the content.
std::string media_id;
};
//! Parse a matrix content URI into its server & media_id components.
MxcUrl
parse_mxc_url(const std::string &url);
//! Check if the given string represents a number.
bool
is_number(const std::string &s);
......
......@@ -231,6 +231,17 @@ Client::upload(const std::string &data,
post<std::string, mtx::responses::ContentURI>(api_path, data, cb, true, content_type);
}
void
Client::download(const std::string &mxc_url,
std::function<void(const std::string &res,
const std::string &content_type,
const std::string &original_filename,
RequestErr err)> callback)
{
auto url = mtx::client::utils::parse_mxc_url(mxc_url);
download(url.server, url.media_id, std::move(callback));
}
void
Client::download(const std::string &server,
const std::string &media_id,
......
......@@ -4,6 +4,7 @@
#include <string>
#include <utility>
#include <boost/algorithm/string.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/detail/error.hpp>
#include <boost/iostreams/detail/forward.hpp>
......@@ -14,6 +15,31 @@
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
mtx::client::utils::MxcUrl
mtx::client::utils::parse_mxc_url(const std::string &url)
{
constexpr auto mxc_uri_protocol = "mxc://";
MxcUrl res;
if (url.find(mxc_uri_protocol) != 0)
return res;
auto str_left = url.substr(6);
std::vector<std::string> parts;
boost::split(parts, str_left, [](char c) { return c == '/'; });
if (parts.size() != 2) {
res.server = parts.at(0);
return res;
}
res.server = parts.at(0);
res.media_id = parts.at(1);
return res;
}
bool
mtx::client::utils::is_number(const std::string &s)
{
......
......@@ -16,15 +16,6 @@ using namespace mtx::identifiers;
using namespace std;
string
get_media_id(const mtx::responses::ContentURI &res)
{
vector<string> results;
boost::split(results, res.content_uri, [](char c) { return c == '/'; });
return results.back();
}
string
read_file(const string &file_path)
{
......@@ -56,8 +47,7 @@ TEST(MediaAPI, UploadTextFile)
[alice, text](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
alice->download("localhost",
get_media_id(res),
alice->download(res.content_uri,
[text](const string &data,
const string &content_type,
const string &original_filename,
......@@ -89,8 +79,7 @@ TEST(MediaAPI, UploadAudio)
[bob, audio](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
bob->download("localhost",
get_media_id(res),
bob->download(res.content_uri,
[audio](const string &data,
const string &content_type,
const string &original_filename,
......@@ -121,8 +110,7 @@ TEST(MediaAPI, UploadImage)
[carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
carl->download("localhost",
get_media_id(res),
carl->download(res.content_uri,
[img](const string &data,
const string &content_type,
const string &original_filename,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment