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

Implement generic upload method

fixes #4
parent 38135705
No related branches found
No related tags found
No related merge requests found
......@@ -85,6 +85,8 @@ if (BUILD_LIB_TESTS)
find_package(GTest)
file(COPY fixtures DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
if (NOT GTest_FOUND)
message(STATUS "Fetching and building GTest 1.8")
include(${CMAKE_SOURCE_DIR}/cmake/GoogleTest.cmake)
......@@ -95,8 +97,8 @@ if (BUILD_LIB_TESTS)
add_executable(client_api tests/client_api.cpp)
target_link_libraries(client_api matrix_client ${GTEST_BOTH_LIBRARIES})
#add_executable(media_api tests/media_api.cpp)
#target_link_libraries(media_api matrix_client ${GTEST_BOTH_LIBRARIES})
add_executable(media_api tests/media_api.cpp)
target_link_libraries(media_api matrix_client ${GTEST_BOTH_LIBRARIES})
#add_executable(sync tests/sync.cpp)
#target_link_libraries(sync matrix_client ${GTEST_BOTH_LIBRARIES})
......@@ -110,8 +112,10 @@ if (BUILD_LIB_TESTS)
if (NOT GTest_FOUND)
add_dependencies(client_api GTest)
add_dependencies(connection GTest)
add_dependencies(media_api GTest)
endif()
add_test(BasicConnectivity connection)
add_test(ClientAPI client_api)
add_test(MediaAPI media_api)
endif()
File added
fixtures/test.jpeg

3.71 KiB

......@@ -334,3 +334,15 @@ Client::versions(std::function<void(const mtx::responses::Versions &res, Request
{
get<mtx::responses::Versions>("/client/versions", callback);
}
void
Client::upload(const std::string &data,
const std::string &content_type,
const std::string &filename,
std::function<void(const mtx::responses::ContentURI &res, RequestErr err)> cb)
{
std::map<std::string, std::string> params = {{"filename", filename}};
const auto api_path = "/media/r0/upload?" + utils::query_params(params);
post<std::string, mtx::responses::ContentURI>(api_path, data, cb, true, content_type);
}
......@@ -37,7 +37,7 @@ public:
//! Add an access token.
void set_access_token(const std::string &token) { access_token_ = token; }
//! Retrieve the access token.
std::string access_token() { return access_token_; }
std::string access_token() const { return access_token_; }
//! Update the next batch token.
void set_next_batch_token(const std::string &token) { next_batch_token_ = token; }
//! Retrieve the current next batch token.
......@@ -81,6 +81,11 @@ public:
//! Get the supported versions from the server.
void versions(std::function<void(const mtx::responses::Versions &res, RequestErr err)>);
//! Upload data to the content repository.
void upload(const std::string &data,
const std::string &content_type,
const std::string &filename,
std::function<void(const mtx::responses::ContentURI &res, RequestErr err)> cb);
/* void download_room_avatar(); */
/* void download_user_avatar(); */
/* void download_media(); */
......@@ -103,7 +108,8 @@ private:
const Request &req,
std::function<void(const Response &,
std::experimental::optional<mtx::client::errors::ClientError>)>,
bool requires_auth = true);
bool requires_auth = true,
const std::string &content_type = "application/json");
// put function for the PUT HTTP requests that send responses
template<class Request, class Response>
......@@ -207,7 +213,8 @@ mtx::client::Client::post(
const Request &req,
std::function<void(const Response &,
std::experimental::optional<mtx::client::errors::ClientError>)> callback,
bool requires_auth)
bool requires_auth,
const std::string &content_type)
{
using CallbackType = std::function<void(
const Response &, std::experimental::optional<mtx::client::errors::ClientError>)>;
......@@ -217,7 +224,7 @@ mtx::client::Client::post(
session->request.method(boost::beast::http::verb::post);
session->request.target("/_matrix" + endpoint);
session->request.set(boost::beast::http::field::user_agent, "mtxclient v0.1.0");
session->request.set(boost::beast::http::field::content_type, "application/json");
session->request.set(boost::beast::http::field::content_type, content_type);
session->request.set(boost::beast::http::field::host, session->host);
if (requires_auth && !access_token_.empty())
session->request.set(boost::beast::http::field::authorization,
......
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <fstream>
#include <streambuf>
#include <thread>
#include <gtest/gtest.h>
#include "client.hpp"
#include "mtx/requests.hpp"
#include "mtx/responses.hpp"
using namespace mtx::client;
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
read_file(const string &file_path)
{
ifstream file(file_path);
string data((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
return data;
}
void
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)
{
std::shared_ptr<Client> alice = std::make_shared<Client>("localhost");
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);
});
alice->close();
}
TEST(MediaAPI, UploadAudio)
{
std::shared_ptr<Client> bob = std::make_shared<Client>("localhost");
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);
});
bob->close();
}
TEST(MediaAPI, UploadImage)
{
std::shared_ptr<Client> carl = std::make_shared<Client>("localhost");
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);
});
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