ffmpeg_web_local_app/source/utils.cpp


Home Back
#include "fmt/base.h"
#include <crow/json.h>
#include <fmt/core.h>
#include <fmt/base.h>
#include <filesystem>
#include <system_error>


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!"); 
       // json[0]["error"]="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
}

Powered by Code, a simple repository browser by Fabio Di Matteo