Working on downloading the song

This commit is contained in:
kdeng00
2019-08-25 16:47:52 -04:00
parent 540d665a7e
commit fb03354189
5 changed files with 92 additions and 8 deletions
+1
View File
@@ -18,6 +18,7 @@ CREATE TABLE Song (
Genre TEXT NOT NULL, Genre TEXT NOT NULL,
Year INT NOT NULL, Year INT NOT NULL,
Duration INT NOT NULL, Duration INT NOT NULL,
SongPath TEXT NOT NULL,
CoverArtId INT NOT NULL, CoverArtId INT NOT NULL,
PRIMARY KEY (SongId), PRIMARY KEY (SongId),
+1 -1
View File
@@ -12,7 +12,7 @@ class directory_manager
{ {
public: public:
std::string create_directory_process(Song, const std::string&); static std::string create_directory_process(Song, const std::string&);
static std::string configPath(std::string_view); static std::string configPath(std::string_view);
static std::string contentOfPath(std::string_view); static std::string contentOfPath(std::string_view);
+60
View File
@@ -1,12 +1,15 @@
#ifndef SONGCONTROLLER_H_ #ifndef SONGCONTROLLER_H_
#define SONGCONTROLLER_H_ #define SONGCONTROLLER_H_
#include <filesystem>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "oatpp/core/data/stream/ChunkedBuffer.hpp"
#include "oatpp/core/data/stream/FileStream.hpp"
#include "oatpp/core/macro/codegen.hpp" #include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp" #include "oatpp/core/macro/component.hpp"
#include "oatpp/web/mime/multipart/InMemoryPartReader.hpp" #include "oatpp/web/mime/multipart/InMemoryPartReader.hpp"
@@ -19,6 +22,9 @@
#include "managers/token_manager.h" #include "managers/token_manager.h"
#include "models/models.h" #include "models/models.h"
#include "types/scopes.h" #include "types/scopes.h"
#include "types/songFilter.h"
namespace fs = std::filesystem;
class songController : public oatpp::web::server::api::ApiController class songController : public oatpp::web::server::api::ApiController
{ {
@@ -95,6 +101,60 @@ public:
return createDtoResponse(Status::CODE_200, songs); return createDtoResponse(Status::CODE_200, songs);
} }
// endpoint for retrieving song record by the song id in json format
ENDPOINT("GET", "/api/v1/song/{id}", songRecord,
PATH(Int32, id)) {
songRepository songRepo(exe_path);
Song songDb;
songDb.id = id;
songDb = songRepo.retrieveRecord(songDb, songFilter::id);
auto song = songDto::createShared();
song->id = songDb.id;
song->title = songDb.title.c_str();
song->artist = songDb.artist.c_str();
song->album = songDb.album.c_str();
song->genre = songDb.genre.c_str();
song->year = songDb.year;
song->duration = songDb.duration;
return createDtoResponse(Status::CODE_200, song);
}
// TODO: left off here
ENDPOINT("GET", "/api/v1/song/data/{id}", downloadSong,
PATH(Int32, id)) {
songRepository songRepo(exe_path);
Song songDb;
songDb.id = id;
songDb = songRepo.retrieveRecord(songDb, songFilter::id);
std::cout << "constructing FileInputStream" << std::endl;
oatpp::data::stream::FileInputStream file(songDb.songPath.c_str());
//oatpp::data::stream::FileOutputStream file(songDb.songPath.c_str());
auto songPath = fs::path(songDb.songPath);
auto songSize = fs::file_size(songPath);
std::cout << "prepping data" << std::endl;
auto data = new char[songSize];
std::cout << "data will be loaded" << std::endl;
auto byteCount = file.read(&data, songSize);
//auto byteCount = file.write(&data, songSize);
std::cout << byteCount << " bytes read" << std::endl;
auto songData = oatpp::data::stream::ChunkedBuffer::createShared();
songData->write(data, byteCount);
delete[] data;
std::cout << "sending file" << std::endl;
return createResponse(Status::CODE_200, songData);
}
#include OATPP_CODEGEN_END(ApiController) #include OATPP_CODEGEN_END(ApiController)
private: private:
std::string exe_path; std::string exe_path;
+16 -5
View File
@@ -65,11 +65,11 @@ void songRepository::saveRecord(const Song& song)
MYSQL_STMT *stmt = mysql_stmt_init(conn); MYSQL_STMT *stmt = mysql_stmt_init(conn);
std::string query = "INSERT INTO Song(Title, Artist, Album, Genre, "; std::string query = "INSERT INTO Song(Title, Artist, Album, Genre, ";
query.append("Year, Duration, CoverArtId) VALUES(?, ?, ?, ?, ?, ?, ?)"); query.append("Year, Duration, SongPath, CoverArtId) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
status = mysql_stmt_prepare(stmt, query.c_str(), query.size()); status = mysql_stmt_prepare(stmt, query.c_str(), query.size());
MYSQL_BIND params[7]; MYSQL_BIND params[8];
memset(params, 0, sizeof(params)); memset(params, 0, sizeof(params));
params[0].buffer_type = MYSQL_TYPE_STRING; params[0].buffer_type = MYSQL_TYPE_STRING;
@@ -106,10 +106,16 @@ void songRepository::saveRecord(const Song& song)
params[5].length = 0; params[5].length = 0;
params[5].is_null = 0; params[5].is_null = 0;
params[6].buffer_type = MYSQL_TYPE_LONG; params[6].buffer_type = MYSQL_TYPE_STRING;
params[6].buffer = (char*)&song.coverArtId; params[6].buffer = (char*)song.songPath.c_str();
params[6].length = 0; auto pathLength = song.songPath.size();
params[6].length = &pathLength;
params[6].is_null = 0; params[6].is_null = 0;
params[7].buffer_type = MYSQL_TYPE_LONG;
params[7].buffer = (char*)&song.coverArtId;
params[7].length = 0;
params[7].is_null = 0;
status = mysql_stmt_bind_param(stmt, params); status = mysql_stmt_bind_param(stmt, params);
status = mysql_stmt_execute(stmt); status = mysql_stmt_execute(stmt);
@@ -155,6 +161,8 @@ std::vector<Song> songRepository::parseRecords(MYSQL_RES* results)
song.duration = std::stoi(row[i]); song.duration = std::stoi(row[i]);
break; break;
case 7: case 7:
song.songPath = row[i];
case 8:
song.coverArtId = std::stoi(row[i]); song.coverArtId = std::stoi(row[i]);
break; break;
} }
@@ -197,6 +205,9 @@ Song songRepository::parseRecord(MYSQL_RES* results)
song.duration = std::stoi(row[i]); song.duration = std::stoi(row[i]);
break; break;
case 7: case 7:
song.songPath = row[i];
break;
case 8:
song.coverArtId = std::stoi(row[i]); song.coverArtId = std::stoi(row[i]);
} }
} }
+14 -2
View File
@@ -27,7 +27,9 @@ void song_manager::saveSong(Song& song)
song.data = std::move(data); song.data = std::move(data);
coverArtManager covMgr(exe_path); coverArtManager covMgr(exe_path);
auto coverRootPath = directory_manager::pathConfigContent(exe_path)["cover_root_path"].get<std::string>(); auto pathConfigContent = directory_manager::pathConfigContent(exe_path);
auto coverRootPath = pathConfigContent["cover_root_path"].get<std::string>();
auto musicRootPath = pathConfigContent["root_music_path"].get<std::string>();
auto stockCoverPath = directory_manager::configPath(exe_path); auto stockCoverPath = directory_manager::configPath(exe_path);
stockCoverPath.append("/CoverArt.png"); stockCoverPath.append("/CoverArt.png");
@@ -35,6 +37,16 @@ void song_manager::saveSong(Song& song)
auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath); auto cov = covMgr.saveCover(song, coverRootPath, stockCoverPath);
song.coverArtId = cov.id; song.coverArtId = cov.id;
auto songPath = directory_manager::create_directory_process(song, musicRootPath);
songPath.append(song.title);
songPath.append(".mp3");
std::cout << std::endl;
std::cout << "temp path: " << song.songPath << std::endl;
std::cout << "new path: " << songPath << std::endl;
fs::copy(song.songPath, songPath);
fs::remove(song.songPath);
song.songPath = std::move(songPath);
printSong(song); printSong(song);
songRepository songRepo(exe_path); songRepository songRepo(exe_path);
@@ -43,7 +55,7 @@ void song_manager::saveSong(Song& song)
void song_manager::printSong(const Song& song) void song_manager::printSong(const Song& song)
{ {
std::cout << "song" << std::endl; std::cout << std::endl << "song" << std::endl;
std::cout << "title: " << song.title << std::endl; std::cout << "title: " << song.title << std::endl;
std::cout << "artist: " << song.artist << std::endl; std::cout << "artist: " << song.artist << std::endl;
std::cout << "album: " << song.album << std::endl; std::cout << "album: " << song.album << std::endl;