Downloading functionality added #4 and #7

This commit is contained in:
amazing-username
2019-06-08 00:09:26 -04:00
parent 723206019e
commit 0e6ca31ed2
6 changed files with 104 additions and 4 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ namespace Managers
{ {
supportedFlags = vector<string>{ supportedFlags = vector<string>{
"-u", "-p", "-t", "-h", "-s", "-u", "-p", "-t", "-h", "-s",
"-d", "-D" "-d", "-D", "-b"
}; };
} }
void ActionManager::validateAction() void ActionManager::validateAction()
+32 -2
View File
@@ -6,6 +6,7 @@
#include"Models/Song.h" #include"Models/Song.h"
#include"Parsers/APIParser.h" #include"Parsers/APIParser.h"
#include"Syncers/Delete.h" #include"Syncers/Delete.h"
#include"Syncers/Download.h"
#include"Syncers/Upload.h" #include"Syncers/Upload.h"
#include"TokenManager.h" #include"TokenManager.h"
@@ -23,6 +24,7 @@ using Models::Song;
using Parsers::APIParser; using Parsers::APIParser;
using Models::IcarusAction; using Models::IcarusAction;
using Syncers::Delete; using Syncers::Delete;
using Syncers::Download;
using Syncers::Upload; using Syncers::Upload;
namespace Managers namespace Managers
@@ -49,7 +51,7 @@ namespace Managers
case downloadAct: case downloadAct:
downloadSong(); downloadSong();
break; break;
case retrieveAct: case retrieveAct: // No plans to imeplement
break; break;
case uploadAct: case uploadAct:
uploadSong(); uploadSong();
@@ -96,7 +98,35 @@ namespace Managers
} }
void CommitManager::downloadSong() void CommitManager::downloadSong()
{ {
// TODO: Implement this functionality cout<<"Starting downloading process..."<<endl;
UserManager usrMgr{icaAction};
auto user = usrMgr.retrieveUser();
APIParser apiPrs{icaAction};
auto api = apiPrs.retrieveAPI();
TokenManager tk{user, api};
auto token = tk.requestToken();
Song song{};
for (auto arg : icaAction.flags)
{
auto flag = arg.flag;
auto value = arg.value;
if (flag.compare("-d") == 0)
{
song.songPath.assign(arg.value);
}
if (flag.compare("-b") == 0)
{
song.id = atoi(value.c_str());
}
}
Download dnld{api};
dnld.downloadSong(token, song);
} }
void CommitManager::uploadSong() void CommitManager::uploadSong()
{ {
+2 -1
View File
@@ -15,7 +15,8 @@ namespace Models
std::string genre; std::string genre;
int year; int year;
int duration; int duration;
char* songData; char *songData;
std::string data;
std::string songPath; std::string songPath;
}; };
} }
+61
View File
@@ -1,5 +1,6 @@
#include"Download.h" #include"Download.h"
#include<exception>
#include<iostream> #include<iostream>
#include<fstream> #include<fstream>
@@ -7,24 +8,31 @@
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::exception;
using std::ofstream; using std::ofstream;
using std::string; using std::string;
using Models::API; using Models::API;
using Models::Song;
using Models::Token;
namespace Syncers namespace Syncers
{ {
#pragma
Download::Download() { } Download::Download() { }
Download::Download(API api) Download::Download(API api)
{ {
this->api = api; this->api = api;
this->api.endpoint = "song/data";
} }
Download::Download(string filePath) Download::Download(string filePath)
{ {
downloadFilePath = filePath; downloadFilePath = filePath;
} }
#pragma Constructors
#pragma
void Download::downloadSong(const int id) void Download::downloadSong(const int id)
{ {
string urlRoot = "http://192.168.1.5"; string urlRoot = "http://192.168.1.5";
@@ -45,4 +53,57 @@ namespace Syncers
cout<<r.header["content-type"]<<endl; cout<<r.header["content-type"]<<endl;
cout<<r.header["content-disposition"]<<endl; cout<<r.header["content-disposition"]<<endl;
} }
void Download::downloadSong(const Token token, Song song)
{
try
{
string url = retrieveUrl(song);
song.songPath.append("track.mp3");
cout<<"song path "<<song.songPath<<endl;
string auth{token.tokenType};
auth.append(" " + token.accessToken);
auto r = cpr::Get(cpr::Url(url),
cpr::Header{{"authorization", auth}});
int statusCode = r.status_code;
if (statusCode == OK)
{
song.data = r.text;
saveSong(&song);
}
cout<<"finsihed with status code "<<statusCode<<endl;
}
catch (exception e)
{
auto msg = e.what();
cout<<msg<<endl;
}
}
string Download::retrieveUrl(Song song)
{
string url{api.url + "api/" + api.version + "/" +
api.endpoint + "/"};
url.append(std::to_string(song.id));
cout<<"url "<<url<<endl;
return url;
}
void Download::saveSong(Song *song)
{
cout<<"\nSaving song to: "<<song->songPath<<endl;
int bufferLength = song->data.length();
const char *data = song->data.c_str();
cout<<"buff length "<<bufferLength<<endl;
ofstream saveSong{song->songPath, std::ios::binary};
saveSong.write(data, bufferLength);
saveSong.close();
}
#pragma Functions
} }
+5
View File
@@ -6,6 +6,7 @@
#include"Models/API.h" #include"Models/API.h"
#include"Models/Song.h" #include"Models/Song.h"
#include"Models/Token.h"
#include"SyncerBase.h" #include"SyncerBase.h"
@@ -19,8 +20,12 @@ namespace Syncers
Download(std::string); Download(std::string);
void downloadSong(int); void downloadSong(int);
void downloadSong(const Models::Token token, Models::Song);
private: private:
std::string retrieveUrl(Models::Song);
std::string downloadFilePath; std::string downloadFilePath;
void saveSong(Models::Song*);
}; };
} }
+3
View File
@@ -11,6 +11,9 @@ namespace Syncers
{ {
protected: protected:
Models::API api; Models::API api;
const int OK = 200;
const int UNAUTHORIZED = 401;
const int NOTFOUND = 404;
}; };
} }