diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp index b67fd18..e0210a6 100644 --- a/Tubio/RestQueryHandler.cpp +++ b/Tubio/RestQueryHandler.cpp @@ -14,6 +14,7 @@ void RestQueryHandler::PreInit() bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode) { + // Print the client ip address alongide every log log->SetAdditionalInformation(std::string("@") + clientAdress); if (!ValidateField("request", JDType::STRING, request, responseBody)) @@ -39,6 +40,7 @@ bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json& else if (requestName == "get_os_name") return GetOSName(requestBody, responseBody, responseCode); else if (requestName == "fetch_session_logs") return FetchSessionLogs(requestBody, responseBody, responseCode); else if (requestName == "fetch_alltime_logs") return FetchAlltimeLogs(requestBody, responseBody, responseCode); + else if (requestName == "update_dep_youtubedl") return UpdateYoutubeDL(requestBody, responseBody, responseCode); @@ -112,7 +114,12 @@ bool RestQueryHandler::FetchSessionCache(const JsonBlock& request, JsonBlock& re JsonBlock dummy; if (ValidateField("max_age", JDType::INT, request, dummy)) { - max_age = request["max_age"].AsInt; + // Only respect the given max_age, if the age does not exceed tubio boot-time! + // This way we can ensure that only entries of this SESSION will appear! + if (request["max_age"].AsInt >= 0) + { + max_age = min(request["max_age"].AsInt, max_age); + } } if (ValidateField("max_num", JDType::INT, request, dummy)) { @@ -357,7 +364,43 @@ bool RestQueryHandler::ClearLogs(const JsonBlock& request, JsonBlock& responseBo return true; } +bool RestQueryHandler::UpdateYoutubeDL(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode) +{ + log->cout << "Updating youtube-dl..."; + log->Flush(); + std::string result = Updater::UpdateYoutubeDL(); + if (result == "OK") + { + log->cout << " => OK!"; + log->Flush(); + + responseCode = OK; + responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK)); + responseBody.Set("message") = "Updated youtube-dl.exe successfully!"; + } + else if (result == "not implemented") + { + log->cout << " => NOT_IMPLEMENTED!"; + log->Flush(); + + log->Flush(); + responseCode = NOT_IMPLEMENTED; + responseBody.CloneFrom(RestResponseTemplates::GetByCode(NOT_IMPLEMENTED)); + responseBody.Set("message") = "On linux you have to update youtube-dl yourself since it is a system-wide package handled by various package managers!"; + } + else // Some other error + { + log->cout << " => urlmon error: " << result; + log->Flush(); + + responseCode = INTERNAL_SERVER_ERROR; + responseBody.CloneFrom(RestResponseTemplates::GetByCode(INTERNAL_SERVER_ERROR)); + responseBody.Set("message") = "Unable do update youtube-dl.exe! See urlmon " + result; + } + + return true; +} diff --git a/Tubio/RestQueryHandler.h b/Tubio/RestQueryHandler.h index a698904..0a35bb5 100644 --- a/Tubio/RestQueryHandler.h +++ b/Tubio/RestQueryHandler.h @@ -5,6 +5,7 @@ #include "Logger.h" #include "DownloadManager.h" #include "ConsoleManager.h" +#include "Updater.h" namespace Rest { @@ -31,6 +32,7 @@ namespace Rest 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 UpdateYoutubeDL(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); diff --git a/Tubio/Tubio.vcxproj b/Tubio/Tubio.vcxproj index 6b86f63..8c8a11b 100644 --- a/Tubio/Tubio.vcxproj +++ b/Tubio/Tubio.vcxproj @@ -93,6 +93,7 @@ Console true + urlmon.lib;%(AdditionalDependencies) @@ -110,6 +111,7 @@ true true true + urlmon.lib;%(AdditionalDependencies) @@ -123,6 +125,7 @@ Console true + urlmon.lib;%(AdditionalDependencies) @@ -140,6 +143,7 @@ true true true + urlmon.lib;%(AdditionalDependencies) @@ -155,6 +159,7 @@ + @@ -171,6 +176,7 @@ + diff --git a/Tubio/Tubio.vcxproj.filters b/Tubio/Tubio.vcxproj.filters index f178d03..1420e2d 100644 --- a/Tubio/Tubio.vcxproj.filters +++ b/Tubio/Tubio.vcxproj.filters @@ -60,6 +60,9 @@ _external_dependencies + + Quelldateien + @@ -104,5 +107,8 @@ _external_dependencies + + Headerdateien + \ No newline at end of file diff --git a/Tubio/Updater.cpp b/Tubio/Updater.cpp new file mode 100644 index 0000000..caac927 --- /dev/null +++ b/Tubio/Updater.cpp @@ -0,0 +1,19 @@ +#include "Updater.h" + +std::string Updater::UpdateYoutubeDL() +{ +#ifdef _WIN + HRESULT res = URLDownloadToFileA(NULL, "https://yt-dl.org/downloads/latest/youtube-dl.exe", "youtube-dl.exe", 0, NULL); + + if (SUCCEEDED(res)) + { + return "OK"; + } + else + { + return "error code: 0x" + (JasonPP::Internal::Helpers::Base10_2_X(res, "0123456789abcdef")); + } +#else +#endif + return "not implemented"; +} diff --git a/Tubio/Updater.h b/Tubio/Updater.h new file mode 100644 index 0000000..6924be5 --- /dev/null +++ b/Tubio/Updater.h @@ -0,0 +1,17 @@ +#pragma once +#ifdef _WIN +#include +#include +#include +#include "external_dependencies/leonetienne/JasonPP/JasonPP.hpp" // We need Internal::Helpers +#endif + +class Updater +{ +public: + /// + /// Will update youtube-dl.exe on windows only!! Returns error message. On linux, you have to update it yourself, since it is a package of its own! + /// + /// + static std::string UpdateYoutubeDL(); +}; diff --git a/Tubio_rest.postman_collection.json b/Tubio_rest.postman_collection.json index 09b8112..f7c823e 100644 --- a/Tubio_rest.postman_collection.json +++ b/Tubio_rest.postman_collection.json @@ -93,7 +93,7 @@ "header": [], "body": { "mode": "raw", - "raw": "{\r\n \"request\": \"fetch_session_cache\",\r\n \"max_age\": -1,\r\n \"max_num\": -1\r\n}", + "raw": "{\r\n \"request\": \"fetch_session_cache\",\r\n \"max-num\": -1\r\n}\r\n", "options": { "raw": { "language": "json" @@ -391,6 +391,33 @@ } }, "response": [] + }, + { + "name": "UpdateYotubeDL_Dependency", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"request\": \"update_dep_youtubedl\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:6969/api", + "host": [ + "localhost" + ], + "port": "6969", + "path": [ + "api" + ] + } + }, + "response": [] } ], "protocolProfileBehavior": {}