diff --git a/Tubio/DownloadManager.cpp b/Tubio/DownloadManager.cpp index 0ac8d39..596c176 100644 --- a/Tubio/DownloadManager.cpp +++ b/Tubio/DownloadManager.cpp @@ -355,6 +355,60 @@ bool DownloadManager::ClearDownloadCache() return false; } +bool DownloadManager::RemoveFromCacheByID(std::string id) +{ + bool removedAny = false; + std::string filePath = ""; + bool wasFinished = false; + for (std::size_t i = 0; i < unfinishedCache.size(); i++) + { + if (unfinishedCache[i].tubio_id == id) + { + filePath = unfinishedCache[i].downloaded_filename; + removedAny = true; + // Edgecase + wasFinished = unfinishedCache[i].status == DOWNLOAD_STATUS::FINISHED; + unfinishedCache.erase(unfinishedCache.begin() + i); + break; + } + } + // It finds it either in the lower two for loops, or in the first one. + for (std::size_t i = 0; i < saveFileCache.Size(); i++) + { + if ((saveFileCache[i].GetDataType() == JDType::JSON) && + (saveFileCache[i].AsJson.DoesExist("tubio_id")) && + (saveFileCache[i]["tubio_id"].GetDataType() == JDType::STRING)) + { + if (saveFileCache[i]["tubio_id"].AsString == id) + { + saveFileCache.RemoveAt(i); + removedAny = true; + break; + } + } + } + for (std::size_t i = 0; i < saveFileCache_Atomic.size(); i++) + { + if (saveFileCache_Atomic[i].tubio_id == id) + { + filePath = saveFileCache_Atomic[i].downloaded_filename; + removedAny = true; + wasFinished = saveFileCache_Atomic[i].status == DOWNLOAD_STATUS::FINISHED; + saveFileCache_Atomic.erase(saveFileCache_Atomic.begin() + i); + break; + } + } + + if ((wasFinished) && (FileSystem::Exists(filePath))) + { + FileSystem::Delete(filePath); + } + + Save(); + + return false; +} + void DownloadManager::Save() { log->cout << "Saving..."; diff --git a/Tubio/DownloadManager.h b/Tubio/DownloadManager.h index 7213187..7011679 100644 --- a/Tubio/DownloadManager.h +++ b/Tubio/DownloadManager.h @@ -94,6 +94,12 @@ namespace Downloader /// static bool ClearDownloadCache(); + /// + /// Will remove an individual download entry by its tubio id + /// + /// + static bool RemoveFromCacheByID(std::string id); + private: static void Save(); static void Load(); diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp index e0210a6..ec61e40 100644 --- a/Tubio/RestQueryHandler.cpp +++ b/Tubio/RestQueryHandler.cpp @@ -41,6 +41,7 @@ bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json& 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); + else if (requestName == "remove_download_entry") return RemoveDownloadEntry(requestBody, responseBody, responseCode); @@ -402,7 +403,34 @@ bool RestQueryHandler::UpdateYoutubeDL(const JsonBlock& request, JsonBlock& resp return true; } +bool RestQueryHandler::RemoveDownloadEntry(const JsonBlock& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode) +{ + if (!ValidateField("id", JDType::STRING, request, responseBody)) + { + responseCode = BAD_REQUEST; + return false; + } + log->cout << "Removing download id " << request["id"].AsString << "..."; + log->Flush(); + + bool didSucceed = Downloader::DownloadManager::RemoveFromCacheByID(request["id"].AsString); + + if (didSucceed) + { + responseCode = OK; + responseBody.CloneFrom(RestResponseTemplates::GetByCode(OK)); + responseBody.Set("message") = "Successfully removed."; + } + else + { + responseCode = BAD_REQUEST; + responseBody.CloneFrom(RestResponseTemplates::GetByCode(BAD_REQUEST)); + responseBody.Set("message") = "Failed."; + } + + return true; +} diff --git a/Tubio/RestQueryHandler.h b/Tubio/RestQueryHandler.h index 0a35bb5..5241daf 100644 --- a/Tubio/RestQueryHandler.h +++ b/Tubio/RestQueryHandler.h @@ -33,6 +33,7 @@ namespace Rest 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 RemoveDownloadEntry(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);