Added query get_disk_usage

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-28 20:01:50 +02:00
parent 336881db75
commit 25c623205e
10 changed files with 147 additions and 32 deletions

View File

@ -48,27 +48,27 @@ std::string DownloadManager::QueueDownload(std::string url, DOWNLOAD_MODE mode)
{
if ((j.AsJson.DoesExist("title")) && (j.AsJson["title"].GetDataType() == JDType::STRING))
{
newDownload.title = j["title"];
newDownload.title = j["title"].AsString;
}
if ((j.AsJson.DoesExist("description")) && (j.AsJson["description"].GetDataType() == JDType::STRING))
{
newDownload.description = j["description"];
newDownload.description = j["description"].AsString;
}
if ((j.AsJson.DoesExist("uploader")) && (j.AsJson["uploader"].GetDataType() == JDType::STRING))
{
newDownload.uploader = j["uploader"];
newDownload.uploader = j["uploader"].AsString;
}
if ((j.AsJson.DoesExist("duration")) && (j.AsJson["duration"].GetDataType() == JDType::INT))
{
newDownload.duration = j["duration"];
newDownload.duration = j["duration"].AsInt;
}
if ((j.AsJson.DoesExist("webpage_url")) && (j.AsJson["webpage_url"].GetDataType() == JDType::STRING))
{
newDownload.webpage_url = j["webpage_url"];
newDownload.webpage_url = j["webpage_url"].AsString;
}
if ((j.AsJson.DoesExist("thumbnails")) && (j.AsJson["thumbnails"].GetDataType() == JDType::ARRAY))
@ -79,11 +79,11 @@ std::string DownloadManager::QueueDownload(std::string url, DOWNLOAD_MODE mode)
if (thumbnails.Size() > 1)
{
// If we have more than one thumbnail to choose from, choose the second-highes quality.
newDownload.thumbnail_url = thumbnails[thumbnails.Size() - 2]["url"];
newDownload.thumbnail_url = thumbnails[thumbnails.Size() - 2]["url"].AsString;
}
else
{
newDownload.thumbnail_url = thumbnails[thumbnails.Size() - 1]["url"];
newDownload.thumbnail_url = thumbnails[thumbnails.Size() - 1]["url"].AsString;
}
}
}
@ -321,7 +321,7 @@ DownloadEntry& DownloadManager::GetDownloadEntryByTubioID(std::string tubioId)
if (saveFileCache_Atomic[i].tubio_id == tubioId) return saveFileCache_Atomic[i];
}
throw std::exception("TubioID not found!");
throw std::exception();
std::terminate();
}
@ -451,52 +451,52 @@ std::vector<DownloadEntry> DownloadManager::ParseJsonArrayToEntries(const JasonP
if ((iter.DoesExist("title")) && (iter["title"].GetDataType() == JDType::STRING))
{
newEntry.title = iter["title"];
newEntry.title = iter["title"].AsString;
}
if ((iter.DoesExist("description")) && (iter["description"].GetDataType() == JDType::STRING))
{
newEntry.description = iter["description"];
newEntry.description = iter["description"].AsString;
}
if ((iter.DoesExist("uploader")) && (iter["uploader"].GetDataType() == JDType::STRING))
{
newEntry.uploader = iter["uploader"];
newEntry.uploader = iter["uploader"].AsString;
}
if ((iter.DoesExist("duration")) && (iter["duration"].GetDataType() == JDType::INT))
{
newEntry.duration = iter["duration"];
newEntry.duration = iter["duration"].AsInt;
}
if ((iter.DoesExist("tubio_id")) && (iter["tubio_id"].GetDataType() == JDType::STRING))
{
newEntry.tubio_id = iter["tubio_id"];
newEntry.tubio_id = iter["tubio_id"].AsString;
}
if ((iter.DoesExist("webpage_url")) && (iter["webpage_url"].GetDataType() == JDType::STRING))
{
newEntry.webpage_url = iter["webpage_url"];
newEntry.webpage_url = iter["webpage_url"].AsString;
}
if ((iter.DoesExist("thumbnail_url")) && (iter["thumbnail_url"].GetDataType() == JDType::STRING))
{
newEntry.thumbnail_url = iter["thumbnail_url"];
newEntry.thumbnail_url = iter["thumbnail_url"].AsString;
}
if ((iter.DoesExist("download_url")) && (iter["download_url"].GetDataType() == JDType::STRING))
{
newEntry.download_url = iter["download_url"];
newEntry.download_url = iter["download_url"].AsString;
}
if ((iter.DoesExist("downloaded_filename")) && (iter["downloaded_filename"].GetDataType() == JDType::STRING))
{
newEntry.downloaded_filename = iter["downloaded_filename"];
newEntry.downloaded_filename = iter["downloaded_filename"].AsString;
}
if ((iter.DoesExist("queued_timestamp")) && (iter["queued_timestamp"].GetDataType() == JDType::INT))
{
newEntry.queued_timestamp = iter["queued_timestamp"];
newEntry.queued_timestamp = iter["queued_timestamp"].AsInt;
}
if ((iter.DoesExist("mode")) && (iter["mode"].GetDataType() == JDType::STRING))
@ -529,7 +529,7 @@ std::string DownloadManager::CreateNewTubioID()
while (!isIdUnique)
{
if (counter > 100000000) throw std::exception("Tubio download id generator timeout");
if (counter > 100000000) throw std::exception();
newId = Internal::Helpers::Base10_2_X(time(0), "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");

View File

@ -9,7 +9,7 @@ std::string FileSystem::ReadFile(std::string filename)
ifs.open(filename, std::ios::in);
if (!ifs.good())
{
throw std::exception("no such file");
throw std::exception();
std::terminate();
}
std::string buf;
@ -86,15 +86,17 @@ bool FileSystem::Delete(std::string filename)
}
#include <iostream>
long long int FileSystem::CalculateSize(std::string name)
long long int FileSystem::CalculateSize(std::string name, bool isDirectory)
{
#ifdef _WIN
// Windows implementation
WIN32_FIND_DATAA data;
HANDLE sh = NULL;
long long int byteCount = 0;
//std::cout << "Scanning dir " << name << std::endl;
sh = FindFirstFileA((name + "\\*").c_str(), &data);
sh = FindFirstFileA((name + ((isDirectory) ? "\\*" : "")).c_str(), &data);
if (sh == INVALID_HANDLE_VALUE) return -1;
@ -106,7 +108,7 @@ long long int FileSystem::CalculateSize(std::string name)
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
// ... a directory, search it recursively
byteCount += CalculateSize(name + "\\" + data.cFileName);
byteCount += CalculateSize(name + "\\" + data.cFileName, true);
}
else
{
@ -121,6 +123,23 @@ long long int FileSystem::CalculateSize(std::string name)
return byteCount;
#else
// Linux implementation
std::string cmd("du -sb '");
cmd.append(name);
cmd.append("' | cut -f1 2>&1");
// execute above command and get the output
FILE* stream = popen(cmd.c_str(), "r");
if (stream) {
const int max_size = 256;
char readbuf[max_size];
if (fgets(readbuf, max_size, stream) != NULL) {
return atoll(readbuf);
}
pclose(stream);
}
// return error val
return -1;
#endif
}

View File

@ -3,6 +3,7 @@
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <filesystem>
class FileSystem
@ -18,7 +19,8 @@ public:
static bool _CreateDirectory(std::string name);
static bool CreateDirectoryIfNotExists(std::string name);
static bool DeleteDirectory(std::string name);
static long long int CalculateSize(std::string name);
static long long int CalculateSize(std::string name, bool isDirectory = false);
private:
};

View File

@ -40,10 +40,15 @@ std::string Logger::Flush()
time_t currTime = time(0);
char timeBuf[256];
#ifdef _WIN
tm currTm;
localtime_s(&currTm, &currTime);
char timeBuf[256];
strftime(timeBuf, 100, "%d.%m.%Y - %T", &currTm);
#else
tm* currTm = localtime(&currTime);
strftime(timeBuf, 100, "%d.%m.%Y - %T", currTm);
#endif
std::stringstream bufOut;
bufOut << "<" << timeBuf << "> [" << identifier << "]" << TypeToPrefix(type) << ((additional_information.length() > 0) ? " " : "") << additional_information << ": " << cout.str();

View File

@ -30,6 +30,7 @@ bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json&
else if (requestName == "queue_download") return QueueDownload(requestBody, responseBody, responseCode);
else if (requestName == "fetch_session_cache") return FetchSessionCache(requestBody, responseBody, responseCode);
else if (requestName == "fetch_alltime_cache") return FetchAlltimeCache(requestBody, responseBody, responseCode);
else if (requestName == "get_disk_usage") return GetDiskUsage(requestBody, responseBody, responseCode);
else if (requestName == "clear_download_cache") return ClearDownloadCache(requestBody, responseBody, responseCode);
else if (requestName == "foo") return Example_Foo(requestBody, responseBody, responseCode);
else if (requestName == "show_console") return ShowConsole(requestBody, responseBody, responseCode);
@ -280,6 +281,67 @@ bool RestQueryHandler::FetchAlltimeLogs(const JsonBlock& request, JsonBlock& res
return true;
}
bool RestQueryHandler::GetDiskUsage(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
log->cout << "Fetching disk usage...";
log->Flush();
responseCode = OK;
responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK));
JsonBlock diskUsages;
long long int dlcache = 0;
if (FileSystem::ExistsDirectory(XGConfig::downloader.cachedir))
{
dlcache = FileSystem::CalculateSize(XGConfig::downloader.cachedir, true);
}
diskUsages.Set("dlcache") = dlcache;
long long int logs = 0;
if (FileSystem::Exists("log.txt"))
{
logs += FileSystem::CalculateSize("log.txt");
}
if (FileSystem::Exists("log.json"))
{
logs += FileSystem::CalculateSize("log.json");
}
diskUsages.Set("logs") = logs;
long long int misc = 0;
if (FileSystem::Exists("config.json"))
{
misc += FileSystem::CalculateSize("config.json");
}
diskUsages.Set("misc") = misc;
long long int dependencies = 0;
if (FileSystem::Exists("ffmpeg.exe"))
{
dependencies += FileSystem::CalculateSize("ffmpeg.exe");
}
if (FileSystem::Exists("ffprobe.exe"))
{
dependencies += FileSystem::CalculateSize("ffprobe.exe");
}
if (FileSystem::Exists("ffplay.exe"))
{
dependencies += FileSystem::CalculateSize("ffplay.exe");
}
if (FileSystem::Exists("youtube-dl.exe"))
{
dependencies += FileSystem::CalculateSize("youtube-dl.exe");
}
diskUsages.Set("dependencies") = dependencies;
diskUsages.Set("total") = dlcache + logs + misc + dependencies;
responseBody.Set("disk_usage") = diskUsages;
responseBody.Set("message") = "Disk usage in bytes. Dependencies are 0 on linux, because the dependencies should be installed globally.";
return true;
}

View File

@ -29,6 +29,7 @@ namespace Rest
static bool GetOSName(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool FetchAlltimeLogs(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool FetchSessionLogs(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool GetDiskUsage(const JasonPP::JsonBlock& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool ValidateField(const std::string name, const JasonPP::JDType type, const JasonPP::Json& checkThat, JasonPP::JsonBlock& putErrorResponseHere);

View File

@ -86,7 +86,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WIN;_WIN32;JASONPP_RENDER_SORTED;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;JASONPP_RENDER_SORTED;_WIN32;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -101,7 +101,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WIN;_WIN32;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;JASONPP_RENDER_SORTED;_WIN32;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -116,7 +116,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WIN;_WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;JASONPP_RENDER_SORTED;_WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -131,7 +131,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_WIN;_WIN64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN;JASONPP_RENDER_SORTED;_WIN64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>

View File

@ -2,9 +2,6 @@
int main()
{
std::cout << (FileSystem::CalculateSize("C:\\Users\\Leon\\source\\repos\\Tubio\\Tubio") / 1024 / 1024) << std::endl;
return 0;
Framework framework;
framework.Run();

29
linux_build/build.sh Normal file
View File

@ -0,0 +1,29 @@
#!/bin/bash
g++ \
\
-std=c++17 \
\
-D __linux__ \
-D JASONPP_RENDER_SORTED \
\
../Tubio/main.cpp \
../Tubio/ConsoleManager.cpp \
../Tubio/DownloadManager.cpp \
../Tubio/FileSystem.cpp \
../Tubio/Framework.cpp \
../Tubio/HttpServer.cpp \
../Tubio/Logger.cpp \
../Tubio/LogHistory.cpp \
../Tubio/RestQueryHandler.cpp \
../Tubio/RestResponseTemplates.cpp \
../Tubio/XGConfig.cpp \
../Tubio/XGControl.cpp \
\
\
../Tubio/external_dependencies/casenta/mongoose/mongoose.c \
../Tubio/external_dependencies/leonetienne/JasonPP/JasonPP.cpp \
\
-lpthread \
\
-w \
-o ./tubio.out

BIN
linux_build/tubio.out Normal file

Binary file not shown.