Factoring

This commit is contained in:
kdeng00
2019-08-20 22:45:25 -04:00
parent e42a551193
commit 92f0d4702b
21 changed files with 362 additions and 233 deletions
+202
View File
@@ -0,0 +1,202 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <sstream>
#include "managers/directory_manager.h"
namespace fs = std::filesystem;
std::string directory_manager::create_directory_process(Song song, const std::string& 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 directory_manager::read_cover_art(const std::string& 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();
}
void directory_manager::copy_stock_to_root(const std::string& 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 directory_manager::copy_song_to_path(const std::string& target, const std::string& 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 directory_manager::delete_cover_art_file(const std::string& cov_path, const std::string& stock_cover_path)
{
if (cov_path.compare(stock_cover_path) == 0) {
std::cout << "cover has stock cover art, will not deleted" << std::endl;
} else {
std::cout << "deleting song path" << std::endl;
auto cov = fs::path(cov_path);
fs::remove(cov);
}
}
void directory_manager::delete_directories(Song song, const std::string& 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 directory_manager::delete_song(const 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;
}
}
*/
+154
View File
@@ -0,0 +1,154 @@
#include <iostream>
#include <iterator>
#include <fstream>
#include <filesystem>
#include <string>
#include <string_view>
#include <sstream>
#include <cstdlib>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include "managers/token_manager.h"
namespace fs = std::filesystem;
token_manager::token_manager()
{
}
loginResult token_manager::retrieve_token()
{
loginResult lr;
lr.access_token = "dsfdsf";
lr.token_type = "demo";
return lr;
}
loginResult token_manager::retrieve_token(std::string_view path)
{
auto cred = parse_auth_credentials(path);
nlohmann::json reqObj;
reqObj["client_id"] = cred.client_id;
reqObj["client_secret"] = cred.client_secret;
reqObj["audience"] = cred.api_identifier;
reqObj["grant_type"] = "client_credentials";
std::string uri{cred.uri};
uri.append("/");
uri.append(cred.endpoint);
auto r = cpr::Post(cpr::Url{uri},
cpr::Body{reqObj.dump()},
cpr::Header{{"Content-Type", "application/json"},
{"Connection", "keep-alive"}});
auto post_res = nlohmann::json::parse(r.text);
loginResult lr;
lr.access_token = post_res["access_token"].get<std::string>();
lr.token_type = post_res["token_type"].get<std::string>();
lr.expiration = post_res["expires_in"].get<int>();
return lr;
}
bool token_manager::is_token_valid(std::string& auth, Scope scope)
{
auto authPair = fetch_auth_header(auth);
if (!std::get<0>(authPair)) {
std::cout << "no Bearer found" << std::endl;
return std::get<0>(authPair);
}
auto authHeader = std::get<1>(authPair);
auto token = authHeader.at(authHeader.size()-1);
auto scopes = extract_scopes(jwt::decode(token));
switch (scope) {
case Scope::upload:
return token_supports_scope(scopes, "upload:songs");
break;
case Scope::download:
return token_supports_scope(scopes, "download:songs");
default:
break;
}
return false;
}
auth_credentials token_manager::parse_auth_credentials(std::string_view path)
{
auto exe_path = fs::canonical(path).parent_path().string();
exe_path.append("/authcredentials.json");
std::fstream a(exe_path, std::ios::in);
std::stringstream s;
s << a.rdbuf();
a.close();
auto con = nlohmann::json::parse(s.str());
auth_credentials auth;
auth.uri = "https://";
auth.uri.append(con["Domain"]);
auth.api_identifier = con["ApiIdentifier"];
auth.client_id = con["ClientId"];
auth.client_secret = con["ClientSecret"];
auth.endpoint = "oauth/token";
return auth;
}
std::vector<std::string> token_manager::extract_scopes(const jwt::decoded_jwt&& decoded)
{
std::vector<std::string> scopes;
for (auto d : decoded.get_payload_claims()) {
if (d.first.compare("scope") == 0) {
std::cout << "found scope" << std::endl;
std::string all_scopes(d.second.to_json().get<std::string>());
std::istringstream iss(all_scopes);
scopes.assign(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>());
}
}
return scopes;
}
std::pair<bool, std::vector<std::string>> token_manager::fetch_auth_header(const std::string& auth)
{
std::istringstream iss(auth);
std::vector<std::string> authHeader{std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>()
};
bool foundBearer = false;
if (std::any_of(authHeader.begin(), authHeader.end(),
[](std::string word) {
return (word.compare("Bearer") == 0);
})) {
std::cout << "Bearer found" << std::endl;
foundBearer = true;
}
return std::make_pair(foundBearer, authHeader);
}
bool token_manager::token_supports_scope(const std::vector<std::string> scopes, const std::string&& scope)
{
return std::any_of(scopes.begin(), scopes.end(),
[&](std::string foundScope) {
return (foundScope.compare(scope) == 0);
});
}