Added remove entry function

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-30 13:22:27 +02:00
parent 189dc71c32
commit d9a012f974
4 changed files with 89 additions and 0 deletions

View File

@ -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...";

View File

@ -94,6 +94,12 @@ namespace Downloader
/// </summary>
static bool ClearDownloadCache();
/// <summary>
/// Will remove an individual download entry by its tubio id
/// </summary>
/// <returns></returns>
static bool RemoveFromCacheByID(std::string id);
private:
static void Save();
static void Load();

View File

@ -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;
}

View File

@ -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);