Able to download song. Now to have something interface with the downloaded songs and play them.

This commit is contained in:
kdeng00
2019-12-17 21:08:28 -05:00
parent dcc2320332
commit 94bee732aa
7 changed files with 57 additions and 35 deletions
-12
View File
@@ -2,15 +2,3 @@
// Created by brahmix on 9/26/19. // Created by brahmix on 9/26/19.
// //
#include "Demo.hpp" #include "Demo.hpp"
+25 -6
View File
@@ -6,6 +6,7 @@
#define MEAR_DEMO_H #define MEAR_DEMO_H
#include <iostream> #include <iostream>
#include <fstream>
#include <string> #include <string>
#include <cstring> #include <cstring>
@@ -18,6 +19,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "manager/Tok.h" #include "manager/Tok.h"
#include "manager/DirectoryManager.h"
#include "model/APIInfo.h" #include "model/APIInfo.h"
#include "model/CoverArt.h" #include "model/CoverArt.h"
#include "model/Song.h" #include "model/Song.h"
@@ -70,6 +72,7 @@ Song ObjToSong(JE *env, SongObj obj) {
auto songFilenameId = env->GetFieldID(songClass, "filename", "Ljava/lang/String;"); auto songFilenameId = env->GetFieldID(songClass, "filename", "Ljava/lang/String;");
auto songDownloadedId = env->GetFieldID(songClass, "downloaded", "Z"); auto songDownloadedId = env->GetFieldID(songClass, "downloaded", "Z");
auto songIdVal = env->GetIntField(obj, songId);
auto titleVal = (jstring)env->GetObjectField(obj, songTitle); auto titleVal = (jstring)env->GetObjectField(obj, songTitle);
auto albumVal = (jstring)env->GetObjectField(obj, songAlbum); auto albumVal = (jstring)env->GetObjectField(obj, songAlbum);
auto albumArtistVal = (jstring)env->GetObjectField(obj, songAlbumArtist); auto albumArtistVal = (jstring)env->GetObjectField(obj, songAlbumArtist);
@@ -83,6 +86,7 @@ Song ObjToSong(JE *env, SongObj obj) {
Song song; Song song;
song.id = songIdVal;
song.title = env->GetStringUTFChars(titleVal, nullptr); song.title = env->GetStringUTFChars(titleVal, nullptr);
song.artist = env->GetStringUTFChars(songArtistVal, nullptr); song.artist = env->GetStringUTFChars(songArtistVal, nullptr);
song.album = env->GetStringUTFChars(albumVal, nullptr); song.album = env->GetStringUTFChars(albumVal, nullptr);
@@ -91,8 +95,8 @@ Song ObjToSong(JE *env, SongObj obj) {
song.duration = songDurationVal; song.duration = songDurationVal;
song.year = songYearVal; song.year = songYearVal;
song.coverArtId = songCoverArtIdVal; song.coverArtId = songCoverArtIdVal;
song.path = env->GetStringUTFChars(songPathVal, nullptr); song.path = (songPathVal == nullptr) ? "" : env->GetStringUTFChars(songPathVal, nullptr);
song.filename = env->GetStringUTFChars(songFilenameVal, nullptr); song.filename = (songFilenameVal == nullptr) ? "" : env->GetStringUTFChars(songFilenameVal, nullptr);
return song; return song;
} }
@@ -255,8 +259,24 @@ void updateShuffleMode(const std::string& path) {
template<class Song = model::Song, class Token = model::Token, typename Str = std::string> template<class Song = model::Song, class Token = model::Token, typename Str = std::string>
void downloadSong(Song& song, const Token& token, const Str& path) { void downloadSong(Song& song, const Token& token, const Str& path) {
song.filename = song.title;
song.path = path;
repository::local::APIRepository apiRepo;
auto apiInfo = apiRepo.retrieveAPIInfo(path);
repository::SongRepository songRepo; repository::SongRepository songRepo;
// songRepo.downloadSong(); auto downloadedSong = songRepo.downloadSong(token, song, apiInfo);
manager::DirectoryManager dirMgr;
if (dirMgr.doesSongExist(song, path)) {
auto a = 0;
std::cout << "song already exists\n";
return;
}
dirMgr.createSongDirectory(song, path);
const auto fullSongPath = dirMgr.fullSongPath(song, path);
std::fstream saveSong(fullSongPath, std::ios::out | std::ios::binary);
saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size());
saveSong.close();
} }
@@ -666,7 +686,7 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord(
extern "C" extern "C"
JNIEXPORT void JNIEXPORT void
JNICALL JNICALL
Java_com_example_mear_repositories_TrackRepositories_downloadSong( Java_com_example_mear_repositories_TrackRepository_downloadSong(
JNIEnv *env, JNIEnv *env,
jobject thisObj, jobject thisObj,
jobject tokenObj, jobject tokenObj,
@@ -681,8 +701,7 @@ Java_com_example_mear_repositories_TrackRepositories_downloadSong(
auto song = ObjToSong<jobject, JNIEnv>(env, songObj); auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
auto path = env->GetStringUTFChars(pathStr, nullptr); auto path = env->GetStringUTFChars(pathStr, nullptr);
downloadSong(song, token, path);
} }
extern "C" extern "C"
+14 -11
View File
@@ -21,43 +21,46 @@ namespace manager {
class DirectoryManager { class DirectoryManager {
public: public:
template<typename Str = std::string> template<typename Str = std::string>
Str fullSongPath(const Song& song, const Str& path) { std::string fullSongPath(const Song& song, const Str& path) {
auto s = utility::GeneralUtility::appendForwardSlashToUri(path); std::string s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
s.append(song.albumArtist); s.append(song.albumArtist);
s.append("/"); s.append("/");
s.append(song.album); s.append(song.album);
s.append("/"); s.append("/");
s.append(song.filename); s.append(song.filename);
s.append(".mp3");
return s; return s;
} }
template<typename Str = std::string> template<typename Str = std::string>
Str albumPath(const Song& song, const Str& path) { Str albumPath(const Song& song, const Str& path) {
auto s = utility::GeneralUtility::appendForwardSlashToUri(path); std::string s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
s.append(song.albumArtist); s.append(song.albumArtist);
s.append("/"); s.append("/");
s.append(song.album); s.append(song.album);
return s; auto ss = s;
return ss.c_str();
} }
template<typename Str = std::string> template<typename Str = std::string>
Str artistPath(const Song& song, const Str& path) { Str artistPath(const Song& song, const Str& path) {
auto s = utility::GeneralUtility::appendForwardSlashToUri(path); Str s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
s.append(song.albumArtist); s.append(song.albumArtist);
return s; return s.c_str();
} }
template<typename Str = std::string, typename B = bool> template<typename Str = std::string, typename B = bool>
B doesSongExist(const Song& song, const Str& path) { B doesSongExist(const Song& song, const Str& path) {
auto s = albumPath(song, path); std::string s = albumPath(song, path);
s.append("/"); s.append("/");
s.append(song.filename); s.append(song.filename);
stat buffer; struct stat buffer;
return (stat (s.c_str(), &buffer) == 0); return (stat (s.c_str(), &buffer) == 0);
} }
@@ -77,7 +80,7 @@ namespace manager {
template<typename Str = std::string> template<typename Str = std::string>
void createSongDirectory(const Song& song, const Str& path) { void createSongDirectory(const Song& song, const Str& path) {
if (!artistDirectoryExists(song, path)) { if (!artistDirectoryExists(song, path)) {
auto status = mkdir(artistPath(song, path).c_str(), 0777); auto status = mkdir(artistPath<std::string>(song, path).c_str(), 0777);
} }
if (!albumDirectoryExists(song, path)) { if (!albumDirectoryExists(song, path)) {
auto status = mkdir(albumPath(song, path), 0777); auto status = mkdir(albumPath(song, path), 0777);
@@ -91,7 +94,7 @@ namespace manager {
private: private:
template<typename Str = std::string, typename B = bool> template<typename Str = std::string, typename B = bool>
B albumDirectoryExists(const Song song, const Str& path) { B albumDirectoryExists(const Song song, const Str& path) {
auto albumPath = utility::GeneralUtility::appendForwardSlashToUri(path); auto albumPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
albumPath.append(song.albumArtist); albumPath.append(song.albumArtist);
albumPath.append("/"); albumPath.append("/");
albumPath.append(song.album); albumPath.append(song.album);
@@ -109,7 +112,7 @@ namespace manager {
template<typename Str = std::string, typename B = bool> template<typename Str = std::string, typename B = bool>
B artistDirectoryExists(const Song song, const Str& path) { B artistDirectoryExists(const Song song, const Str& path) {
auto artistPath = utility::GeneralUtility::appendForwardSlashToUri(path); auto artistPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
artistPath.append(song.albumArtist); artistPath.append(song.albumArtist);
DIR* dir = opendir(artistPath.c_str()); DIR* dir = opendir(artistPath.c_str());
+10 -4
View File
@@ -122,9 +122,9 @@ namespace repository {
return sng; return sng;
} }
template<typename Token = model::Token> template<typename Token = model::Token, typename API = model::APIInfo>
Song downloadSong(const Token &token, const Song song, const std::string& uri) { Song downloadSong(const Token &token, const Song song, const API& uri) {
auto fullUri = utility::GeneralUtility::appendForwardSlashToUri(uri); auto fullUri = utility::GeneralUtility::appendForwardSlashToUri(uri.uri);
fullUri.append(songDownloadEndpoint()); fullUri.append(songDownloadEndpoint());
fullUri.append(std::to_string(song.id)); fullUri.append(std::to_string(song.id));
Song downloadedSong; Song downloadedSong;
@@ -146,7 +146,13 @@ namespace repository {
auto res = curl_easy_perform(curl); auto res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end())); downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end()));
downloadedSong.filename = downloadedSong.title; downloadedSong.filename = "track";
if (song.id < 10) {
downloadedSong.filename.append("0");
downloadedSong.filename.append(std::to_string(song.id));
} else {
downloadedSong.filename.append(std::to_string(song.id));
}
downloadedSong.filename.append(".mp3"); downloadedSong.filename.append(".mp3");
return downloadedSong; return downloadedSong;
+1 -1
View File
@@ -14,7 +14,7 @@ namespace utility {
public: public:
template<typename S = std::string> template<typename S = std::string>
static S appendForwardSlashToUri(const S& uri) { static S appendForwardSlashToUri(const S& uri) {
S fullUri(uri); std::string fullUri(uri);
if (fullUri.at(fullUri.size() - 1) != '/') { if (fullUri.at(fullUri.size() - 1) != '/') {
fullUri.append("/"); fullUri.append("/");
} }
@@ -378,6 +378,7 @@ class MainActivity : BaseServiceActivity() {
val token = tokenRepo.retrieveToken(appPath) val token = tokenRepo.retrieveToken(appPath)
val apiInfo = apiRepo.retrieveRecord(appPath) val apiInfo = apiRepo.retrieveRecord(appPath)
trackRepo.download(token, song, appPath)
} }
R.id.action_song_play_count-> { R.id.action_song_play_count-> {
val trk = musicService!!.getCurrentSong() val trk = musicService!!.getCurrentSong()
@@ -22,7 +22,7 @@ class TrackRepository(var context: Context? = null) {
private external fun retrieveSong(token: Token, song: Song, uri: String): Song private external fun retrieveSong(token: Token, song: Song, uri: String): Song
private external fun downloadSong(token: Token, song: Song, uri: String) private external fun downloadSong(token: Token, song: Song, path: String)
fun fetchSongs(token: Token, uri: String): Array<Song> { fun fetchSongs(token: Token, uri: String): Array<Song> {
@@ -38,6 +38,11 @@ class TrackRepository(var context: Context? = null) {
downloadSong(token, song, uri) downloadSong(token, song, uri)
} }
fun download(token: Token, song: Song, path: String) {
downloadSong(token, song, path)
val s = 4
}
fun insertTracks(tracks: List<Track>) = context!!.database.use { fun insertTracks(tracks: List<Track>) = context!!.database.use {
transaction { transaction {