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/imageFile.cpp
|
||||
src/main.cpp
|
||||
src/managers/song_manager.cpp
|
||||
src/metadata_retriever.cpp
|
||||
src/token_manager.cpp
|
||||
)
|
||||
@@ -21,6 +22,7 @@ set(HEADERS
|
||||
include/database/base_repository.h
|
||||
include/directory_manager.h
|
||||
include/imageFile.h
|
||||
include/managers/song_manager.h
|
||||
include/metadata_retriever.h
|
||||
include/models.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_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct Song
|
||||
{
|
||||
@@ -13,6 +14,7 @@ struct Song
|
||||
int Year;
|
||||
int Duration;
|
||||
char SongPath[1024];
|
||||
std::vector<unsigned char> data;
|
||||
};
|
||||
|
||||
struct Cover
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
@@ -12,7 +13,8 @@
|
||||
#include "oatpp/web/mime/multipart/Reader.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
|
||||
{
|
||||
@@ -24,16 +26,61 @@ public:
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||
|
||||
// TODO: work on uploading a song
|
||||
// and clean up
|
||||
ENDPOINT("POST", "/api/v1/song/data", songUpload,
|
||||
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)
|
||||
private:
|
||||
std::string exe_path;
|
||||
|
||||
const long dataSize = 1000000000000;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+4
-28
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "appComponent.hpp"
|
||||
#include "controller/loginController.hpp"
|
||||
#include "controller/songController.hpp"
|
||||
#include "database/base_repository.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
@@ -20,12 +21,12 @@ void run(const std::string& working_path)
|
||||
{
|
||||
appComponent component;
|
||||
|
||||
//auto router = oatpp::web::server::HttpRouter::createShared();
|
||||
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 sngController = std::make_shared<songController>(working_path);
|
||||
logController->addEndpointsToRouter(router);
|
||||
sngController->addEndpointsToRouter(router);
|
||||
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connectionHandler);
|
||||
|
||||
@@ -38,31 +39,6 @@ void run(const std::string& working_path)
|
||||
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()
|
||||
{
|
||||
database_connection mysqlD;
|
||||
@@ -108,7 +84,7 @@ int main(int argc, char **argv)
|
||||
oatpp::base::Environment::init();
|
||||
std::string working_path = argv[0];
|
||||
|
||||
test_database();
|
||||
//test_database();
|
||||
run(working_path);
|
||||
|
||||
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