diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp index eed8f2416561fa0deab9b62abc190b5e8d6ced5a..cc43a66d164e9a4b4770aaf5106cb911ad4eab6c 100644 --- a/include/mtxclient/http/client.hpp +++ b/include/mtxclient/http/client.hpp @@ -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. diff --git a/lib/http/client.cpp b/lib/http/client.cpp index c5e72d14d426de77a5c8110efdbdef78b1e5aaf1..8c608fdd3a4d5a643136ed8da5a6540067f4471d 100644 --- a/lib/http/client.cpp +++ b/lib/http/client.cpp @@ -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, diff --git a/tests/media_api.cpp b/tests/media_api.cpp index 2512eaec2b3d9d4e0134e3d44b74fe20784d4555..c07833b5d05b040bf049637febdc69ed1d3f8d49 100644 --- a/tests/media_api.cpp +++ b/tests/media_api.cpp @@ -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();