many different fixes

This commit is contained in:
faumaray committed 2026-02-03 05:07:15 +03:00
1 parent a583c17916
commit 53801f2b53
36 files changed
+5149 -1291

No files matched your search

+301 -72
View File
@@ -1,11 +1,12 @@
#include "echoion/core/YtDlpManager.hpp"
#include "echoion/core/Logger.hpp"
#include <glaze/glaze.hpp>
#include <format>
#include <fstream>
#include <cstdlib>
#include "echoion/core/YtDlpManager.hpp"
#include <array>
#include <chrono>
#include <cstdlib>
#include <format>
#include <fstream>
#include <future>
#include <glaze/glaze.hpp>
#ifdef _WIN32
#include <windows.h>
@@ -20,7 +21,7 @@ YtDlpManager::YtDlpManager(std::shared_ptr<NetworkClient> networkClient)
: networkClient_(std::move(networkClient)) {
}
YtDlpManager::~YtDlpManager() = default;
YtDlpManager::~YtDlpManager() { stopThreadPool(); }
std::filesystem::path YtDlpManager::getStorageDir() const {
// Store in ~/.local/share/echoion/yt-dlp on Linux
@@ -138,10 +139,153 @@ Api::Result<void> YtDlpManager::initialize() {
}
ECHOION_LOG_INFO(std::format("yt-dlp ready: {}", version));
std::lock_guard<std::mutex> lock(mutex_);
initialized_ = true;
return {};
// Start thread pool for async operations
startThreadPool();
std::lock_guard<std::mutex> lock(mutex_);
initialized_ = true;
return {};
}
void YtDlpManager::startThreadPool()
{
stopThreadPool(); // Clear any existing threads first
// Auto-adjust thread pool size based on hardware
unsigned int hardwareThreads = std::thread::hardware_concurrency();
if (hardwareThreads > 0 && threadPoolSize_ > hardwareThreads / 2) {
threadPoolSize_ = std::max(1u, hardwareThreads / 2);
ECHOION_LOG_INFO(std::format(
"Auto-adjusted yt-dlp thread pool to {} threads (hardware: {})",
threadPoolSize_, hardwareThreads));
}
ECHOION_LOG_INFO(std::format("Starting yt-dlp thread pool with {} threads",
threadPoolSize_));
for (size_t i = 0; i < threadPoolSize_; ++i) {
threadPool_.emplace_back([this] { workerThread(); });
}
}
void YtDlpManager::stopThreadPool()
{
stopThreads_ = true;
taskCv_.notify_all();
for (auto& thread : threadPool_) {
if (thread.joinable()) {
thread.join();
}
}
threadPool_.clear();
stopThreads_ = false;
}
void YtDlpManager::workerThread()
{
while (!stopThreads_.load()) {
std::packaged_task<YtDlpResult()> task;
{
std::unique_lock<std::mutex> lock(taskMutex_);
taskCv_.wait(lock, [this] {
return !taskQueue_.empty() || stopThreads_.load();
});
if (stopThreads_.load()) {
return;
}
if (!taskQueue_.empty()) {
task = std::move(taskQueue_.front());
taskQueue_.pop();
}
}
if (task.valid()) {
task();
}
}
}
void YtDlpManager::setThreadPoolSize(size_t size)
{
if (size == 0) {
size = 1;
}
if (size > 16) {
size = 16; // Cap at 16 threads
}
std::lock_guard<std::mutex> lock(mutex_);
if (threadPoolSize_ != size) {
threadPoolSize_ = size;
if (initialized_) {
// Restart thread pool with new size
stopThreadPool();
startThreadPool();
}
}
}
size_t YtDlpManager::getThreadPoolSize() const
{
std::lock_guard<std::mutex> lock(mutex_);
return threadPoolSize_;
}
void YtDlpManager::setProxyConfig(const std::optional<Api::ProxyConfig>& proxy)
{
std::lock_guard<std::mutex> lock(proxyMutex_);
proxyConfig_ = proxy;
if (proxy.has_value() && proxy->isValid()) {
ECHOION_LOG_INFO(std::format("yt-dlp proxy configured: {}:{}",
proxy->host, proxy->port));
} else {
ECHOION_LOG_INFO("yt-dlp proxy cleared");
}
}
std::optional<Api::ProxyConfig> YtDlpManager::getProxyConfig() const
{
std::lock_guard<std::mutex> lock(proxyMutex_);
return proxyConfig_;
}
std::vector<std::string> YtDlpManager::getProxyArgs() const
{
std::lock_guard<std::mutex> lock(proxyMutex_);
std::vector<std::string> proxyArgs;
if (!proxyConfig_.has_value() || !proxyConfig_->isValid()) {
return proxyArgs;
}
const auto& proxy = *proxyConfig_;
std::string proxyUrl;
if (proxy.isSocks5()) {
if (proxy.requiresAuth()) {
proxyUrl = std::format("socks5://{}:{}@{}:{}", *proxy.username,
*proxy.password, proxy.host, proxy.port);
} else {
proxyUrl = std::format("socks5://{}:{}", proxy.host, proxy.port);
}
} else {
if (proxy.requiresAuth()) {
proxyUrl = std::format("http://{}:{}@{}:{}", *proxy.username,
*proxy.password, proxy.host, proxy.port);
} else {
proxyUrl = std::format("http://{}:{}", proxy.host, proxy.port);
}
}
proxyArgs.push_back("--proxy");
proxyArgs.push_back(proxyUrl);
return proxyArgs;
}
Api::Result<void> YtDlpManager::downloadYtDlp() {
@@ -374,50 +518,86 @@ std::vector<std::string> YtDlpManager::getCookieArgs() const {
}
std::string YtDlpManager::execute(const std::vector<std::string>& args) const {
if (!std::filesystem::exists(executablePath_)) {
return "";
}
std::string cmd = executablePath_.string();
// Add cookie arguments first
auto cookieArgs = getCookieArgs();
for (const auto& arg : cookieArgs) {
cmd += " ";
// Simple escaping
if (arg.find(' ') != std::string::npos ||
arg.find('"') != std::string::npos ||
arg.find('\\') != std::string::npos) {
std::string escaped;
for (char c : arg) {
if (c == '"') escaped += "\\";
escaped += c;
}
cmd += "\"" + escaped + "\"";
} else {
cmd += arg;
}
}
for (const auto& arg : args) {
cmd += " ";
// Simple escaping - wrap in quotes if contains spaces or special chars
if (arg.find(' ') != std::string::npos ||
arg.find('"') != std::string::npos ||
arg.find('\\') != std::string::npos) {
// Escape existing quotes and wrap
std::string escaped;
for (char c : arg) {
if (c == '"') escaped += "\\";
escaped += c;
}
cmd += "\"" + escaped + "\"";
} else {
cmd += arg;
}
}
// Suppress stderr
return executeInternal(args).output;
}
YtDlpResult
YtDlpManager::executeInternal(const std::vector<std::string>& args) const
{
YtDlpResult result;
result.success = false;
result.exitCode = -1;
if (!std::filesystem::exists(executablePath_)) {
return result;
}
auto startTime = std::chrono::steady_clock::now();
std::string cmd = buildCommand(args);
ECHOION_LOG_DEBUG(std::format("Executing yt-dlp: {}", cmd.substr(0, 200)));
std::array<char, 8192> buffer;
std::string output;
#ifdef _WIN32
FILE* pipe = _popen(cmd.c_str(), "r");
#else
FILE* pipe = popen(cmd.c_str(), "r");
#endif
if (!pipe) {
ECHOION_LOG_WARNING("Failed to open pipe for yt-dlp execution");
return result;
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
output += buffer.data();
}
#ifdef _WIN32
result.exitCode = _pclose(pipe);
#else
result.exitCode = pclose(pipe);
if (WIFEXITED(result.exitCode)) {
result.exitCode = WEXITSTATUS(result.exitCode);
}
#endif
auto endTime = std::chrono::steady_clock::now();
result.executionTime =
std::chrono::duration_cast<std::chrono::milliseconds>(endTime -
startTime);
result.output = std::move(output);
result.success = (result.exitCode == 0);
return result;
}
std::string
YtDlpManager::buildCommand(const std::vector<std::string>& args) const
{
std::string cmd = executablePath_.string();
// Add proxy arguments first (before cookies)
auto proxyArgs = getProxyArgs();
for (const auto& arg : proxyArgs) {
cmd += " ";
cmd += escapeArg(arg);
}
// Add cookie arguments
auto cookieArgs = getCookieArgs();
for (const auto& arg : cookieArgs) {
cmd += " ";
cmd += escapeArg(arg);
}
for (const auto& arg : args) {
cmd += " ";
cmd += escapeArg(arg);
}
// Suppress stderr
#ifdef _WIN32
cmd += " 2>nul";
#else
@@ -433,24 +613,73 @@ std::string YtDlpManager::execute(const std::vector<std::string>& args) const {
#elif defined(__APPLE__)
cmd = std::format("gtimeout {} {} 2>/dev/null || {}", timeoutSec, cmd, cmd);
#endif
ECHOION_LOG_DEBUG(std::format("Executing yt-dlp: {}", cmd));
std::array<char, 8192> buffer;
std::string result;
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) {
ECHOION_LOG_WARNING("Failed to open pipe for yt-dlp execution");
return "";
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
pclose(pipe);
return result;
return cmd;
}
std::string YtDlpManager::escapeArg(const std::string& arg)
{
// Simple escaping - wrap in quotes if contains spaces or special chars
if (arg.find(' ') != std::string::npos ||
arg.find('"') != std::string::npos ||
arg.find('\\') != std::string::npos ||
arg.find('>') != std::string::npos ||
arg.find('<') != std::string::npos ||
arg.find('|') != std::string::npos ||
arg.find('&') != std::string::npos ||
arg.find('$') != std::string::npos ||
arg.find('`') != std::string::npos) {
// Escape existing quotes and wrap
std::string escaped;
for (char c : arg) {
if (c == '"') {
escaped += "\\";
}
escaped += c;
}
return "\"" + escaped + "\"";
}
return arg;
}
std::future<YtDlpResult>
YtDlpManager::executeAsync(const std::vector<std::string>& args)
{
std::packaged_task<YtDlpResult()> task(
[this, args]() { return executeInternal(args); });
auto future = task.get_future();
{
std::lock_guard<std::mutex> lock(taskMutex_);
taskQueue_.push(std::move(task));
}
taskCv_.notify_one();
return future;
}
std::vector<YtDlpResult> YtDlpManager::executeBatch(
const std::vector<std::vector<std::string>>& argsList)
{
std::vector<std::future<YtDlpResult>> futures;
futures.reserve(argsList.size());
// Launch all tasks
for (const auto& args : argsList) {
futures.push_back(executeAsync(args));
}
// Collect results
std::vector<YtDlpResult> results;
results.reserve(argsList.size());
for (auto& future : futures) {
results.push_back(future.get());
}
return results;
}
} // namespace Echoion::Core