mlc/collection.cpp

103 lines
2.9 KiB
C++
Raw Permalink Normal View History

2023-07-19 22:12:18 +00:00
#include "collection.h"
2023-07-21 21:32:24 +00:00
#include "taskmanager.h"
2023-07-19 22:12:18 +00:00
namespace fs = std::filesystem;
static const std::string flac(".flac");
2023-07-21 21:32:24 +00:00
Collection::Collection(const std::string& path, TaskManager* tm) :
2023-07-19 22:12:18 +00:00
path(fs::canonical(path)),
countMusical(0),
counted(false),
2023-07-21 21:32:24 +00:00
taskManager(tm)
2023-07-19 22:12:18 +00:00
{
}
2023-07-21 21:32:24 +00:00
Collection::Collection(const std::filesystem::path& path, TaskManager* tm):
2023-07-19 22:12:18 +00:00
path(fs::canonical(path)),
countMusical(0),
counted(false),
2023-07-21 21:32:24 +00:00
taskManager(tm)
2023-07-19 22:12:18 +00:00
{
}
Collection::~Collection() {
}
void Collection::list() const {
if (fs::is_regular_file(path))
return;
for (const fs::directory_entry & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
uint32_t Collection::countMusicFiles() const {
if (counted)
return countMusical;
if (fs::is_regular_file(path)) {
if (isMusic(path))
++countMusical;
} else if (fs::is_directory(path)) {
for (const fs::directory_entry& entry : fs::directory_iterator(path)) {
switch (entry.status().type()) {
case fs::file_type::regular:
if (isMusic(entry.path()))
++countMusical;
break;
case fs::file_type::directory: {
Collection collection(entry.path());
countMusical += collection.countMusicFiles();
} break;
default:
break;
}
}
}
counted = true;
return countMusical;
}
2023-07-21 21:32:24 +00:00
void Collection::convert(const std::string& outPath) {
if (taskManager == nullptr)
throw 6;
2023-07-19 22:12:18 +00:00
fs::path out = fs::absolute(outPath);
fs::create_directories(out);
out = fs::canonical(outPath);
for (const fs::directory_entry& entry : fs::directory_iterator(path)) {
switch (entry.status().type()) {
case fs::file_type::regular: {
fs::path sourcePath = entry.path();
fs::path dstPath = out / sourcePath.filename();
if (isMusic(sourcePath)) {
dstPath.replace_extension(".mp3");
2023-07-21 21:32:24 +00:00
taskManager->queueJob(sourcePath, dstPath);
2023-07-19 22:12:18 +00:00
} else {
2023-07-21 21:32:24 +00:00
fs::copy_file(sourcePath, dstPath, fs::copy_options::overwrite_existing);
2023-07-19 22:12:18 +00:00
}
//std::cout << sourcePath << " => " << dstPath << std::endl;
} break;
case fs::file_type::directory: {
fs::path sourcePath = entry.path();
2023-07-21 21:32:24 +00:00
Collection collection(sourcePath, taskManager);
2023-07-19 22:12:18 +00:00
fs::path::iterator itr = sourcePath.end();
--itr;
2023-07-21 21:32:24 +00:00
collection.convert(std::string(out / *itr));
2023-07-19 22:12:18 +00:00
} break;
default:
break;
}
}
}
bool Collection::isMusic(const std::filesystem::path& path) {
return path.extension() == flac; //I know, it's primitive yet, but it's the fastest
}