Able to upload song. Left TODO on what's next
This commit is contained in:
@@ -14,6 +14,7 @@ set(SOURCES
|
|||||||
src/dto/loginResultDto.hpp
|
src/dto/loginResultDto.hpp
|
||||||
src/imageFile.cpp
|
src/imageFile.cpp
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
src/managers/song_manager.cpp
|
||||||
src/metadata_retriever.cpp
|
src/metadata_retriever.cpp
|
||||||
src/token_manager.cpp
|
src/token_manager.cpp
|
||||||
)
|
)
|
||||||
@@ -21,6 +22,7 @@ set(HEADERS
|
|||||||
include/database/base_repository.h
|
include/database/base_repository.h
|
||||||
include/directory_manager.h
|
include/directory_manager.h
|
||||||
include/imageFile.h
|
include/imageFile.h
|
||||||
|
include/managers/song_manager.h
|
||||||
include/metadata_retriever.h
|
include/metadata_retriever.h
|
||||||
include/models.h
|
include/models.h
|
||||||
include/token_manager.h
|
include/token_manager.h
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef SONGMANAGER_H_
|
||||||
|
#define SONGMANAGER_H_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "models.h"
|
||||||
|
|
||||||
|
class song_manager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
song_manager(std::string&);
|
||||||
|
|
||||||
|
void saveSong(Song&);
|
||||||
|
private:
|
||||||
|
std::string exe_path;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#define MODELS_H_
|
#define MODELS_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
struct Song
|
struct Song
|
||||||
{
|
{
|
||||||
@@ -13,6 +14,7 @@ struct Song
|
|||||||
int Year;
|
int Year;
|
||||||
int Duration;
|
int Duration;
|
||||||
char SongPath[1024];
|
char SongPath[1024];
|
||||||
|
std::vector<unsigned char> data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Cover
|
struct Cover
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "oatpp/core/macro/codegen.hpp"
|
#include "oatpp/core/macro/codegen.hpp"
|
||||||
#include "oatpp/core/macro/component.hpp"
|
#include "oatpp/core/macro/component.hpp"
|
||||||
@@ -12,7 +13,8 @@
|
|||||||
#include "oatpp/web/mime/multipart/Reader.hpp"
|
#include "oatpp/web/mime/multipart/Reader.hpp"
|
||||||
#include "oatpp/web/server/api/ApiController.hpp"
|
#include "oatpp/web/server/api/ApiController.hpp"
|
||||||
|
|
||||||
//#include "../dto/DTO.h"
|
#include "managers/song_manager.h"
|
||||||
|
#include "models.h"
|
||||||
|
|
||||||
class songController : public oatpp::web::server::api::ApiController
|
class songController : public oatpp::web::server::api::ApiController
|
||||||
{
|
{
|
||||||
@@ -24,16 +26,61 @@ public:
|
|||||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||||
|
|
||||||
// TODO: work on uploading a song
|
// TODO: work on uploading a song
|
||||||
|
// and clean up
|
||||||
ENDPOINT("POST", "/api/v1/song/data", songUpload,
|
ENDPOINT("POST", "/api/v1/song/data", songUpload,
|
||||||
REQUEST(std::shared_ptr<IncomingRequest>, request))
|
REQUEST(std::shared_ptr<IncomingRequest>, request))
|
||||||
{
|
{
|
||||||
|
auto mp = std::make_shared<oatpp::web::mime::multipart::Multipart>(request->getHeaders());
|
||||||
|
|
||||||
return createResonse(Status::CODE_200, "OK");
|
oatpp::web::mime::multipart::Reader mp_reader(mp.get());
|
||||||
|
|
||||||
|
mp_reader.setPartReader("file", oatpp::web::mime::multipart::createInMemoryPartReader(dataSize));
|
||||||
|
|
||||||
|
request->transferBody(&mp_reader);
|
||||||
|
|
||||||
|
auto file = mp->getNamedPart("file");
|
||||||
|
|
||||||
|
OATPP_ASSERT_HTTP(file, Status::CODE_400, "file is null");
|
||||||
|
|
||||||
|
auto stream = file->getInputStream();
|
||||||
|
//char *buff = new char[file->getKnownSize()];
|
||||||
|
auto buff = std::unique_ptr<char>(new char[file->getKnownSize()]);
|
||||||
|
//std::vector<unsigned char> buff;
|
||||||
|
//buff.reserve(file->getKnownSize());
|
||||||
|
//std::string buff;
|
||||||
|
//std::vector<unsigned char> buff;
|
||||||
|
auto buffSize = stream->read(buff.get(), file->getKnownSize()-1);
|
||||||
|
//auto buffSize = stream->read(&buff[0], file->getKnownSize()-1);
|
||||||
|
std::cout << "buff size " << buffSize << std::endl;
|
||||||
|
/**
|
||||||
|
std::cout << "buff " << buff << std::endl;
|
||||||
|
//std::cout << stream.get() << std::endl;
|
||||||
|
*/
|
||||||
|
std::vector<unsigned char> data(buff.get(), buff.get() + buffSize);
|
||||||
|
std::cout << "size of data " << data.size() << std::endl;
|
||||||
|
|
||||||
|
Song sng;
|
||||||
|
sng.data = std::move(data);
|
||||||
|
//sng.data = std::move(buff);
|
||||||
|
//std::cout << "data buff " << sng.data << std::endl;
|
||||||
|
std::cout << "data size " << sng.data.size() << std::endl;
|
||||||
|
song_manager s_mgr(exe_path);
|
||||||
|
s_mgr.saveSong(sng);
|
||||||
|
/* */
|
||||||
|
|
||||||
|
//std::string fileStr = file->getInMemoryData()->c_str();
|
||||||
|
//std::cout << fileStr << std::endl;
|
||||||
|
//std::cout << "known size " << file->getKnownSize() << std::endl;
|
||||||
|
//std::cout << "size " << fileStr.size() << std::endl;
|
||||||
|
|
||||||
|
return createResponse(Status::CODE_200, "OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
#include OATPP_CODEGEN_END(ApiController)
|
#include OATPP_CODEGEN_END(ApiController)
|
||||||
private:
|
private:
|
||||||
std::string exe_path;
|
std::string exe_path;
|
||||||
|
|
||||||
|
const long dataSize = 1000000000000;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+4
-28
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#include "appComponent.hpp"
|
#include "appComponent.hpp"
|
||||||
#include "controller/loginController.hpp"
|
#include "controller/loginController.hpp"
|
||||||
|
#include "controller/songController.hpp"
|
||||||
#include "database/base_repository.h"
|
#include "database/base_repository.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
@@ -20,12 +21,12 @@ void run(const std::string& working_path)
|
|||||||
{
|
{
|
||||||
appComponent component;
|
appComponent component;
|
||||||
|
|
||||||
//auto router = oatpp::web::server::HttpRouter::createShared();
|
|
||||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
||||||
|
|
||||||
//router->route("GET", "/test", std::make_shared<loginHandler>());
|
|
||||||
auto logController = std::make_shared<loginController>(working_path);
|
auto logController = std::make_shared<loginController>(working_path);
|
||||||
|
auto sngController = std::make_shared<songController>(working_path);
|
||||||
logController->addEndpointsToRouter(router);
|
logController->addEndpointsToRouter(router);
|
||||||
|
sngController->addEndpointsToRouter(router);
|
||||||
|
|
||||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connectionHandler);
|
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connectionHandler);
|
||||||
|
|
||||||
@@ -38,31 +39,6 @@ void run(const std::string& working_path)
|
|||||||
server.run();
|
server.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
MYSQL* mysql_connection_setup(database_connection mysql_details)
|
|
||||||
{
|
|
||||||
// first of all create a mysql instance and initialize the variables within
|
|
||||||
MYSQL *connection = mysql_init(NULL);
|
|
||||||
|
|
||||||
// connect to the database with the details attached.
|
|
||||||
if (!mysql_real_connect(connection,mysql_details.server.c_str(), mysql_details.username.c_str(), mysql_details.password.c_str(), mysql_details.database.c_str(), 0, NULL, 0)) {
|
|
||||||
printf("Conection error : %s\n", mysql_error(connection));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
|
|
||||||
{
|
|
||||||
// send the query to the database
|
|
||||||
if (mysql_query(connection, sql_query))
|
|
||||||
{
|
|
||||||
printf("MySQL query error : %s\n", mysql_error(connection));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mysql_use_result(connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_database()
|
void test_database()
|
||||||
{
|
{
|
||||||
database_connection mysqlD;
|
database_connection mysqlD;
|
||||||
@@ -108,7 +84,7 @@ int main(int argc, char **argv)
|
|||||||
oatpp::base::Environment::init();
|
oatpp::base::Environment::init();
|
||||||
std::string working_path = argv[0];
|
std::string working_path = argv[0];
|
||||||
|
|
||||||
test_database();
|
//test_database();
|
||||||
run(working_path);
|
run(working_path);
|
||||||
|
|
||||||
oatpp::base::Environment::destroy();
|
oatpp::base::Environment::destroy();
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include "managers/song_manager.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
song_manager::song_manager(std::string& x_path)
|
||||||
|
: exe_path(x_path)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
void song_manager::saveSong(Song& song)
|
||||||
|
{
|
||||||
|
constexpr auto tmp_song = "/tmp/song.mp3";
|
||||||
|
if (fs::exists(fs::path(tmp_song))) {
|
||||||
|
std::cout << "deleting old song " << std::endl;
|
||||||
|
fs::remove(fs::path(tmp_song));
|
||||||
|
}
|
||||||
|
std::fstream s(tmp_song, std::fstream::binary | std::fstream::out);
|
||||||
|
s.write((char*)&song.data[0], song.data.size());
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user