Move logger to file and adjust path string in publish contoller

This commit is contained in:
Hepatica
2024-08-11 01:59:54 +02:00
parent 4e94641571
commit 1f3086c96c
5 changed files with 204 additions and 127 deletions

View File

@@ -14,9 +14,8 @@ private:
FileProcessingService file_processing;
//std::string publish_app_path = "/mnt/c/Users/Danil/SpCloudApp";//Todo change to linux path
//std::string publish_app_path = "/home/danilt2000/SpCloudMain/SpCloudApp";//Todo change to linux path
std::string publish_app_path = "/home/danilt2000/SpCloud/";//Todo change to linux path
std::string publish_app_path = "/mnt/c/Users/Danil/SpCloudApp";
//std::string publish_app_path = "/home/danilt2000/SpCloud/";
//std::string publish_app_path = "C:/Temps/";// Todo delete if not needed
public:
@@ -32,67 +31,69 @@ public:
//httplib::Headers test = req.headers;//Todo add processing header for authorization layer
});
}
private:
void process_publish(const httplib::Request& req, httplib::Response& res)
private:
void process_publish(const httplib::Request& req, httplib::Response& res)
{
if (this->authorization.is_user_authorized())
{
if (this->authorization.is_user_authorized())
{
const auto& content = req.files.begin()->second.content;
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
if (file_processing.save_file(filename, content)) {
std::string random_string = generate_random_string(20);//Todo think about change
file_processing.unzip(filename, this->publish_app_path + random_string);
this->dotnet_publish(this->publish_app_path + random_string);
res.set_content("File uploaded successfully: " + filename, "text/plain");
}
else {
res.status = 500;
res.set_content("Failed to save file, please ensure you are putting rar file"
+ filename, "text/plain");
}
const auto& content = req.files.begin()->second.content;
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
//if (file_processing.save_file_with_retry(filename, content)) {
if (file_processing.save_file(filename, content)) {
//Todo uncommit later
//std::string random_string = generate_random_string(20);//Todo think about change
//file_processing.unzip(filename, this->publish_app_path + random_string);
//this->dotnet_publish(this->publish_app_path + random_string);
res.set_content("File uploaded successfully: " + filename, "text/plain");
}
else {
res.status = 400;
res.set_content("Invalid file type. Only .rar files are allowed.",
"text/plain");
res.status = 500;
res.set_content("Failed to save file, please ensure you are putting rar file"
+ filename, "text/plain");
}
}
else
{
//Todo add logging and exiting from function with bead request
else {
res.status = 400;
res.set_content("Invalid file type. Only .rar files are allowed.",
"text/plain");
}
}
void dotnet_publish(const std::string& path)
else
{
std::string dll_file_name = file_processing.find_file_by_suffix(path, "dll");
std::string command = R"(dotnet )" + path + "/" + dll_file_name;
std::thread commandThread(&CommandService::execute_command, command);
commandThread.detach();
//Todo add logging and exiting from function with bead request
}
static std::string generate_random_string(size_t length, const std::string& char_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> distribution(0, char_set.size() - 1);
std::string random_string;
for (size_t i = 0; i < length; ++i) {
char random_char = char_set[distribution(generator)];
random_string += random_char;
}
return random_string;
}
void dotnet_publish(const std::string& path)
{
std::string dll_file_name = file_processing.find_file_by_suffix(path, "dll");
std::string command = R"(dotnet )" + path + "/" + dll_file_name;
std::thread commandThread(&CommandService::execute_command, command);
commandThread.detach();
}
static std::string generate_random_string(size_t length, const std::string& char_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> distribution(0, char_set.size() - 1);
std::string random_string;
for (size_t i = 0; i < length; ++i) {
char random_char = char_set[distribution(generator)];
random_string += random_char;
}
return random_string;
}
};