mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-09 20:19:35 +02:00
Add authorization file processing layers
This commit is contained in:
@@ -3,7 +3,14 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
# Add source to this project's executable.
|
# Add source to this project's executable.
|
||||||
add_executable (SpCloudMain "SpCloudMain.cpp" "SpCloudMain.h" )
|
add_executable(SpCloudMain
|
||||||
|
"SpCloudMain.cpp"
|
||||||
|
"SpCloudMain.h"
|
||||||
|
"Controllers/PublishController.cpp"
|
||||||
|
"Service/AuthorizationService.cpp"
|
||||||
|
"Service/FileProcessingService.cpp"
|
||||||
|
"Service/CommandService.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
if (CMAKE_VERSION VERSION_GREATER 3.12)
|
if (CMAKE_VERSION VERSION_GREATER 3.12)
|
||||||
set_property(TARGET SpCloudMain PROPERTY CXX_STANDARD 20)
|
set_property(TARGET SpCloudMain PROPERTY CXX_STANDARD 20)
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
//#pragma once
|
|
||||||
//#include "AuthorizationService.h"
|
|
||||||
//#include "FileProcessingService.h"
|
|
||||||
//#include "httplib.h"
|
//#include "httplib.h"
|
||||||
//#include <windows.h>
|
//#include <windows.h>
|
||||||
//#include "httplib.h"
|
//#include "Service/AuthorizationService.cpp"
|
||||||
|
//#include "Service/FileProcessingService.cpp"
|
||||||
|
|
||||||
|
#include "../Service/AuthorizationService.cpp"
|
||||||
|
#include "../Service/FileProcessingService.cpp"
|
||||||
|
|
||||||
class PublishController
|
class PublishController
|
||||||
{
|
{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*AuthorizationService authorization;
|
AuthorizationService authorization;
|
||||||
|
|
||||||
FileProcessingService file_processing;*/
|
FileProcessingService file_processing;
|
||||||
|
|
||||||
std::string publish_app_path = "C:/Temps/";
|
std::string publish_app_path = "C:/Temps/";
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PublishController(/*httplib::Server& svr, AuthorizationService authorization, FileProcessingService file_processing*/)
|
PublishController(/*httplib::Server& svr,*/ AuthorizationService authorization, FileProcessingService file_processing)
|
||||||
{
|
{
|
||||||
std::cout << "test" << "\n";
|
|
||||||
/*this->authorization = authorization;
|
/*this->authorization = authorization;
|
||||||
|
|
||||||
this->file_processing = file_processing;*/
|
this->file_processing = file_processing;*/
|
||||||
@@ -29,67 +29,67 @@ public:
|
|||||||
// //httplib::Headers test = req.headers;//Todo add processing header for authorization layer
|
// //httplib::Headers test = req.headers;//Todo add processing header for authorization layer
|
||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
//private:
|
//private:
|
||||||
// void process_publish(const httplib::Request& req, httplib::Response& res)
|
// 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& content = req.files.begin()->second.content;
|
||||||
//
|
//
|
||||||
// const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
|
// const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
|
||||||
//
|
//
|
||||||
// if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
|
// if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
|
||||||
// if (file_processing.save_file(filename, content)) {
|
// if (file_processing.save_file(filename, content)) {
|
||||||
//
|
//
|
||||||
// std::string random_string = generate_random_string(20);//Todo think about change
|
// std::string random_string = generate_random_string(20);//Todo think about change
|
||||||
//
|
//
|
||||||
// file_processing.unzip(filename, this->publish_app_path + random_string);
|
// file_processing.unzip(filename, this->publish_app_path + random_string);
|
||||||
//
|
//
|
||||||
// this->dotnet_publish(this->publish_app_path + random_string);
|
// this->dotnet_publish(this->publish_app_path + random_string);
|
||||||
//
|
//
|
||||||
// res.set_content("File uploaded successfully: " + filename, "text/plain");
|
// res.set_content("File uploaded successfully: " + filename, "text/plain");
|
||||||
// }
|
// }
|
||||||
// else {
|
// else {
|
||||||
// res.status = 500;
|
// res.status = 500;
|
||||||
// res.set_content("Failed to save file, please ensure you are putting rar file"
|
// res.set_content("Failed to save file, please ensure you are putting rar file"
|
||||||
// + filename, "text/plain");
|
// + filename, "text/plain");
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// else {
|
// else {
|
||||||
// res.status = 400;
|
// res.status = 400;
|
||||||
// res.set_content("Invalid file type. Only .rar files are allowed.",
|
// res.set_content("Invalid file type. Only .rar files are allowed.",
|
||||||
// "text/plain");
|
// "text/plain");
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// else
|
// else
|
||||||
// {
|
// {
|
||||||
// //Todo add logging and exiting from function with bead request
|
// //Todo add logging and exiting from function with bead request
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// void dotnet_publish(const std::string& path)
|
// void dotnet_publish(const std::string& path)
|
||||||
// {
|
// {
|
||||||
// std::string dll_file_name = file_processing.find_file_by_suffix(path, "dll");
|
// std::string dll_file_name = file_processing.find_file_by_suffix(path, "dll");
|
||||||
//
|
//
|
||||||
// std::string command = R"(dotnet )" + path + "/" + dll_file_name;
|
// std::string command = R"(dotnet )" + path + "/" + dll_file_name;
|
||||||
//
|
//
|
||||||
// std::thread commandThread(&CommandService::execute_command, command);
|
// std::thread commandThread(&CommandService::execute_command, command);
|
||||||
//
|
//
|
||||||
// commandThread.detach();
|
// commandThread.detach();
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// static std::string generate_random_string(size_t length, const std::string& char_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
|
// static std::string generate_random_string(size_t length, const std::string& char_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
|
||||||
// std::random_device rd;
|
// std::random_device rd;
|
||||||
// std::mt19937 generator(rd());
|
// std::mt19937 generator(rd());
|
||||||
// std::uniform_int_distribution<> distribution(0, char_set.size() - 1);
|
// std::uniform_int_distribution<> distribution(0, char_set.size() - 1);
|
||||||
//
|
//
|
||||||
// std::string random_string;
|
// std::string random_string;
|
||||||
// for (size_t i = 0; i < length; ++i) {
|
// for (size_t i = 0; i < length; ++i) {
|
||||||
// char random_char = char_set[distribution(generator)];
|
// char random_char = char_set[distribution(generator)];
|
||||||
// random_string += random_char;
|
// random_string += random_char;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// return random_string;
|
// return random_string;
|
||||||
// }
|
// }
|
||||||
};
|
};
|
||||||
10
SpCloudMain/Service/AuthorizationService.cpp
Normal file
10
SpCloudMain/Service/AuthorizationService.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
class AuthorizationService
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool is_user_authorized()
|
||||||
|
{
|
||||||
|
return true;//Todo implement logic for authorization processing
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
15
SpCloudMain/Service/CommandService.cpp
Normal file
15
SpCloudMain/Service/CommandService.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class CommandService
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void execute_command(const std::string& command) {
|
||||||
|
int result = std::system(command.c_str()); // NOLINT(concurrency-mt-unsafe)
|
||||||
|
if (result != 0) {
|
||||||
|
std::cerr << "Command failed with code: " << result << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
68
SpCloudMain/Service/FileProcessingService.cpp
Normal file
68
SpCloudMain/Service/FileProcessingService.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
// ReSharper disable CppClangTidyBugproneSuspiciousInclude
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "CommandService.cpp"
|
||||||
|
|
||||||
|
class FileProcessingService
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FileProcessingService()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool save_file(const std::string& filename, const std::string& content) {
|
||||||
|
std::ofstream ofs(filename, std::ios::binary);
|
||||||
|
if (!ofs) return false;
|
||||||
|
ofs << content;
|
||||||
|
return ofs.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
void create_directory(const std::string& path) {
|
||||||
|
std::filesystem::create_directories(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unzip(const std::string& file_path, const std::string& final_files_directory) {
|
||||||
|
create_directory(final_files_directory);
|
||||||
|
|
||||||
|
//Windows version
|
||||||
|
//std::string command = R"(powershell -Command "& \"C:\Program Files\WinRAR\WinRAR.exe\" x \")" + file_path + R"(\" \")" + final_files_directory + R"(\")";
|
||||||
|
|
||||||
|
//Linux version
|
||||||
|
std::string command = "unzip " + file_path + " -d " + final_files_directory;
|
||||||
|
|
||||||
|
std::thread commandThread(&CommandService::execute_command, command);
|
||||||
|
|
||||||
|
commandThread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string find_file_by_suffix(const std::string& directory, const std::string& suffix) {
|
||||||
|
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
||||||
|
if (entry.is_regular_file() && entry.path().filename().string().ends_with(suffix)) {
|
||||||
|
return entry.path().filename().string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""; //todo add throwing exception
|
||||||
|
}
|
||||||
|
|
||||||
|
//static void execute_command(const std::string& command) {//todo delete if not needed
|
||||||
|
// int result = std::system(command.c_str()); // NOLINT(concurrency-mt-unsafe)
|
||||||
|
// if (result != 0) {
|
||||||
|
// std::cerr << "Command failed with code: " << result << std::endl;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//void unzip(const std::string& file_path, const std::string& final_files_directory)
|
||||||
|
//{
|
||||||
|
// std::string createDirCommand = "mkdir \"" + final_files_directory + "\"";
|
||||||
|
|
||||||
|
// system(createDirCommand.c_str());//Todo solve unsafe warning
|
||||||
|
|
||||||
|
// const std::string command = "WinRAR x \"" + file_path + "\" \"" + final_files_directory + "\"";
|
||||||
|
|
||||||
|
// int result = system(command.c_str());//Todo solve unsafe warning
|
||||||
|
//}
|
||||||
|
};
|
||||||
@@ -4,8 +4,11 @@
|
|||||||
// ReSharper disable CppClangTidyBugproneSuspiciousInclude
|
// ReSharper disable CppClangTidyBugproneSuspiciousInclude
|
||||||
|
|
||||||
#include "SpCloudMain.h"
|
#include "SpCloudMain.h"
|
||||||
|
#include "httplib.h"
|
||||||
|
|
||||||
#include "Controllers/PublishController.cpp"
|
#include "Controllers/PublishController.cpp"
|
||||||
|
//#include "Service/AuthorizationService.cpp"
|
||||||
|
//#include "Service/FileProcessingService.cpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -13,6 +16,25 @@ int main()
|
|||||||
{
|
{
|
||||||
std::cout << "SpCloud start\n";
|
std::cout << "SpCloud start\n";
|
||||||
|
|
||||||
PublishController publish_controller;
|
httplib::Server svr;
|
||||||
|
|
||||||
|
svr.Get("/ping", [](const httplib::Request& req, httplib::Response& res)
|
||||||
|
{
|
||||||
|
res.set_content("Pong", "text/plain");
|
||||||
|
|
||||||
|
httplib::Headers test = req.headers;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
AuthorizationService authorization_service;
|
||||||
|
|
||||||
|
FileProcessingService file_processing;
|
||||||
|
|
||||||
|
//PublishController publish_controller(svr, authorization_service, file_processing);
|
||||||
|
|
||||||
|
PublishController publish_controller(/*svr,*/ authorization_service, file_processing);
|
||||||
|
|
||||||
|
std::cout << "Server is running at http://localhost:8080" << '\n';
|
||||||
|
|
||||||
|
svr.listen("0.0.0.0", 8080);
|
||||||
}
|
}
|
||||||
|
|||||||
9906
SpCloudMain/httplib.h
Normal file
9906
SpCloudMain/httplib.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user