add: Task Status

This commit is contained in:
Simon Hardt
2021-05-31 23:19:02 +02:00
parent 98fb1349a1
commit e12fbfa2aa
12 changed files with 1222 additions and 147 deletions

View File

@@ -8,6 +8,8 @@
#include <fstream>
#include <functional>
#include <soci/boost-optional.h>
#include <ctre.hpp>
#include <fmt/chrono.h>
#include <fmt/ostream.h>
@@ -30,7 +32,9 @@ void Api::RegisterServerHandles(httplib::Server& server)
server.Get("/api/files", std::bind_front(&Api::files, this));
server.Get("/api/file/([A-E0-9]+)", std::bind_front(&Api::file, this));
server.Get("/api/file/([A-F0-9]+)", std::bind_front(&Api::file, this));
server.Get("/api/status/([A-F0-9]+)", std::bind_front(&Api::status, this));
server.Options("/(.*)", [this](httplib::Request const& rq, httplib::Response& rp) {});
@@ -205,6 +209,70 @@ void Api::file(const httplib::Request& rq, httplib::Response& rs)
rs.set_content("File Not Found", "text/plain");
}
}
void Api::status(httplib::Request const& rq, httplib::Response& rs)
{
if (!rq.matches[1].matched)
{
rs.status = 404;
rs.set_content("Parameter error", "text/plain");
return;
}
std::string hash = rq.matches[1];
boost::optional<Task> data;
sql << "SELECT * FROM Tasks WHERE hash=:hash", soci::use(hash, "hash"), soci::into(data);
/*soci::rowset<Task> data = (sql.prepare << "SELECT * "
"FROM Tasks "
"WHERE hash=:hash ",
soci::use(hash, "hash"));*/
json response;
auto& status = response["status"];
if (data)
{
auto const& el = data.value();
status["name"] = fmt::format("Task {}", el.hash);
status["source"] = el.url;
status["status"] = magic_enum::enum_name(el.status);
status["status_id"] = static_cast<int>(el.status);
status["timestamp"] = el.timestamp;
status["task_file_type"] =
fmt::format("{}: {}", el.audio_only ? "Audio" : "Video", el.format.value_or("Auto"));
status["id"] = el.hash;
if (el.status == FileStatus::COMPLETE)
{
boost::optional<File> file_data;
sql << "SELECT * FROM Files WHERE hash=:hash", soci::use(hash, "hash"),
soci::into(file_data);
if (file_data)
{
auto const& file = file_data.value();
auto& file_status = status["file"];
file_status["timestamp"] = file.timestamp;
file_status["name"] = file.filename;
file_status["format"] =
fmt::format("{}: {}", el.audio_only ? "Audio" : "Video", file.format);
}
}
} else
{
rs.status = 404;
response["error"] = fmt::format("File '{}' not found!", hash);
}
rs.set_content(response.dump(), "application/json");
}
void Api::set_cross_headers(httplib::Response& rs)
{
rs.set_header("Access-Control-Allow-Origin", "*");