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

Add method for retrieving thumbnails

parent 71f80c5f
No related branches found
No related tags found
No related merge requests found
......@@ -76,11 +76,23 @@ struct MessagesOpts
uint16_t limit = 30;
};
//! Configuration for thumbnail retrieving.
struct ThumbOpts
{
//! The desired width of the thumbnail.
uint16_t width = 128;
//! The desired height of the thumbnail.
uint16_t height = 128;
//! The desired resizing method. One of: ["crop", "scale"]
std::string method = "crop";
//! A mxc URI which points to the content.
std::string mxc_url;
};
//! The main object that the user will interact.
class Client : public std::enable_shared_from_this<Client>
{
public:
Client() = default;
Client(const std::string &server = "", uint16_t port = 443);
//! Wait for the client to close.
......@@ -191,6 +203,10 @@ public:
const std::string &content_type,
const std::string &original_filename,
RequestErr err)> cb);
//! Retrieve a thumbnail from the given mxc url.
void get_thumbnail(const ThumbOpts &opts, Callback<std::string> cb);
//! Send typing notifications to the room.
void start_typing(const std::string &room_id, uint64_t timeout, ErrCallback cb);
//! Remove typing notifications from the room.
......
......@@ -234,6 +234,22 @@ Client::download(const std::string &mxc_url,
download(url.server, url.media_id, std::move(callback));
}
void
Client::get_thumbnail(const ThumbOpts &opts, Callback<std::string> callback)
{
std::map<std::string, std::string> params;
params.emplace("width", std::to_string(opts.width));
params.emplace("height", std::to_string(opts.height));
params.emplace("method", opts.method);
const auto mxc = mtx::client::utils::parse_mxc_url(opts.mxc_url);
const auto api_path = "/media/r0/thumbnail/" + mxc.server + "/" + mxc.media_id + "?" +
client::utils::query_params(params);
get<std::string>(
api_path,
[callback](const std::string &res, HeaderFields, RequestErr err) { callback(res, err); });
}
void
Client::download(const std::string &server,
const std::string &media_id,
......
......@@ -104,24 +104,31 @@ TEST(MediaAPI, UploadImage)
const auto img = read_file("./fixtures/test.jpeg");
carl->upload(img,
"image/jpeg",
"test.jpeg",
[carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
carl->download(res.content_uri,
[img](const string &data,
const string &content_type,
const string &original_filename,
RequestErr err) {
ASSERT_FALSE(err);
EXPECT_EQ(data, img);
EXPECT_EQ(content_type, "image/jpeg");
EXPECT_EQ(original_filename,
"test.jpeg");
});
});
carl->upload(
img,
"image/jpeg",
"test.jpeg",
[carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
ThumbOpts opts;
opts.mxc_url = res.content_uri;
carl->get_thumbnail(opts, [](const std::string &res, RequestErr err) {
ASSERT_FALSE(err);
ASSERT_FALSE(res.empty());
});
carl->download(res.content_uri,
[img](const string &data,
const string &content_type,
const string &original_filename,
RequestErr err) {
ASSERT_FALSE(err);
EXPECT_EQ(data, img);
EXPECT_EQ(content_type, "image/jpeg");
EXPECT_EQ(original_filename, "test.jpeg");
});
});
});
carl->close();
......
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