From ee7246abacc5bba3d1827330211504059beb1633 Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Thu, 8 Mar 2018 19:35:59 +0200
Subject: [PATCH] Add basic support for downloading

---
 src/client.cpp      |  9 +++++++
 src/client.hpp      | 15 +++++------
 tests/media_api.cpp | 63 ++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 70 insertions(+), 17 deletions(-)

diff --git a/src/client.cpp b/src/client.cpp
index c5023508f..6f171d2fa 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -359,3 +359,12 @@ Client::upload(const std::string &data,
         const auto api_path = "/media/r0/upload?" + utils::query_params(params);
         post<std::string, mtx::responses::ContentURI>(api_path, data, cb, true, content_type);
 }
+
+void
+Client::download(const std::string &server,
+                 const std::string &media_id,
+                 std::function<void(const std::string &res, RequestErr err)> cb)
+{
+        const auto api_path = "/media/r0/download/" + server + "/" + media_id;
+        get<std::string>(api_path, cb);
+}
diff --git a/src/client.hpp b/src/client.hpp
index 211034505..7182466f1 100644
--- a/src/client.hpp
+++ b/src/client.hpp
@@ -89,15 +89,14 @@ public:
                     const std::string &content_type,
                     const std::string &filename,
                     std::function<void(const mtx::responses::ContentURI &res, RequestErr err)> cb);
+        //! Retrieve data from the content repository.
+        void download(const std::string &server,
+                      const std::string &media_id,
+                      std::function<void(const std::string &data, RequestErr err)> cb);
         /* void download_room_avatar(); */
         /* void download_user_avatar(); */
         /* void download_media(); */
 
-        /* void upload_image(); */
-        /* void upload_file(); */
-        /* void upload_audio(); */
-        /* void upload_video(); */
-
         /* void upload_filter(); */
 
         /* void send_typing_notification(); */
@@ -349,16 +348,16 @@ mtx::client::Client::create_session(const Callback &callback)
                                           return callback(response_data, client_error);
                                   } catch (nlohmann::json::exception &e) {
                                           std::cout << e.what() << ": Couldn't parse response\n"
-                                                    << response.body().data() << std::endl;
+                                                    << response.body() << std::endl;
                                           // TODO: handle error
                                   }
                           }
 
                           try {
-                                  response_data = deserialize<Response>(response.body().data());
+                                  response_data = deserialize<Response>(response.body());
                           } catch (nlohmann::json::exception &e) {
                                   std::cout << e.what() << ": Couldn't parse response\n"
-                                            << response.body().data() << std::endl;
+                                            << response.body() << std::endl;
                                   // TODO: handle error
                           }
 
diff --git a/tests/media_api.cpp b/tests/media_api.cpp
index 84f6dcbd4..a5a5e2b70 100644
--- a/tests/media_api.cpp
+++ b/tests/media_api.cpp
@@ -16,11 +16,17 @@ using namespace mtx::identifiers;
 
 using namespace std;
 
-// std::vector<std::string> results;
-// boost::split(results, res.content_uri, [](char c) { return c == '/'; });
-
 using ErrType = std::experimental::optional<errors::ClientError>;
 
+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)
 {
@@ -35,7 +41,6 @@ validate_upload(const mtx::responses::ContentURI &res, ErrType err)
 {
         ASSERT_FALSE(err);
         ASSERT_TRUE(res.content_uri.size() > 10);
-        cout << res.content_uri << endl;
 }
 
 TEST(MediaAPI, UploadTextFile)
@@ -45,7 +50,21 @@ TEST(MediaAPI, UploadTextFile)
         alice->login("alice", "secret", [alice](const mtx::responses::Login &, ErrType err) {
                 ASSERT_FALSE(err);
 
-                alice->upload("This is a text content", "text/plain", "doc.txt", validate_upload);
+                const auto text = "This is some random text";
+
+                alice->upload(text,
+                              "text/plain",
+                              "doc.txt",
+                              [alice, text](const mtx::responses::ContentURI &res, ErrType err) {
+                                      validate_upload(res, err);
+
+                                      alice->download("localhost",
+                                                      get_media_id(res),
+                                                      [text](const string &data, ErrType err) {
+                                                              ASSERT_FALSE(err);
+                                                              EXPECT_EQ(data, text);
+                                                      });
+                              });
         });
 
         alice->close();
@@ -58,8 +77,21 @@ TEST(MediaAPI, UploadAudio)
         bob->login("bob", "secret", [bob](const mtx::responses::Login &, ErrType err) {
                 ASSERT_FALSE(err);
 
-                bob->upload(
-                  read_file("./fixtures/sound.mp3"), "audio/mp3", "sound.mp3", validate_upload);
+                const auto audio = read_file("./fixtures/sound.mp3");
+
+                bob->upload(audio,
+                            "audio/mp3",
+                            "sound.mp3",
+                            [bob, audio](const mtx::responses::ContentURI &res, ErrType err) {
+                                    validate_upload(res, err);
+
+                                    bob->download("localhost",
+                                                  get_media_id(res),
+                                                  [audio](const string &data, ErrType err) {
+                                                          ASSERT_FALSE(err);
+                                                          EXPECT_EQ(data, audio);
+                                                  });
+                            });
         });
 
         bob->close();
@@ -72,8 +104,21 @@ TEST(MediaAPI, UploadImage)
         carl->login("carl", "secret", [carl](const mtx::responses::Login &, ErrType err) {
                 ASSERT_FALSE(err);
 
-                carl->upload(
-                  read_file("./fixtures/test.jpeg"), "image/jpeg", "test.jpeg", validate_upload);
+                const auto img = read_file("./fixtures/test.jpeg");
+
+                carl->upload(img,
+                             "image/jpeg",
+                             "test.jpeg",
+                             [carl, img](const mtx::responses::ContentURI &res, ErrType err) {
+                                     validate_upload(res, err);
+
+                                     carl->download("localhost",
+                                                    get_media_id(res),
+                                                    [img](const string &data, ErrType err) {
+                                                            ASSERT_FALSE(err);
+                                                            EXPECT_EQ(data, img);
+                                                    });
+                             });
         });
 
         carl->close();
-- 
GitLab