ffmpeg_web_local_app/source/utils.cpp
#include "fmt/base.h"
#include "reproc++/arguments.hpp"
#include <fmt/core.h>
#include <fmt/base.h>
#include <filesystem>
#include <system_error>
#include <crow/json.h>
#include <crow/http_response.h>
#include <crow.h>
#include <fmt/core.h>
#include <string>
#include <reproc/reproc.h>
#include <reproc++/reproc.hpp>
#include <reproc++/run.hpp>
std::vector<std::string> splitByWhitespace(const std::string& text) {
std::istringstream stream(text);
std::vector<std::string> words;
std::string word;
while (stream >> word) {
words.push_back(word);
}
return words;
}
reproc::process run_process(const std::string& cmd){
reproc::process p;
reproc::arguments args(splitByWhitespace(cmd));
reproc::options opts;
opts.redirect.out.type = reproc::redirect::pipe;
if (!p.start(args,opts)){
auto res=p.pid();
if (res.second.value()!=0)
fmt::println("Error {} on run process! {}",res.second.value(), res.second.message());
}
return p;
}
void kill_all(const std::string& program){
#if defined(__linux__)
const std::string cmd =fmt::format("killall {}",program);
auto p=run_process(cmd);
#elif defined(_WIN32)
const std::string cmd =fmt::format("taskkill /IM {}.exe /F",program);
auto p=run_process(cmd);
#endif
}
void open_url(const std::string& url){
#if defined(__linux__)
const std::string cmd=fmt::format("xdg-open {}",url);
run_process(cmd);
#elif defined(_WIN32)
const std::string cmd = fmt::format("cmd /C start \"\" {}", url);
run_process(cmd);
#endif
}
std::string genPassword(const unsigned int len) {
const std::string caratteri = "abcdefghijklmnopqrstuvwxyz";
std::string password;
std::srand(static_cast<unsigned int>(std::time(0)));
for (unsigned int i = 0; i < len; ++i) {
int indice = std::rand() % caratteri.length();
password += caratteri[indice];
}
return password;
}
bool is_port_ready(const char* host, int port) {
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0)
return false;
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_port = htons(port);
if (inet_pton(AF_INET, host, &address.sin_addr) != 1) {
close(socket_fd);
return false;
}
bool is_ready = connect(
socket_fd,
reinterpret_cast<sockaddr*>(&address),
sizeof(address)
) == 0;
close(socket_fd);
return is_ready;
}
crow::json::wvalue::list open_dir(const std::string& d)
{
namespace fs = std::filesystem;
std::error_code ec;
const fs::path dir = d;
crow::json::wvalue::list json;
crow::json::wvalue item;
if (!fs::exists(dir, ec) || !fs::is_directory(dir, ec))
{
crow::json::wvalue error;
fmt::println("Path not exisist!");
error["error"] = "Path does not exist or is not a directory";
json.push_back(std::move(error));
return json;
}
for (const auto& entry : fs::directory_iterator(dir))
{
const auto file_name = entry.path().filename().string();
if (file_name[0]=='.') continue;
if (entry.is_directory()) {
fmt::println("Folder: {}",entry.path().string());
item["type"]="d";
item["path"]=entry.path();
json.push_back(std::move(item));
}
}
for (const auto& entry : fs::directory_iterator(dir))
{
const auto file_name = entry.path().filename().string();
if (file_name[0]=='.') continue;
if (entry.is_regular_file()) {
fmt::println("File: {}",entry.path().string());
item["type"]="f";
item["path"]=entry.path();
json.push_back(std::move(item));
}
}
return json;
}
std::string get_home()
{
// Get the user's home directory.
#ifdef _WIN32
const char* profile = std::getenv("USERPROFILE");
if (profile) {
return std::string(profile);
}
const char* drive = std::getenv("HOMEDRIVE");
const char* path = std::getenv("HOMEPATH");
if (drive && path) {
return std::string(drive) + std::string(path);
}
return {};
#else
const char* home = std::getenv("HOME");
return home ? std::string(home) : std::string{};
#endif
}
std::string get_out_file(const std::string& path, const std::string& new_ext)
{
namespace fs = std::filesystem;
fs::path p{path};
std::string folder=p.parent_path().string();
// std::string ext=p.extension().string();
std::string stem= p.stem().string();
std::string sub_ext="crow_convert_output";
const auto now = std::chrono::system_clock::now();
const auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(
now.time_since_epoch()
).count();
auto res=fmt::format("{}/{}.{}-{}.{}",folder,stem,sub_ext,timestamp,new_ext);
return res;
}
std::string get_xterminal() {
const char* de_env = std::getenv("XDG_CURRENT_DESKTOP");
if (de_env != nullptr) {
std::string de(de_env);
std::transform(de.begin(), de.end(), de.begin(), ::tolower);
if (de.find("gnome") != std::string::npos) return "gnome-terminal";
if (de.find("unity") != std::string::npos) return "gnome-terminal";
if (de.find("cinnamon") != std::string::npos) return "gnome-terminal";
if (de.find("budgie") != std::string::npos) return "gnome-terminal";
if (de.find("pop") != std::string::npos) return "gnome-terminal"; // Pop!_OS
if (de.find("zorin") != std::string::npos) return "gnome-terminal"; // Zorin OS
// KDE e Plasma
if (de.find("kde") != std::string::npos) return "konsole";
if (de.find("plasma") != std::string::npos) return "konsole";
if (de.find("kubuntu") != std::string::npos) return "konsole";
// XFCE
if (de.find("xfce") != std::string::npos) return "xfce4-terminal";
// MATE
if (de.find("mate") != std::string::npos) return "mate-terminal";
// LXQt / LXDE
if (de.find("lxqt") != std::string::npos) return "lxterminal";
if (de.find("lxde") != std::string::npos) return "lxterminal";
// Elementary OS / Pantheon
if (de.find("elementary") != std::string::npos) return "io.elementary.terminal";
if (de.find("pantheon") != std::string::npos) return "io.elementary.terminal";
// Deepin
if (de.find("deepin") != std::string::npos) return "deepin-terminal";
// Window Managers Wayland/Tiling
if (de.find("sway") != std::string::npos) return "foot";
if (de.find("hyprland") != std::string::npos) return "kitty";
if (de.find("i3") != std::string::npos) return "alacritty";
if (de.find("awesome") != std::string::npos) return "alacritty";
if (de.find("bspwm") != std::string::npos) return "alacritty";
if (de.find("dwm") != std::string::npos) return "st";
if (de.find("openbox") != std::string::npos) return "xterm";
if (de.find("fluxbox") != std::string::npos) return "xterm";
if (de.find("enlightenment") != std::string::npos) return "terminology";
if (de.find("efl") != std::string::npos) return "terminology";
}
std::vector<std::string> fallback_list = {
"kitty", "alacritty", "foot", "st", "konsole",
"gnome-terminal", "xfce4-terminal", "mate-terminal","sakura",
"lxterminal", "tilix", "terminator", "xterm"
};
for (const auto& term : fallback_list) {
std::string check_cmd = "which " + term + " > /dev/null 2>&1";
if (std::system(check_cmd.c_str()) == 0) {
return term;
}
}
return "xterm";
}