Tying something out

This commit is contained in:
kdeng00
2019-08-17 16:04:54 -04:00
parent 533a8d81a9
commit 0fc16daf86
84 changed files with 295 additions and 5694 deletions
+34
View File
@@ -0,0 +1,34 @@
#ifndef APPCOMPONENT_H_
#define APPCOMPONENT_H_
#include <memory>
#include "oatpp/core/macro/component.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
class appComponent
{
public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(5002);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
return oatpp::parser::json::mapping::ObjectMapper::createShared();
}());
private:
};
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef LOGINCONTROLLER_H_
#define LOGINCONTROLLER_H_
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"
#include "oatpp/web/server/api/ApiController.hpp"
#include "../dto/loginResultDto.hpp"
class loginController : public oatpp::web::server::api::ApiController
{
public:
loginController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
: oatpp::web::server::api::ApiController(objectMapper)
{ }
#include OATPP_CODEGEN_BEGIN(ApiController)
ENDPOINT("POST", "/api/v1/login", root)
{
OATPP_LOGI("icarus", "logging in");
auto logRes = loginResultDto::createShared();
logRes->access_token = "sdfsdfsdfsdert";
logRes->token_type = "Special";
return createDtoResponse(Status::CODE_200, logRes);
}
#include OATPP_CODEGEN_END(ApiController)
private:
};
#endif
+199
View File
@@ -0,0 +1,199 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <sstream>
#include "directory_manager.h"
namespace fs = std::filesystem;
std::string create_directory_process(Song song, const char *root_path)
{
auto curr_path = fs::path(root_path);
if (fs::exists(curr_path)) {
std::cout<<"path exists"<<std::endl;
} else {
std::cout<<"creating path"<<std::endl;
fs::create_directory(curr_path);
}
auto art_path = fs::path(curr_path.string() + song.Artist);
if (fs::exists(art_path)) {
std::cout<<"artist path exists"<<std::endl;
} else {
std::cout<<"creating artist path"<<std::endl;
fs::create_directory(art_path);
}
auto alb_path = fs::path(art_path.string() + "/" + song.Album);
if (fs::exists(alb_path)) {
std::cout<<"album path exists"<<std::endl;
} else {
std::cout<<"creating album path"<<std::endl;
fs::create_directory(alb_path);
}
return alb_path.string() + "/";
}
std::string read_cover_art(const char *source)
{
auto source_path = fs::path(source);
std::fstream cov(source, std::ios::in | std::ios::binary);
cov.seekg(0);
std::stringstream buf;
std::copy(std::istreambuf_iterator<char>(cov),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(buf));
return buf.str();
}
bool delete_song(Song *song)
{
return fs::remove(song->SongPath);
}
void copy_stock_to_root(const char *target, const std::string buff)
{
std::cout<<"starting process"<<std::endl;
auto target_path = fs::path(target);
if (fs::exists(target_path)) {
std::cout<<target_path.string()<<" exists"<<std::endl;
return;
}
std::cout<<target_path.string()<<" does not exist, copying over"<<std::endl;
std::fstream cov(target, std::ios::out | std::ios::binary);
cov.write(buff.c_str(), buff.size());
cov.close();
std::cout<<"copy finished"<<std::endl;
}
void copy_song_to_path(const char *target, const char *source)
{
std::cout<<"starting process to copy song"<<std::endl;
auto target_path = fs::path(target);
auto src_path = fs::path(source);
std::cout<<"copting over to "<<target_path.string()<<std::endl;
fs::copy(src_path, target_path);
fs::remove(source);
std::cout<<"copy finished"<<std::endl;
}
void delete_cover_art_file(const std::string cov_path)
{
auto cov = fs::path(cov_path);
fs::remove(cov);
}
void delete_directories(Song song, const char *root_path)
{
std::cout<<"checking to for empty directories to delete"<<std::endl;
const std::string art{root_path + std::string{"/"} + song.Artist};
const std::string alb{art + "/" + song.Album};
auto alb_path = fs::path(alb);
if (!fs::exists(alb_path)) {
std::cout<<"directory does not exists"<<std::endl;
} else if (fs::is_empty(alb_path)) {
fs::remove(alb_path);
}
auto art_path = fs::path(art);
if (!fs::exists(art_path)) {
std::cout<<"directory does not exists"<<std::endl;
return;
} else if (fs::is_empty(art_path)) {
fs::remove(art_path);
}
std::cout<<"deleted empty directory or directories"<<std::endl;
}
void delete_song(Song song)
{
std::cout<<"deleting song"<<std::endl;
auto song_path = fs::path(song.SongPath);
if (!fs::exists(song_path)) {
std::cout<<"song does not exists"<<std::endl;
return;
}
fs::remove(song_path);
std::cout<<"deleted song"<<std::endl;
}
extern "C"
{
void create_directory(Song, const char*, char*);
void copy_stock_cover_art(const char*, const char*);
void copy_song(const char*, const char*);
void delete_cover_art(const char*, const char*);
void delete_empty_directories(Song, const char*);
void delete_from_filesystem(Song);
void delete_song_empty_directories(Song, const char*);
void print_song_details(const Song);
void create_directory(Song song, const char *root_path, char *dir)
{
const auto tmp = create_directory_process(song, root_path);
size_t tmp_sz = tmp.size();
tmp.copy(dir, tmp_sz, 0);
}
void copy_stock_cover_art(const char *target, const char *source)
{
const auto buff = read_cover_art(source);
copy_stock_to_root(target, buff);
}
void copy_song(const char *target, const char *source)
{
copy_song_to_path(target, source);
}
void delete_cover_art(const char *cover_path, const char *stock_path)
{
std::cout<<"starting process to delete cover art"<<std::endl;
const auto cov_path = std::string{cover_path};
const auto s_path = std::string{stock_path};
if (cov_path.compare(s_path) != 0) {
std::cout<<"cover art is not the stock path"<<std::endl;
delete_cover_art_file(cov_path);
} else {
std::cout<<"cover art is the stock path and will not be deleted"<<std::endl;
}
}
void delete_empty_directories(Song song, const char *root_path)
{
delete_directories(song, root_path);
}
void delete_from_filesystem(Song song)
{
if (delete_song(&song)) {
std::cout<<"successfully deleted song from filesystem"<<std::endl;
} else {
std::cout<<"failed to deleted song from filesystem"<<std::endl;
}
}
void delete_song_empty_directories(Song song, const char *root_path)
{
delete_song(song);
delete_directories(song, root_path);
}
void print_song_details(const Song song)
{
std::cout<<"song details"<<std::endl;
std::cout<<"title: "<<song.Title<<std::endl;
std::cout<<"artist: "<<song.Artist<<std::endl;
std::cout<<"album: "<<song.Album<<std::endl;
std::cout<<"genre: "<<song.Genre<<std::endl;
std::cout<<"year: "<<song.Year<<std::endl;
}
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef LOGINRESULTDTO_H_
#define LOGINRESULTDTO_H_
#include "oatpp/core/data/mapping/type/Object.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include OATPP_CODEGEN_BEGIN(DTO)
class loginResultDto : public oatpp::data::mapping::type::Object
{
DTO_INIT(loginResultDto, Object)
DTO_FIELD(String, access_token);
DTO_FIELD(String, token_type);
};
#include OATPP_CODEGEN_END(DTO)
#endif
+10
View File
@@ -0,0 +1,10 @@
#include "imageFile.h"
imageFile::imageFile(const char *file) : TagLib::File(file)
{
}
TagLib::ByteVector imageFile::data()
{
return readBlock(length());
}
+2
View File
@@ -0,0 +1,2 @@
#include "loginHandler.h"
+29
View File
@@ -0,0 +1,29 @@
#ifndef LOGINHANDLER_H_
#define LOGINHANDLER_H_
#include <memory>
#include "oatpp/network/server/Server.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "dto/loginResultDto.hpp"
//#include "dto/loginResultDto.hpp"
class loginHandler : public oatpp::web::server::HttpRequestHandler
{
public:
std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override
{
auto logRes = loginResultDto::createShared();
logRes->access_token = "hahahahahahaha";
logRes->token_type = "Fly";
return ResponseFactory::createResponse(Status::CODE_200, "ddd");
}
private:
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, m_objectMapper);
};
#endif
+43
View File
@@ -0,0 +1,43 @@
#include <iostream>
#include <memory>
#include "oatpp/network/server/Server.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "appComponent.hpp"
#include "controller/loginController.hpp"
//#include "loginHandler.hpp"
void run()
{
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>();
logController->addEndpointsToRouter(router);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connectionHandler);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
oatpp::network::server::Server server(connectionProvider, connectionHandler);
OATPP_LOGI("icarus", "Server running on port %s", connectionProvider->getProperty("port").getData());
server.run();
}
int main(int argc, char **argv)
{
oatpp::base::Environment::init();
run();
oatpp::base::Environment::destroy();
return 0;
}
+102
View File
@@ -0,0 +1,102 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <sstream>
#include <string.h>
//#include <taglib/attachedpictureframe.h>
#include <attachedpictureframe.h>
//#include <taglib/fileref.h>
#include <fileref.h>
//#include <taglib/mpegfile.h>
#include <mpegfile.h>
//#include <taglib/tag.h>
#include <tag.h>
#include "directory_manager.h"
#include "metadata_retriever.h"
#include "imageFile.h"
extern "C"
{
void retrieve_metadata(Song*, char*);
void update_metadata(Song*, Song*);
void retrieve_metadata(Song *sng, char* song_path)
{
std::cout<<"extern C"<<std::endl;
TagLib::FileRef file(song_path);
strcpy(sng->Title, file.tag()->title().toCString());
strcpy(sng->Artist, file.tag()->artist().toCString());
strcpy(sng->Album, file.tag()->album().toCString());
strcpy(sng->Genre, file.tag()->genre().toCString());
sng->Year = file.tag()->year();
sng->Duration = file.audioProperties()->lengthInSeconds();
strcpy(sng->SongPath, song_path);
}
void update_metadata(Song *sng_updated, Song *sng_old)
{
std::cout<<"updating metadata"<<std::endl;
TagLib::FileRef file(sng_old->SongPath);
if (strlen(sng_updated->Title) > 0) {
file.tag()->setTitle(sng_updated->Title);
}
if (strlen(sng_updated->Artist) > 0) {
file.tag()->setArtist(sng_updated->Artist);
}
if (strlen(sng_updated->Album) > 0) {
file.tag()->setAlbum(sng_updated->Album);
}
if (strlen(sng_updated->Genre) > 0) {
file.tag()->setGenre(sng_updated->Genre);
}
if (sng_updated->Year > 0) {
file.tag()->setYear(sng_updated->Year);
}
file.save();
}
void update_cover_art(Cover *cov, Song *song, const char *root_path)
{
TagLib::MPEG::File sngF(song->SongPath);
TagLib::ID3v2::Tag *tag = sngF.ID3v2Tag();
auto frameList = tag->frameListMap()["APIC"];
if (frameList.isEmpty()) {
std::string stock_path{root_path};
stock_path.append("CoverArt.png");
strcpy(cov->ImagePath, stock_path.c_str());
imageFile stock_img(stock_path.c_str());
TagLib::ID3v2::AttachedPictureFrame *pic =
new TagLib::ID3v2::AttachedPictureFrame;
pic->setPicture(stock_img.data());
pic->setType(TagLib::ID3v2::AttachedPictureFrame::FrontCover);
tag->addFrame(pic);
sngF.save();
std::cout<<"applied stock cover art"<<std::endl;
} else {
auto frame = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(
frameList.front());
auto img_path = create_directory_process(*song, root_path);
img_path.append(song->Title);
img_path.append(".png");
strcpy(cov->ImagePath, img_path.c_str());
std::cout<<cov->ImagePath<<std::endl;
std::fstream img_save(cov->ImagePath, std::ios::out |
std::ios::binary);
img_save.write(frame->picture().data(), frame->picture().size());
img_save.close();
std::cout<<"saved to "<<cov->ImagePath<<std::endl;
}
}
}
+41
View File
@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
#include <cstdlib>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include "models.h"
extern "C"
{
LoginRes* retrieve_token(TokenReq *tok)
{
LoginRes *res = new LoginRes;
nlohmann::json reqObj;
reqObj["client_id"] = tok->ClientId;
reqObj["client_secret"] = tok->ClientSecret;
reqObj["audience"] = tok->Audience;
reqObj["grant_type"] = tok->GrantType;
std::string uri{tok->URI};
uri.append("/");
uri.append(tok->Endpoint);
auto r = cpr::Post(cpr::Url{uri},
cpr::Body{reqObj.dump()},
cpr::Header{{"Content-Type", "application/json"}});
auto post_res = nlohmann::json::parse(r.text);
strcpy(res->Token, post_res["access_token"].get<std::string>().c_str());
strcpy(res->TokenType, post_res["token_type"].get<std::string>().c_str());
res->Expiration = post_res["expires_in"].get<int>();
strcpy(res->Message, "Success");
return res;
}
}