Able to download song. Now to have something interface with the downloaded songs and play them.
This commit is contained in:
@@ -2,15 +2,3 @@
|
||||
// Created by brahmix on 9/26/19.
|
||||
//
|
||||
#include "Demo.hpp"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define MEAR_DEMO_H
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
@@ -18,6 +19,7 @@
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "manager/Tok.h"
|
||||
#include "manager/DirectoryManager.h"
|
||||
#include "model/APIInfo.h"
|
||||
#include "model/CoverArt.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 songDownloadedId = env->GetFieldID(songClass, "downloaded", "Z");
|
||||
|
||||
auto songIdVal = env->GetIntField(obj, songId);
|
||||
auto titleVal = (jstring)env->GetObjectField(obj, songTitle);
|
||||
auto albumVal = (jstring)env->GetObjectField(obj, songAlbum);
|
||||
auto albumArtistVal = (jstring)env->GetObjectField(obj, songAlbumArtist);
|
||||
@@ -83,6 +86,7 @@ Song ObjToSong(JE *env, SongObj obj) {
|
||||
|
||||
|
||||
Song song;
|
||||
song.id = songIdVal;
|
||||
song.title = env->GetStringUTFChars(titleVal, nullptr);
|
||||
song.artist = env->GetStringUTFChars(songArtistVal, nullptr);
|
||||
song.album = env->GetStringUTFChars(albumVal, nullptr);
|
||||
@@ -91,8 +95,8 @@ Song ObjToSong(JE *env, SongObj obj) {
|
||||
song.duration = songDurationVal;
|
||||
song.year = songYearVal;
|
||||
song.coverArtId = songCoverArtIdVal;
|
||||
song.path = env->GetStringUTFChars(songPathVal, nullptr);
|
||||
song.filename = env->GetStringUTFChars(songFilenameVal, nullptr);
|
||||
song.path = (songPathVal == nullptr) ? "" : env->GetStringUTFChars(songPathVal, nullptr);
|
||||
song.filename = (songFilenameVal == nullptr) ? "" : env->GetStringUTFChars(songFilenameVal, nullptr);
|
||||
|
||||
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>
|
||||
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;
|
||||
// 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"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepositories_downloadSong(
|
||||
Java_com_example_mear_repositories_TrackRepository_downloadSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject tokenObj,
|
||||
@@ -681,8 +701,7 @@ Java_com_example_mear_repositories_TrackRepositories_downloadSong(
|
||||
|
||||
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
|
||||
auto path = env->GetStringUTFChars(pathStr, nullptr);
|
||||
|
||||
|
||||
downloadSong(song, token, path);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
|
||||
@@ -21,43 +21,46 @@ namespace manager {
|
||||
class DirectoryManager {
|
||||
public:
|
||||
template<typename Str = std::string>
|
||||
Str fullSongPath(const Song& song, const Str& path) {
|
||||
auto s = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
std::string fullSongPath(const Song& song, const Str& path) {
|
||||
std::string s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
s.append(song.albumArtist);
|
||||
s.append("/");
|
||||
s.append(song.album);
|
||||
s.append("/");
|
||||
s.append(song.filename);
|
||||
s.append(".mp3");
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<typename Str = std::string>
|
||||
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("/");
|
||||
s.append(song.album);
|
||||
|
||||
return s;
|
||||
auto ss = s;
|
||||
|
||||
return ss.c_str();
|
||||
}
|
||||
|
||||
template<typename Str = std::string>
|
||||
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);
|
||||
|
||||
return s;
|
||||
return s.c_str();
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B doesSongExist(const Song& song, const Str& path) {
|
||||
auto s = albumPath(song, path);
|
||||
std::string s = albumPath(song, path);
|
||||
s.append("/");
|
||||
s.append(song.filename);
|
||||
|
||||
stat buffer;
|
||||
struct stat buffer;
|
||||
return (stat (s.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
@@ -77,7 +80,7 @@ namespace manager {
|
||||
template<typename Str = std::string>
|
||||
void createSongDirectory(const Song& song, const Str& 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)) {
|
||||
auto status = mkdir(albumPath(song, path), 0777);
|
||||
@@ -91,7 +94,7 @@ namespace manager {
|
||||
private:
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
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("/");
|
||||
albumPath.append(song.album);
|
||||
@@ -109,7 +112,7 @@ namespace manager {
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
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);
|
||||
DIR* dir = opendir(artistPath.c_str());
|
||||
|
||||
|
||||
@@ -122,9 +122,9 @@ namespace repository {
|
||||
return sng;
|
||||
}
|
||||
|
||||
template<typename Token = model::Token>
|
||||
Song downloadSong(const Token &token, const Song song, const std::string& uri) {
|
||||
auto fullUri = utility::GeneralUtility::appendForwardSlashToUri(uri);
|
||||
template<typename Token = model::Token, typename API = model::APIInfo>
|
||||
Song downloadSong(const Token &token, const Song song, const API& uri) {
|
||||
auto fullUri = utility::GeneralUtility::appendForwardSlashToUri(uri.uri);
|
||||
fullUri.append(songDownloadEndpoint());
|
||||
fullUri.append(std::to_string(song.id));
|
||||
Song downloadedSong;
|
||||
@@ -146,7 +146,13 @@ namespace repository {
|
||||
auto res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
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");
|
||||
|
||||
return downloadedSong;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace utility {
|
||||
public:
|
||||
template<typename S = std::string>
|
||||
static S appendForwardSlashToUri(const S& uri) {
|
||||
S fullUri(uri);
|
||||
std::string fullUri(uri);
|
||||
if (fullUri.at(fullUri.size() - 1) != '/') {
|
||||
fullUri.append("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user