Merge pull request #9 from kdeng00/support_new_upload_endpoint
Support new upload endpoint
This commit was merged in pull request #9.
This commit is contained in:
+22
-2
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.12)
|
cmake_minimum_required(VERSION 3.14)
|
||||||
|
|
||||||
|
|
||||||
project(IcarusDownloadManager)
|
project(IcarusDownloadManager)
|
||||||
@@ -9,6 +9,20 @@ message(STATUS "Checking compiler flags for C++17 support.")
|
|||||||
# Set C++17 support flags for various compilers
|
# Set C++17 support flags for various compilers
|
||||||
include(CheckCXXCompilerFlag)
|
include(CheckCXXCompilerFlag)
|
||||||
|
|
||||||
|
if(WIN32)
|
||||||
|
message("Windows")
|
||||||
|
set(vs_ver 19.29.30138)
|
||||||
|
|
||||||
|
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL ${vs_ver})
|
||||||
|
message("Visual Studio version is at least ${vs_ver}")
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD_COMPILE_OPTION "-std:c++latest")
|
||||||
|
set(CMAKE_CXX_EXTENSION_COMPILE_OPTION "-std:c++latest")
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
|
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
|
||||||
check_cxx_compiler_flag("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
|
check_cxx_compiler_flag("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
|
||||||
|
|
||||||
@@ -33,6 +47,7 @@ elseif(COMPILER_SUPPORTS_CXX0X)
|
|||||||
else()
|
else()
|
||||||
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
|
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
|
||||||
endif()
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
@@ -42,6 +57,7 @@ set(SOURCES
|
|||||||
src/Managers/FileManager.cpp
|
src/Managers/FileManager.cpp
|
||||||
src/Managers/TokenManager.cpp
|
src/Managers/TokenManager.cpp
|
||||||
src/Managers/UserManager.cpp
|
src/Managers/UserManager.cpp
|
||||||
|
src/Models/Song.cpp
|
||||||
src/Parsers/APIParser.cpp
|
src/Parsers/APIParser.cpp
|
||||||
src/Syncers/Delete.cpp
|
src/Syncers/Delete.cpp
|
||||||
src/Syncers/Download.cpp
|
src/Syncers/Download.cpp
|
||||||
@@ -54,7 +70,11 @@ set(IDM_INCLUDE_DIR
|
|||||||
"${CMAKE_CURRENT_SOURCE_DIR}/include")
|
"${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||||
|
|
||||||
# conan
|
# conan
|
||||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
set(CONAN_BUILDINFO
|
||||||
|
${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||||
|
|
||||||
|
message("conan build info ${CONAN_BUILDINFO}")
|
||||||
|
include(${CONAN_BUILDINFO})
|
||||||
conan_basic_setup()
|
conan_basic_setup()
|
||||||
|
|
||||||
include_directories(${CPR_INCLUDE_DIRS} ${IDM_INCLUDE_DIR})
|
include_directories(${CPR_INCLUDE_DIRS} ${IDM_INCLUDE_DIR})
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ IcarusDownloadManager is a Linux CLI software client application that has the fe
|
|||||||
|
|
||||||
* C++ with C++17 features
|
* C++ with C++17 features
|
||||||
* CMake
|
* CMake
|
||||||
* GCC >= 9 or Visual Studio 2019
|
* GCC >= 9 or Visual Studio >= 16 [2019]
|
||||||
* [conan](https://github.com/conan-io/conan)
|
* [conan](https://github.com/conan-io/conan)
|
||||||
* [json](https://github.com/nlohmann/json)
|
* [json](https://github.com/nlohmann/json)
|
||||||
* [openssl](https://github.com/openssl/openssl)
|
* [openssl](https://github.com/openssl/openssl)
|
||||||
@@ -28,23 +28,48 @@ cd build
|
|||||||
|
|
||||||
conan install .. --build
|
conan install .. --build
|
||||||
|
|
||||||
cmake -DCMAKE_BUILD_TYPE=RELEASE
|
cmake ..
|
||||||
cmake --build . -j
|
cmake --build . --config release -j
|
||||||
```
|
```
|
||||||
|
|
||||||
The program has been built and can be executed by the binary file *icd*. For information on how to use icd, merely execute the program without any command line arguments.
|
The program has been built and can be executed by the binary file *icd*. For information on how to use icd, merely execute the program without any command line arguments.
|
||||||
|
|
||||||
### Downloading Song
|
### Downloading Song
|
||||||
``icd download -u spacecadet -p stellar40 -h https://icarus.com -b 15``
|
|
||||||
|
```BASH
|
||||||
|
icd download -u spacecadet -p stellar40 -h https://icarus.com -b 15
|
||||||
|
```
|
||||||
|
|
||||||
### Uploading Song
|
### Uploading Song
|
||||||
``icd upload -u spacecadet -p stellar40 -h https://icarus.com -s /path/of/song.mp3``
|
|
||||||
|
```BASH
|
||||||
|
icd upload -u spacecadet -p stellar40 -h https://icarus.com -s /path/of/song.mp3
|
||||||
|
```
|
||||||
|
|
||||||
|
### Uploading Song with metadata
|
||||||
|
|
||||||
|
```BASH
|
||||||
|
icd upload-meta -u spacecadet -p stellar40 -h https://icarus.com -s /path/of/song.mp3 -t 1 -m /path/to/metadata/config/collection.json -ca /path/to/cover/art/image.png
|
||||||
|
```
|
||||||
|
|
||||||
|
### Uploading Song with metadata from directory
|
||||||
|
|
||||||
|
```BASH
|
||||||
|
icd upload-meta -u spacecadet -p stellar40 -h https://icarus.com -smca /path/where/songs/and/metadata/exists/
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Retrieving Song in json
|
### Retrieving Song in json
|
||||||
``icd retrieve -u spacecadet -p stellar40 -h https://icarus.com -rt songs``
|
|
||||||
|
```Bash
|
||||||
|
icd retrieve -u spacecadet -p stellar40 -h https://icarus.com -rt songs
|
||||||
|
```
|
||||||
|
|
||||||
### Deleting Song
|
### Deleting Song
|
||||||
``icd delete -u spacecadet -p stellar40 -h https://icarus.com -D 15``
|
|
||||||
|
```BASH
|
||||||
|
icd delete -u spacecadet -p stellar40 -h https://icarus.com -D 15
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
@@ -53,6 +78,7 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the code of conduc
|
|||||||
|
|
||||||
## Versioning
|
## Versioning
|
||||||
|
|
||||||
|
[v0.2.0](https://github.com/kdeng00/IcarusDownloadManager/releases/tag/v0.2.0)
|
||||||
[v0.1.1](https://github.com/kdeng00/IcarusDownloadManager/releases/tag/v0.1.1)
|
[v0.1.1](https://github.com/kdeng00/IcarusDownloadManager/releases/tag/v0.1.1)
|
||||||
[v0.1.0](https://github.com/kdeng00/IcarusDownloadManager/releases/tag/0.1.0)
|
[v0.1.0](https://github.com/kdeng00/IcarusDownloadManager/releases/tag/0.1.0)
|
||||||
|
|
||||||
|
|||||||
@@ -6,3 +6,15 @@ cpr/1.7.2
|
|||||||
|
|
||||||
[generators]
|
[generators]
|
||||||
cmake
|
cmake
|
||||||
|
|
||||||
|
[options]
|
||||||
|
openssl:shared=False
|
||||||
|
libcurl:shared=False
|
||||||
|
cpr:shared=False
|
||||||
|
libcurl:with_ssl=openssl
|
||||||
|
libcurl:with_ftp=False
|
||||||
|
libcurl:with_gopher=False
|
||||||
|
libcurl:with_imap=False
|
||||||
|
libcurl:with_pop3=False
|
||||||
|
libcurl:with_smb=False
|
||||||
|
libcurl:with_smtp=False
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef ACTIONMANAGER_H_
|
#ifndef ACTIONMANAGER_H_
|
||||||
#define ACTIONMANAGER_H_
|
#define ACTIONMANAGER_H_
|
||||||
|
|
||||||
|
#include<algorithm>
|
||||||
#include<string>
|
#include<string>
|
||||||
#include<string_view>
|
#include<string_view>
|
||||||
#include<array>
|
#include<array>
|
||||||
@@ -12,6 +13,8 @@
|
|||||||
namespace Managers
|
namespace Managers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ActionManager
|
class ActionManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -19,19 +22,64 @@ public:
|
|||||||
|
|
||||||
Models::IcarusAction retrieveIcarusAction() const;
|
Models::IcarusAction retrieveIcarusAction() const;
|
||||||
private:
|
private:
|
||||||
constexpr std::array<const char*, 12> supportedFlags() noexcept;
|
constexpr std::array<const char*, 16> supportedFlags() noexcept
|
||||||
constexpr std::array<const char*, 4> supportedActions() noexcept;
|
{
|
||||||
|
constexpr std::array<const char*, 16> allFlags{"-u", "-p", "-t", "-h", "-s",
|
||||||
|
"-sd", "-sr", "-d", "-D", "-b", "-rt", "-nc",
|
||||||
|
"-m", "-ca", "-smca", "-t"};
|
||||||
|
|
||||||
bool isNumber(const std::string_view) noexcept;
|
return allFlags;
|
||||||
|
}
|
||||||
|
constexpr std::array<const char*, 4> supportedActions() noexcept;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void validateFlags();
|
void validateFlags();
|
||||||
|
// Checks to see if the flag is valid
|
||||||
|
template<typename Str>
|
||||||
|
bool isValidFlag(const Str flag)
|
||||||
|
{
|
||||||
|
const auto flags = supportedFlags();
|
||||||
|
const auto i = std::find_if(flags.begin(), flags.end(), [&](const Str &f)
|
||||||
|
{
|
||||||
|
return f.compare(flag) == 0 ? true : false;
|
||||||
|
});
|
||||||
|
|
||||||
std::vector<std::string> parsedFlags();
|
auto result = i != flags.end() ? true : false;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Str>
|
||||||
|
bool doesFlagHaveValue(const Str flag)
|
||||||
|
{
|
||||||
|
const auto flags = parsedFlags();
|
||||||
|
auto i = std::find_if(flags.begin(), flags.end(), [&](const Str &f)
|
||||||
|
{
|
||||||
|
return f.compare(flag) == 0 ? true : false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (i != flags.end())
|
||||||
|
{
|
||||||
|
if (++i != flags.end() && !isValidFlag<Str>(*i))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void printAction() noexcept;
|
void printAction() noexcept;
|
||||||
void printFlags() noexcept;
|
void printFlags() noexcept;
|
||||||
|
|
||||||
|
std::vector<std::string> parsedFlags();
|
||||||
|
|
||||||
std::string action;
|
std::string action;
|
||||||
|
|
||||||
std::vector<Models::Flags> flags;
|
std::vector<Models::Flags> flags;
|
||||||
|
|||||||
@@ -2,11 +2,15 @@
|
|||||||
#define COMMITMANAGER_H_
|
#define COMMITMANAGER_H_
|
||||||
|
|
||||||
#include<map>
|
#include<map>
|
||||||
|
#include<iostream>
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include<string_view>
|
||||||
|
|
||||||
#include"Models/API.h"
|
#include"Models/API.h"
|
||||||
#include"Models/Token.h"
|
|
||||||
#include"Models/IcarusAction.h"
|
#include"Models/IcarusAction.h"
|
||||||
|
#include"Models/Song.h"
|
||||||
|
#include"Models/Token.h"
|
||||||
|
#include"Utilities/Checks.h"
|
||||||
|
|
||||||
namespace Managers
|
namespace Managers
|
||||||
{
|
{
|
||||||
@@ -18,32 +22,192 @@ public:
|
|||||||
|
|
||||||
void commitAction();
|
void commitAction();
|
||||||
|
|
||||||
|
|
||||||
enum class RetrieveTypes
|
enum class RetrieveTypes
|
||||||
{
|
{
|
||||||
songs
|
songs
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Used for parsing songs from the metadata file
|
||||||
|
class Album
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Album() = default;
|
||||||
|
|
||||||
|
void printInfo();
|
||||||
|
|
||||||
|
|
||||||
|
std::string album;
|
||||||
|
std::string albumArtist;
|
||||||
|
std::string genre;
|
||||||
|
int year;
|
||||||
|
int trackCount;
|
||||||
|
int discCount;
|
||||||
|
std::vector<Models::Song> songs;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class ActionValues;
|
enum class ActionValues;
|
||||||
|
|
||||||
std::map<std::string, ActionValues> mapActions() noexcept;
|
std::map<std::string, ActionValues> mapActions() noexcept;
|
||||||
|
|
||||||
Models::Token parseToken(Models::API);
|
|
||||||
|
|
||||||
void deleteSong();
|
void deleteSong();
|
||||||
void downloadSong();
|
void downloadSong();
|
||||||
void retrieveObjects();
|
void retrieveObjects();
|
||||||
void uploadSong();
|
void uploadSong();
|
||||||
|
|
||||||
|
Models::Token parseToken(Models::API);
|
||||||
|
|
||||||
|
// Uploads a single song. The song is constructed from a metadata file that contains
|
||||||
|
// information about the album the song is from. Also, the cover art of the song must
|
||||||
|
// be present.
|
||||||
|
//
|
||||||
|
// Expects
|
||||||
|
// * Song - mp3 file path
|
||||||
|
// * TrackID - track number to chose from when retrieving metadata. "1" and "1:1" are similar
|
||||||
|
// * Metadata - Source file containing metadata of the song
|
||||||
|
// * Cover art - path to image cover art
|
||||||
|
void uploadSongWithMetadata();
|
||||||
|
|
||||||
|
// Expects the song path, trackID, metadata file path, and cover path
|
||||||
|
void singTargetUpload(const std::string &songPath, const std::string &trackID,
|
||||||
|
const std::string &metaPath, const std::string &coverPath);
|
||||||
|
// Expects the source directory that contains songs, a metadata file, and cover image
|
||||||
|
// Disc and Track is retrieved from the filename if the filename conforms to a standard.
|
||||||
|
// If not, then the disc and track will default to 1
|
||||||
|
//
|
||||||
|
// Standards
|
||||||
|
// * track01.mp3 - Disc 1, Track 1
|
||||||
|
// * track05d02.mp3 - Disc 2, Track 5
|
||||||
|
void multiTargetUpload(const std::string &sourcePath);
|
||||||
|
|
||||||
|
// Standards
|
||||||
|
// * track01.mp3 - Disc 1, Track 1
|
||||||
|
// * track05d02.mp3 - Disc 2, Track 5
|
||||||
|
template<typename Song, typename Str>
|
||||||
|
void initializeDiscAndTrack(Song &song)
|
||||||
|
{
|
||||||
|
auto disc = 1;
|
||||||
|
auto track = 1;
|
||||||
|
// If 1 go with first standard, if 2 go with the second, if 0 then will default to 1 for disc and track
|
||||||
|
auto mode = 0;
|
||||||
|
const Str &songPath = song.songPath;
|
||||||
|
|
||||||
|
auto trd = song.songPath.find("trackd");
|
||||||
|
auto tr = song.songPath.find("track");
|
||||||
|
|
||||||
|
if (tr != Str::npos)
|
||||||
|
{
|
||||||
|
mode = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trd != Str::npos)
|
||||||
|
{
|
||||||
|
mode = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto dl = [](char c, char t){ return c == t; };
|
||||||
|
auto d = Utilities::Checks::itemIterInContainer<char, Str>(songPath, 'd', dl);
|
||||||
|
auto k = Utilities::Checks::itemIterInContainer<char, Str>(songPath, 'k', dl);
|
||||||
|
auto dot = Utilities::Checks::itemIterInContainer<char, Str>(songPath, '.', dl);
|
||||||
|
|
||||||
|
switch(mode)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
if (k != songPath.end() && dot != songPath.end())
|
||||||
|
{
|
||||||
|
auto tStr = std::string(++k, dot);
|
||||||
|
std::cout << "TStr: " << tStr<<"\n";
|
||||||
|
|
||||||
|
if (Utilities::Checks::isNumber(tStr))
|
||||||
|
{
|
||||||
|
track = std::atoi(tStr.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
if (k != songPath.end() && dot != songPath.end() && d != songPath.end())
|
||||||
|
{
|
||||||
|
auto tStr = std::string(++k, d);
|
||||||
|
auto dStr = std::string(++d, dot);
|
||||||
|
std::cout<<"DStr: "<<dStr<<" TStr: " << tStr<<"\n";
|
||||||
|
|
||||||
|
if (Utilities::Checks::isNumber(tStr))
|
||||||
|
{
|
||||||
|
track = std::atoi(tStr.c_str());
|
||||||
|
}
|
||||||
|
else if (Utilities::Checks::isNumber(dStr))
|
||||||
|
{
|
||||||
|
disc = std::atoi(dStr.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
song.disc = disc;
|
||||||
|
song.track = track;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Song, typename Str>
|
||||||
|
void parseDiscAndTrack(Song &song, const Str &trackID)
|
||||||
|
{
|
||||||
|
auto sep = [](char c, char t) { return c == t; };
|
||||||
|
auto separator = Utilities::Checks::itemIterInContainer<char, Str>(trackID, ':', sep);
|
||||||
|
|
||||||
|
if (separator != trackID.end())
|
||||||
|
{
|
||||||
|
auto dStr = Str(trackID.begin(), separator);
|
||||||
|
auto tStr = Str(++separator, trackID.end());
|
||||||
|
|
||||||
|
song.disc = std::atoi(dStr.c_str());
|
||||||
|
song.track = std::atoi(tStr.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto isNumber = Utilities::Checks::isNumber(trackID);
|
||||||
|
if (isNumber)
|
||||||
|
{
|
||||||
|
song.track = std::atoi(trackID.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Checks for the no confirm flag. Used when uploading songs from a directory
|
||||||
|
bool checkForNoConfirm()
|
||||||
|
{
|
||||||
|
for (const auto &arg: this->icaAction.flags)
|
||||||
|
{
|
||||||
|
if (arg.flag.compare("-nc") == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Album retrieveMetadata(const std::string_view path);
|
||||||
|
std::string retrieveFileContent(const std::string_view path);
|
||||||
|
|
||||||
enum class ActionValues
|
enum class ActionValues
|
||||||
{
|
{
|
||||||
deleteAct,
|
deleteAct,
|
||||||
downloadAct,
|
downloadAct,
|
||||||
retrieveAct,
|
retrieveAct,
|
||||||
uploadAct
|
uploadAct,
|
||||||
|
UPLOAD_SONG_WITH_METADATA // Uploads the song with metadata, including cover art
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Models::IcarusAction icaAction;
|
Models::IcarusAction icaAction;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,9 @@
|
|||||||
namespace Models
|
namespace Models
|
||||||
{
|
{
|
||||||
|
|
||||||
struct Flags
|
class Flags
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
std::string flag;
|
std::string flag;
|
||||||
std::string value;
|
std::string value;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,15 +2,48 @@
|
|||||||
#define ICARUSACTION_H_
|
#define ICARUSACTION_H_
|
||||||
|
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include<algorithm>
|
||||||
|
#include<string_view>
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
#include<iostream>
|
||||||
|
|
||||||
#include"Flags.h"
|
#include"Flags.h"
|
||||||
|
|
||||||
namespace Models
|
namespace Models
|
||||||
{
|
{
|
||||||
|
|
||||||
struct IcarusAction
|
class IcarusAction
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
std::string retrieveFlagValue(const std::string_view flag)
|
||||||
|
{
|
||||||
|
std::string value;
|
||||||
|
|
||||||
|
const auto fg = std::find_if(flags.begin(), flags.end(), [&](Flags f)
|
||||||
|
{
|
||||||
|
return f.flag.compare(flag) == 0 ? true : false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (fg != flags.end())
|
||||||
|
{
|
||||||
|
value.assign(fg->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
void print_action_and_flags() noexcept
|
||||||
|
{
|
||||||
|
std::cout<<"Action: "<<this->action<<"\n";
|
||||||
|
std::cout<<"Flag count: "<<this->flags.size()<<"\n";
|
||||||
|
|
||||||
|
for (const auto &flag : this->flags)
|
||||||
|
{
|
||||||
|
std::cout<<"flag "<<flag.flag<<" value "<<flag.value<<"\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout<<"\n";
|
||||||
|
}
|
||||||
|
|
||||||
std::string action;
|
std::string action;
|
||||||
std::vector<Flags> flags;
|
std::vector<Flags> flags;
|
||||||
};
|
};
|
||||||
|
|||||||
+65
-1
@@ -2,13 +2,65 @@
|
|||||||
#define SONG_H_
|
#define SONG_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace Models
|
namespace Models
|
||||||
{
|
{
|
||||||
|
|
||||||
struct Song
|
class Song
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Song() = default;
|
||||||
|
|
||||||
|
void printInfo()
|
||||||
|
{
|
||||||
|
std::cout<<"Title: "<<this->title<<"\n";
|
||||||
|
std::cout<<"\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string song_path() noexcept
|
||||||
|
{
|
||||||
|
std::stringstream buffer;
|
||||||
|
|
||||||
|
buffer << this->directory;
|
||||||
|
|
||||||
|
const auto count = this->directory.size();
|
||||||
|
|
||||||
|
if (this->directory.at(count - 1) != '/' || this->directory.at(count - 1) != '\\')
|
||||||
|
{
|
||||||
|
buffer << "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer << this->filename;
|
||||||
|
|
||||||
|
return buffer.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
int generate_filename_from_track()
|
||||||
|
{
|
||||||
|
auto result = 0;
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << "track";
|
||||||
|
|
||||||
|
// NOTE: Multiple discs in one directory is not being addressed
|
||||||
|
if (this->track < 10)
|
||||||
|
{
|
||||||
|
buffer << "0";
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer << this->track << ".mp3";
|
||||||
|
|
||||||
|
|
||||||
|
this->filename.assign(buffer.str());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toMetadataJson();
|
||||||
|
|
||||||
|
|
||||||
int id;
|
int id;
|
||||||
std::string title;
|
std::string title;
|
||||||
std::string artist;
|
std::string artist;
|
||||||
@@ -17,8 +69,20 @@ struct Song
|
|||||||
int year;
|
int year;
|
||||||
int duration;
|
int duration;
|
||||||
int track;
|
int track;
|
||||||
|
int disc;
|
||||||
std::string data;
|
std::string data;
|
||||||
|
[[deprecated("Use song_path() function instead")]]
|
||||||
std::string songPath;
|
std::string songPath;
|
||||||
|
std::string filename;
|
||||||
|
std::string directory;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CoverArt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int id;
|
||||||
|
std::string title;
|
||||||
|
std::string path;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "Managers/CommitManager.h"
|
||||||
#include "Managers/FileManager.h"
|
#include "Managers/FileManager.h"
|
||||||
#include "Models/API.h"
|
#include "Models/API.h"
|
||||||
#include "Models/Song.h"
|
#include "Models/Song.h"
|
||||||
@@ -22,16 +23,20 @@ namespace Syncers
|
|||||||
class Upload
|
class Upload
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Upload();
|
Upload() = default;
|
||||||
Upload(Models::API);
|
Upload(Models::API api, Models::Token token) : m_token(token), api(api)
|
||||||
|
{
|
||||||
|
this->api.endpoint = "song/data";
|
||||||
|
}
|
||||||
|
|
||||||
Models::Song uploadSong(const Models::Token&, Models::Song&);
|
Models::Song uploadSong(Models::Song&);
|
||||||
void uploadSongsFromDirectory(const Models::Token&,
|
void uploadSongsFromDirectory(const std::string&, const bool, bool);
|
||||||
const std::string&, const bool, bool);
|
void uploadSongWithMetadata(Managers::CommitManager::Album&, Models::Song&, Models::CoverArt&);
|
||||||
private:
|
private:
|
||||||
Managers::FileManager fMgr;
|
Managers::FileManager fMgr;
|
||||||
Models::API api;
|
Models::API api;
|
||||||
Models::Song song;
|
Models::Song song;
|
||||||
|
Models::Token m_token;
|
||||||
|
|
||||||
std::vector<Models::Song> retrieveAllSongsFromDirectory(const std::string&,
|
std::vector<Models::Song> retrieveAllSongsFromDirectory(const std::string&,
|
||||||
bool);
|
bool);
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
#ifndef CHECKS_H_
|
||||||
|
#define CHECKS_H_
|
||||||
|
|
||||||
|
#include<algorithm>
|
||||||
|
#include<cstdlib>
|
||||||
|
#include<ctype.h>
|
||||||
|
|
||||||
|
namespace Utilities
|
||||||
|
{
|
||||||
|
class Checks
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Checks() = delete;
|
||||||
|
|
||||||
|
static auto isNumber(const std::string &val)
|
||||||
|
{
|
||||||
|
return !val.empty() && std::find_if(val.begin(),
|
||||||
|
val.end(), [](char c)
|
||||||
|
{
|
||||||
|
return !std::isdigit(c);
|
||||||
|
}) == val.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: Not implemented
|
||||||
|
template<typename Item, typename Container, typename Func>
|
||||||
|
static auto itemInContainer(const Container container, const Item item, Func func)
|
||||||
|
{
|
||||||
|
auto result = false;
|
||||||
|
auto i = std::find_if(container.begin(), container.end(), [&](Item i)
|
||||||
|
{
|
||||||
|
return func(item, i);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (i != container.end())
|
||||||
|
{
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Item, typename Container, typename Func>
|
||||||
|
static auto itemIterInContainer(const Container &container, const Item &item, Func func)
|
||||||
|
{
|
||||||
|
auto result = false;
|
||||||
|
// std::cout<<container<<"\n";
|
||||||
|
auto ii = std::find_if(container.begin(), container.end(), [&](Item i)
|
||||||
|
{
|
||||||
|
// std::cout<<"iter "<<i<<" target "<<item<<"\n";
|
||||||
|
return func(i, item);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (ii == container.end())
|
||||||
|
{
|
||||||
|
// std::cout<<item<<" not found in container\n";
|
||||||
|
ii = container.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ii;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -12,6 +12,14 @@ class Conversions
|
|||||||
public:
|
public:
|
||||||
Conversions();
|
Conversions();
|
||||||
|
|
||||||
|
static void toLowerChar(char &c)
|
||||||
|
{
|
||||||
|
if (std::isalpha(c))
|
||||||
|
{
|
||||||
|
c = std::tolower(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void initializeValues();
|
void initializeValues();
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
+3
-1
@@ -14,7 +14,7 @@ using Managers::CommitManager;
|
|||||||
|
|
||||||
constexpr static auto IcarusDownloadManager_version()
|
constexpr static auto IcarusDownloadManager_version()
|
||||||
{
|
{
|
||||||
return "v0.1.2";
|
return "v0.2.0";
|
||||||
}
|
}
|
||||||
|
|
||||||
void printHelp()
|
void printHelp()
|
||||||
@@ -62,6 +62,8 @@ int main(int argc, char** argv)
|
|||||||
ActionManager actMgr(argv, argc);
|
ActionManager actMgr(argv, argc);
|
||||||
auto chosenAction = actMgr.retrieveIcarusAction();
|
auto chosenAction = actMgr.retrieveIcarusAction();
|
||||||
|
|
||||||
|
chosenAction.print_action_and_flags();
|
||||||
|
|
||||||
CommitManager commitMgr(chosenAction);
|
CommitManager commitMgr(chosenAction);
|
||||||
commitMgr.commitAction();
|
commitMgr.commitAction();
|
||||||
|
|
||||||
|
|||||||
@@ -37,23 +37,6 @@ IcarusAction ActionManager::retrieveIcarusAction() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
constexpr std::array<const char*, 12> ActionManager::supportedFlags() noexcept
|
|
||||||
{
|
|
||||||
constexpr std::array<const char*, 12> allFlags{"-u", "-p", "-t", "-h", "-s",
|
|
||||||
"-sd", "-sr", "-d", "-D", "-b", "-rt", "-nc"};
|
|
||||||
|
|
||||||
return allFlags;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool ActionManager::isNumber(string_view val) noexcept
|
|
||||||
{
|
|
||||||
return !val.empty() && std::find_if(val.begin(),
|
|
||||||
val.end(), [](char c)
|
|
||||||
{
|
|
||||||
return !std::isdigit(c);
|
|
||||||
}) == val.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActionManager::initialize()
|
void ActionManager::initialize()
|
||||||
{
|
{
|
||||||
@@ -65,44 +48,33 @@ void ActionManager::initialize()
|
|||||||
}
|
}
|
||||||
void ActionManager::validateFlags()
|
void ActionManager::validateFlags()
|
||||||
{
|
{
|
||||||
cout<<"Validating flags"<<endl;
|
cout<<"Validating flags\n";
|
||||||
|
|
||||||
auto flagVals = parsedFlags();
|
const auto flagVals = parsedFlags();
|
||||||
|
|
||||||
Flags flg{};
|
for (auto flag = flagVals.begin(); flag != flagVals.end(); ++flag)
|
||||||
|
|
||||||
auto allSupportedFlags = supportedFlags();
|
|
||||||
|
|
||||||
for (auto flag : flagVals)
|
|
||||||
{
|
{
|
||||||
if (flag.compare("-nc") == 0)
|
Flags flg;
|
||||||
|
cout<<"Value: "<<*flag<<"\n";
|
||||||
|
|
||||||
|
if (isValidFlag<string>(*flag) && doesFlagHaveValue<string>(*flag))
|
||||||
{
|
{
|
||||||
flg.flag = flag;
|
cout<<"Flag has value\n";
|
||||||
flags.push_back(flg);
|
flg.flag = *flag;
|
||||||
continue;
|
flg.value = *(++flag);
|
||||||
}
|
}
|
||||||
if (flag.size() > 3 || isNumber(flag))
|
else if (isValidFlag<string>(*flag))
|
||||||
{
|
{
|
||||||
flg.value = flag;
|
cout<<"Flag does not have a value\n";
|
||||||
|
flg.flag = *flag;
|
||||||
flags.push_back(flg);
|
|
||||||
flg = Flags{};
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std::any_of(allSupportedFlags.begin(), allSupportedFlags.end(),
|
|
||||||
[&](const char *val)
|
|
||||||
{
|
|
||||||
return !flag.compare(val);
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
flg.flag = flag;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout<<"Flag is not valid"<<endl;
|
cout<<"Flag "<<*flag<<" is not valid"<<endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags.emplace_back(std::move(flg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+225
-11
@@ -1,6 +1,11 @@
|
|||||||
#include"Managers/CommitManager.h"
|
#include"Managers/CommitManager.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
#include "Models/API.h"
|
#include "Models/API.h"
|
||||||
#include "Models/Song.h"
|
#include "Models/Song.h"
|
||||||
@@ -31,6 +36,8 @@ using Syncers::Download;
|
|||||||
using Syncers::RetrieveRecords;
|
using Syncers::RetrieveRecords;
|
||||||
using Syncers::Upload;
|
using Syncers::Upload;
|
||||||
|
|
||||||
|
namespace filesystem = std::filesystem;
|
||||||
|
|
||||||
namespace Managers
|
namespace Managers
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -45,6 +52,7 @@ void CommitManager::commitAction()
|
|||||||
{
|
{
|
||||||
auto action = icaAction.action;
|
auto action = icaAction.action;
|
||||||
cout<<"Commiting "<<action<<" action"<<endl;
|
cout<<"Commiting "<<action<<" action"<<endl;
|
||||||
|
|
||||||
switch (mapActions()[action])
|
switch (mapActions()[action])
|
||||||
{
|
{
|
||||||
case ActionValues::deleteAct:
|
case ActionValues::deleteAct:
|
||||||
@@ -59,13 +67,18 @@ void CommitManager::commitAction()
|
|||||||
case ActionValues::uploadAct:
|
case ActionValues::uploadAct:
|
||||||
uploadSong();
|
uploadSong();
|
||||||
break;
|
break;
|
||||||
|
case ActionValues::UPLOAD_SONG_WITH_METADATA:
|
||||||
|
uploadSongWithMetadata();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum class ActionValues;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::map<std::string, CommitManager::ActionValues>
|
std::map<std::string, CommitManager::ActionValues>
|
||||||
CommitManager::mapActions() noexcept
|
CommitManager::mapActions() noexcept
|
||||||
@@ -74,16 +87,19 @@ std::map<std::string, CommitManager::ActionValues>
|
|||||||
{"delete", ActionValues::deleteAct},
|
{"delete", ActionValues::deleteAct},
|
||||||
{"download", ActionValues::downloadAct},
|
{"download", ActionValues::downloadAct},
|
||||||
{"retrieve", ActionValues::retrieveAct},
|
{"retrieve", ActionValues::retrieveAct},
|
||||||
{"upload", ActionValues::uploadAct}
|
{"upload", ActionValues::uploadAct},
|
||||||
|
{"upload-meta", ActionValues::UPLOAD_SONG_WITH_METADATA}
|
||||||
};
|
};
|
||||||
|
|
||||||
return actions;
|
return actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Token CommitManager::parseToken(API api)
|
Token CommitManager::parseToken(API api)
|
||||||
{
|
{
|
||||||
cout<<"fetching token"<<endl;
|
cout<<"fetching token\n";
|
||||||
UserManager usrMgr{icaAction};
|
UserManager usrMgr{icaAction};
|
||||||
auto user = usrMgr.retrieveUser();
|
auto user = usrMgr.retrieveUser();
|
||||||
|
|
||||||
@@ -175,11 +191,12 @@ void CommitManager::retrieveObjects()
|
|||||||
songs.retrieve(retrieveType);
|
songs.retrieve(retrieveType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommitManager::uploadSong()
|
void CommitManager::uploadSong()
|
||||||
{
|
{
|
||||||
auto uploadSingleSong = true;
|
auto uploadSingleSong = true;
|
||||||
auto recursiveDirectory = false;
|
auto recursiveDirectory = false;
|
||||||
auto noConfirm = false;
|
const auto noConfirm = checkForNoConfirm();
|
||||||
string songDirectory;
|
string songDirectory;
|
||||||
APIParser apiPrs{icaAction};
|
APIParser apiPrs{icaAction};
|
||||||
auto api = apiPrs.retrieveAPI();
|
auto api = apiPrs.retrieveAPI();
|
||||||
@@ -208,24 +225,221 @@ void CommitManager::uploadSong()
|
|||||||
uploadSingleSong = false;
|
uploadSingleSong = false;
|
||||||
recursiveDirectory = true;
|
recursiveDirectory = true;
|
||||||
}
|
}
|
||||||
else if (flag.compare("-nc") == 0)
|
|
||||||
{
|
|
||||||
noConfirm = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Upload upld{api};
|
Upload upld{api, token};
|
||||||
if (uploadSingleSong)
|
if (uploadSingleSong)
|
||||||
{
|
{
|
||||||
cout<<"Uploading song..."<<endl;
|
cout<<"Uploading song..."<<endl;
|
||||||
upld.uploadSong(token, song);
|
upld.uploadSong(song);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout<<"Uploading songs from " << songDirectory << endl;
|
cout<<"Uploading songs from " << songDirectory << endl;
|
||||||
upld.uploadSongsFromDirectory(token, songDirectory, noConfirm, recursiveDirectory);
|
upld.uploadSongsFromDirectory(songDirectory, noConfirm, recursiveDirectory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void CommitManager::uploadSongWithMetadata()
|
||||||
|
{
|
||||||
|
cout<<"Uploading single song with metadata\n\n";
|
||||||
|
|
||||||
|
// Either the set of "-s", "-m", "-ca", "-t" flags or "-smca" must exist with values
|
||||||
|
// in order to be valid but not both
|
||||||
|
const auto songPath = this->icaAction.retrieveFlagValue("-s");
|
||||||
|
const auto metadataPath = this->icaAction.retrieveFlagValue("-m");
|
||||||
|
const auto coverPath = this->icaAction.retrieveFlagValue("-ca");
|
||||||
|
const auto trackID = this->icaAction.retrieveFlagValue("-t");
|
||||||
|
const auto singleTarget = !songPath.empty() && !metadataPath.empty() &&
|
||||||
|
!coverPath.empty() && !trackID.empty() ? true : false;
|
||||||
|
|
||||||
|
const auto uni = this->icaAction.retrieveFlagValue("-smca");
|
||||||
|
const auto multiTarget = !uni.empty() ? true : false;
|
||||||
|
|
||||||
|
if (singleTarget && multiTarget)
|
||||||
|
{
|
||||||
|
cout<<"Cannot upload from source and directory\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout<<"Song path: "<<songPath<<"\n";
|
||||||
|
cout<<"TrackID: "<<trackID<<"\n";
|
||||||
|
cout<<"Metadata path: "<<metadataPath<<"\n";
|
||||||
|
cout<<"Cover Art path: "<<coverPath<<"\n";
|
||||||
|
|
||||||
|
if (singleTarget)
|
||||||
|
{
|
||||||
|
singTargetUpload(songPath, trackID, metadataPath, coverPath);
|
||||||
|
}
|
||||||
|
else if (multiTarget)
|
||||||
|
{
|
||||||
|
multiTargetUpload(uni);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CommitManager::singTargetUpload(const std::string &songPath, const std::string &trackID,
|
||||||
|
const std::string &metaPath, const std::string &coverPath)
|
||||||
|
{
|
||||||
|
APIParser apiPrs(icaAction);
|
||||||
|
auto api = apiPrs.retrieveAPI();
|
||||||
|
const auto token = parseToken(api);
|
||||||
|
|
||||||
|
auto album = retrieveMetadata(metaPath);
|
||||||
|
album.printInfo();
|
||||||
|
|
||||||
|
Song song;
|
||||||
|
song.track = 1;
|
||||||
|
song.disc = 1;
|
||||||
|
|
||||||
|
cout<<"TrackID: "<<trackID<<"\n";
|
||||||
|
|
||||||
|
parseDiscAndTrack<Song, std::string>(song, trackID);
|
||||||
|
|
||||||
|
auto c = [](const Song &songA, const Song &songB) { return songA.track == songB.track && songA.disc == songB.disc; };
|
||||||
|
auto sng = Utilities::Checks::itemIterInContainer<Song, std::vector<Song>>(album.songs, song, c);
|
||||||
|
|
||||||
|
if (sng == album.songs.end())
|
||||||
|
{
|
||||||
|
cout<<"Not found with disc "<<song.disc<<" track "<<song.track<<"\n";
|
||||||
|
std::exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
song = *sng;
|
||||||
|
const auto p = fs::path(songPath);
|
||||||
|
song.directory = p.parent_path.string();
|
||||||
|
song.generate_filename_from_track();
|
||||||
|
|
||||||
|
Models::CoverArt cover;
|
||||||
|
cover.title = song.title;
|
||||||
|
cover.path = coverPath;
|
||||||
|
|
||||||
|
Upload up(api, token);
|
||||||
|
up.uploadSongWithMetadata(album, song, cover);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CommitManager::multiTargetUpload(const std::string &sourcePath)
|
||||||
|
{
|
||||||
|
APIParser apiPrs(icaAction);
|
||||||
|
auto api = apiPrs.retrieveAPI();
|
||||||
|
const auto token = parseToken(api);
|
||||||
|
|
||||||
|
if (!fs::is_directory(sourcePath))
|
||||||
|
{
|
||||||
|
cout<<sourcePath<<" is not a directory\n";
|
||||||
|
std::exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Song> songs;
|
||||||
|
Models::CoverArt cover;
|
||||||
|
string metadataPath;
|
||||||
|
|
||||||
|
for (auto &p: fs::directory_iterator(sourcePath))
|
||||||
|
{
|
||||||
|
const auto &pp = p.path();
|
||||||
|
const auto stem = pp.stem();
|
||||||
|
const auto file = pp.filename();
|
||||||
|
const auto extension = pp.extension();
|
||||||
|
|
||||||
|
cout<<"Stem "<<stem<<" Extension "<<extension<<"\n";
|
||||||
|
|
||||||
|
if (extension.compare(".mp3") == 0)
|
||||||
|
{
|
||||||
|
Song song;
|
||||||
|
song.songPath = pp.string();
|
||||||
|
|
||||||
|
initializeDiscAndTrack<Song, std::string>(song);
|
||||||
|
|
||||||
|
songs.emplace_back(std::move(song));
|
||||||
|
}
|
||||||
|
else if (extension.compare(".jpg") == 0 || extension.compare(".png") == 0)
|
||||||
|
{
|
||||||
|
cover.path.assign(pp.string());
|
||||||
|
}
|
||||||
|
else if (extension.compare(".json") == 0)
|
||||||
|
{
|
||||||
|
metadataPath.assign(pp.string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto album = retrieveMetadata(metadataPath);
|
||||||
|
songs.clear();
|
||||||
|
songs.assign(album.songs.begin(), album.songs.end());
|
||||||
|
|
||||||
|
Upload up(api, token);
|
||||||
|
|
||||||
|
for (auto &song : songs)
|
||||||
|
{
|
||||||
|
up.uploadSongWithMetadata(album, song, cover);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma region private
|
||||||
|
CommitManager::Album CommitManager::retrieveMetadata(const std::string_view path)
|
||||||
|
{
|
||||||
|
CommitManager::Album album;
|
||||||
|
const auto fileContent = retrieveFileContent(path);
|
||||||
|
cout<<"Parsing...\n";
|
||||||
|
auto serialized = nlohmann::json::parse(fileContent);
|
||||||
|
cout<<"Parsed\n";
|
||||||
|
|
||||||
|
album.album = serialized["album"].get<std::string>();
|
||||||
|
album.albumArtist = serialized["album_artist"].get<std::string>();
|
||||||
|
album.genre = serialized["genre"].get<std::string>();
|
||||||
|
album.year = serialized["year"].get<int>();
|
||||||
|
album.trackCount = serialized["track_count"].get<int>();
|
||||||
|
album.discCount = serialized["disc_count"].get<int>();
|
||||||
|
album.songs.reserve(album.trackCount);
|
||||||
|
|
||||||
|
for (auto &j : serialized["tracks"])
|
||||||
|
{
|
||||||
|
Song song;
|
||||||
|
song.title = j["title"].get<std::string>();
|
||||||
|
song.track = j["track"].get<int>();
|
||||||
|
song.disc = j["disc"].get<int>();
|
||||||
|
song.artist = j["artist"].get<std::string>();
|
||||||
|
song.album = album.album;
|
||||||
|
song.year = album.year;
|
||||||
|
song.genre = album.genre;
|
||||||
|
song.generate_filename_from_track();
|
||||||
|
const auto p = fs::path(path);
|
||||||
|
song.directory = p.parent_path().string();
|
||||||
|
|
||||||
|
album.songs.push_back(song);
|
||||||
|
}
|
||||||
|
|
||||||
|
return album;
|
||||||
|
}
|
||||||
|
|
||||||
|
string CommitManager::retrieveFileContent(const std::string_view path)
|
||||||
|
{
|
||||||
|
string path_str(path);
|
||||||
|
string value;
|
||||||
|
|
||||||
|
std::stringstream buffer;
|
||||||
|
std::fstream file(path_str, std::ios::in);
|
||||||
|
buffer<<file.rdbuf();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
value.assign(buffer.str());
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
|
void CommitManager::Album::printInfo()
|
||||||
|
{
|
||||||
|
std::cout<<"Album: "<<this->album<<"\n";
|
||||||
|
std::cout<<"Album Artist: "<<this->albumArtist<<"\n";
|
||||||
|
std::cout<<"Genre: "<<this->genre<<"\n";
|
||||||
|
std::cout<<"Year: "<<this->year<<"\n";
|
||||||
|
std::cout<<"Track count: "<<this->trackCount<<"\n";
|
||||||
|
std::cout<<"Disc count: "<<this->discCount<<"\n";
|
||||||
|
std::cout<<"\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma region Functions
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include "Models/Song.h"
|
||||||
|
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
namespace Models
|
||||||
|
{
|
||||||
|
string Song::toMetadataJson()
|
||||||
|
{
|
||||||
|
nlohmann::json s;
|
||||||
|
s["title"] = this->title;
|
||||||
|
s["artist"] = this->artist;
|
||||||
|
s["album"] = this->album;
|
||||||
|
s["genre"] = this->genre;
|
||||||
|
s["year"] = this->year;
|
||||||
|
s["track"] = this->track;
|
||||||
|
s["disc"] = this->disc;
|
||||||
|
|
||||||
|
return s.dump();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,7 +41,6 @@ void APIParser::parseAPI()
|
|||||||
api.url = (value[value.size()-1] == '/') ? value : value + "/";
|
api.url = (value[value.size()-1] == '/') ? value : value + "/";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: For now I will hard code
|
// TODO: For now I will hard code
|
||||||
|
|||||||
+58
-36
@@ -2,10 +2,10 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#include<cpr/cpr.h>
|
#include "cpr/cpr.h"
|
||||||
#include<nlohmann/json.hpp>
|
|
||||||
|
|
||||||
#include "Syncers/Upload.h"
|
#include "Syncers/Upload.h"
|
||||||
|
#include "Utilities/Conversions.h"
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::cin;
|
using std::cin;
|
||||||
@@ -26,24 +26,19 @@ namespace Syncers
|
|||||||
{
|
{
|
||||||
|
|
||||||
#pragma region Constructors
|
#pragma region Constructors
|
||||||
Upload::Upload() { }
|
|
||||||
Upload::Upload(API api) : api(api)
|
|
||||||
{
|
|
||||||
this->api.endpoint = "song/data";
|
|
||||||
}
|
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
|
|
||||||
#pragma region Functions
|
#pragma region Functions
|
||||||
Song Upload::uploadSong(const Models::Token& token, Song& song)
|
Song Upload::uploadSong(Song& song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto url = retrieveUrl();
|
auto url = retrieveUrl();
|
||||||
|
|
||||||
cout<<"url "<<url<<endl;
|
cout<<"url "<<url<<endl;
|
||||||
string auth{token.tokenType};
|
string auth{this->m_token.tokenType};
|
||||||
auth.append(" " + token.accessToken);
|
auth.append(" " + this->m_token.accessToken);
|
||||||
auto r = cpr::Post(cpr::Url{url},
|
auto r = cpr::Post(cpr::Url{url},
|
||||||
cpr::Multipart{{"key", "small value"},
|
cpr::Multipart{{"key", "small value"},
|
||||||
{"file", cpr::File{song.songPath}}},
|
{"file", cpr::File{song.songPath}}},
|
||||||
@@ -74,8 +69,7 @@ Song Upload::uploadSong(const Models::Token& token, Song& song)
|
|||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Upload::uploadSongsFromDirectory(const Models::Token& token,
|
void Upload::uploadSongsFromDirectory(const std::string& directory,
|
||||||
const std::string& directory,
|
|
||||||
const bool noConfirm, bool recursive = false)
|
const bool noConfirm, bool recursive = false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -88,29 +82,19 @@ void Upload::uploadSongsFromDirectory(const Models::Token& token,
|
|||||||
auto answer = 'a';
|
auto answer = 'a';
|
||||||
cout << "are you sure you want to upload " << songs.size() << " songs? [y/n]";
|
cout << "are you sure you want to upload " << songs.size() << " songs? [y/n]";
|
||||||
cin >> answer;
|
cin >> answer;
|
||||||
|
Utilities::Conversions::toLowerChar(answer);
|
||||||
|
|
||||||
if (answer == 'y' || answer == 'Y')
|
if (answer == 'y' || answer == 'Y')
|
||||||
{
|
{
|
||||||
confirmUpload = true;
|
confirmUpload = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (answer == 'n' || answer == 'N')
|
|
||||||
{
|
|
||||||
confirmUpload = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!confirmUpload)
|
|
||||||
{
|
|
||||||
cout << "exiting...\n";
|
|
||||||
std::exit(-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "uploading songs\n";
|
cout << "uploading songs\n";
|
||||||
for (auto& song: songs)
|
for (auto& song: songs)
|
||||||
{
|
{
|
||||||
song = uploadSong(token, song);
|
song = uploadSong(song);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (exception& e)
|
catch (exception& e)
|
||||||
@@ -120,10 +104,57 @@ void Upload::uploadSongsFromDirectory(const Models::Token& token,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Upload::uploadSongWithMetadata(Managers::CommitManager::Album &album, Models::Song& song, Models::CoverArt &cover)
|
||||||
|
{
|
||||||
|
this->api.endpoint.assign("song/data/upload/with/data");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto url = retrieveUrl();
|
||||||
|
|
||||||
|
cout << "url " << url << "\n";
|
||||||
|
string auth(this->m_token.tokenType);
|
||||||
|
auth.append(" " + this->m_token.accessToken);
|
||||||
|
|
||||||
|
nlohmann::json s;
|
||||||
|
s["title"] = song.title;
|
||||||
|
s["album"] = album.album;
|
||||||
|
s["album_artist"] = album.albumArtist;
|
||||||
|
s["artist"] = song.artist;
|
||||||
|
s["year"] = album.year;
|
||||||
|
s["genre"] = album.genre;
|
||||||
|
s["disc"] = song.disc;
|
||||||
|
s["track"] = song.track;
|
||||||
|
s["disc_count"] = album.discCount;
|
||||||
|
s["track_count"] = album.trackCount;
|
||||||
|
|
||||||
|
const auto meta = s.dump();
|
||||||
|
|
||||||
|
cout<<"\n\nMeta:\n"<<meta<<"\n";
|
||||||
|
cout << "Filepath: " << song.song_path() << "\n";
|
||||||
|
|
||||||
|
auto multipart = cpr::Multipart{{"cover", cpr::File{cover.path}},
|
||||||
|
{"metadata", meta},
|
||||||
|
{"file", cpr::File{song.song_path()}}};
|
||||||
|
|
||||||
|
auto r = cpr::Post(cpr::Url{url}, multipart,
|
||||||
|
cpr::Header{{"authorization", auth}}
|
||||||
|
);
|
||||||
|
|
||||||
|
cout << "status code: " << r.status_code<< std::endl;
|
||||||
|
cout << r.text << endl;
|
||||||
|
}
|
||||||
|
catch (exception &e)
|
||||||
|
{
|
||||||
|
auto msg = e.what();
|
||||||
|
cout<<"Error: "<<msg<<"\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
std::vector<Song> Upload::retrieveAllSongsFromDirectory(const std::string& directory,
|
std::vector<Song> Upload::retrieveAllSongsFromDirectory(const std::string& directory,
|
||||||
bool recursive)
|
bool recursive)
|
||||||
{
|
{
|
||||||
std::vector<Song> allSongs;
|
std::vector<Song> allSongs;
|
||||||
|
|
||||||
if (recursive)
|
if (recursive)
|
||||||
{
|
{
|
||||||
for (auto p: fs::recursive_directory_iterator(directory))
|
for (auto p: fs::recursive_directory_iterator(directory))
|
||||||
@@ -175,17 +206,7 @@ Song Upload::retrieveSongPath(fs::directory_entry& dirEntry)
|
|||||||
|
|
||||||
|
|
||||||
#pragma region Testing
|
#pragma region Testing
|
||||||
void Upload::printSongDetails()
|
|
||||||
{
|
|
||||||
cout<<"Song details: "<<endl;
|
|
||||||
cout<<"Id: "<<song.id<<endl;
|
|
||||||
cout<<"Title: "<<song.title<<endl;
|
|
||||||
cout<<"Artist: "<<song.artist<<endl;
|
|
||||||
cout<<"Album: "<<song.album<<endl;
|
|
||||||
cout<<"Genre: "<<song.genre<<endl;
|
|
||||||
cout<<"Year: "<<song.year<<endl;
|
|
||||||
cout<<"Duration: "<<song.duration<<endl;
|
|
||||||
}
|
|
||||||
void Upload::printSongDetails(std::vector<Song>& songs)
|
void Upload::printSongDetails(std::vector<Song>& songs)
|
||||||
{
|
{
|
||||||
for (auto& song: songs)
|
for (auto& song: songs)
|
||||||
@@ -201,6 +222,7 @@ void Upload::printSongDetails(std::vector<Song>& songs)
|
|||||||
cout<<"Path: "<<song.songPath<<endl;
|
cout<<"Path: "<<song.songPath<<endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Upload::printJsonData(const json& obj)
|
void Upload::printJsonData(const json& obj)
|
||||||
{
|
{
|
||||||
cout<<endl<<endl<<"JSon data: "<<endl;
|
cout<<endl<<endl<<"JSon data: "<<endl;
|
||||||
@@ -213,7 +235,7 @@ void Upload::printJsonData(const json& obj)
|
|||||||
cout<<"duration: "<<obj["duration"]<<endl;
|
cout<<"duration: "<<obj["duration"]<<endl;
|
||||||
cout<<"song_data: "<<obj["song_data"]<<endl;
|
cout<<"song_data: "<<obj["song_data"]<<endl;
|
||||||
|
|
||||||
cout<<endl<<endl;;
|
cout<<endl<<endl;
|
||||||
}
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|||||||
Reference in New Issue
Block a user