mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-10 04:29:35 +02:00
Fix unzip
This commit is contained in:
@@ -26,20 +26,9 @@ public:
|
|||||||
PublishController(httplib::Server& svr, AuthorizationService authorization, std::shared_ptr<FileProcessingService> file_processing, Logger& logger)
|
PublishController(httplib::Server& svr, AuthorizationService authorization, std::shared_ptr<FileProcessingService> file_processing, Logger& logger)
|
||||||
: authorization(authorization), file_processing(file_processing), logger_(logger)
|
: authorization(authorization), file_processing(file_processing), logger_(logger)
|
||||||
{
|
{
|
||||||
/*this->authorization = authorization;
|
|
||||||
|
|
||||||
this->file_processing = file_processing;*/
|
|
||||||
|
|
||||||
svr.Post("/publish", [this](const httplib::Request& req, httplib::Response& res)
|
|
||||||
{
|
|
||||||
logger_.log(INFO, "Start publish");
|
|
||||||
|
|
||||||
this->process_publish(req, res);
|
|
||||||
//httplib::Headers test = req.headers;//Todo add processing header for authorization layer
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
public:
|
||||||
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())
|
||||||
@@ -49,15 +38,13 @@ private:
|
|||||||
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_with_retry(filename, content)) {
|
|
||||||
if (file_processing->save_file(filename, content)) {
|
if (file_processing->save_file(filename, content)) {
|
||||||
|
|
||||||
//Todo uncommit later
|
std::string random_string = generate_random_string(20);//TODO VERY IMPORTANT CHANGE THIS RANDOM GENERATING TO GENERATE UNIQUE STRING
|
||||||
//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");
|
||||||
}
|
}
|
||||||
@@ -79,6 +66,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
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");
|
||||||
|
|||||||
@@ -7,9 +7,6 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include "CommandService.cpp"
|
#include "CommandService.cpp"
|
||||||
#include "Logger.cpp"
|
#include "Logger.cpp"
|
||||||
//#include <Poco/Mutex.h>
|
|
||||||
//#include <Poco/Path.h>
|
|
||||||
//#include <Poco/FileStr*/eam.h >
|
|
||||||
class FileProcessingService
|
class FileProcessingService
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -24,8 +21,6 @@ public:
|
|||||||
|
|
||||||
logger_.log(INFO, "Start saving file method");
|
logger_.log(INFO, "Start saving file method");
|
||||||
|
|
||||||
//std::lock_guard<std::mutex> lock(file_mutex); // Блокируем мьютекс//Todo TEST STABILITY OF THIS
|
|
||||||
|
|
||||||
std::ofstream ofs(filename, std::ios::binary);
|
std::ofstream ofs(filename, std::ios::binary);
|
||||||
|
|
||||||
logger_.log(INFO, "Create file stream");
|
logger_.log(INFO, "Create file stream");
|
||||||
@@ -39,62 +34,6 @@ public:
|
|||||||
return ofs.good();
|
return ofs.good();
|
||||||
}
|
}
|
||||||
|
|
||||||
//bool save_file(const std::string& filename, const std::string& content) {
|
|
||||||
// Mutex::ScopedLock lock(file_mutex); // Блокируем мьютекс
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// Poco::File file(filename);
|
|
||||||
// Poco::FileOutputStream ofs(filename, std::ios::binary | std::ios::trunc);
|
|
||||||
|
|
||||||
// if (!ofs.good()) {
|
|
||||||
// std::cerr << "Error opening file: " << filename << std::endl;
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ofs.write(content.c_str(), content.size());
|
|
||||||
|
|
||||||
// if (!ofs.good()) {
|
|
||||||
// std::cerr << "Error writing to file: " << filename << std::endl;
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ofs.close(); // Явно закрываем файл
|
|
||||||
// }
|
|
||||||
// catch (const Poco::Exception& ex) {
|
|
||||||
// std::cerr << "Poco exception: " << ex.displayText() << std::endl;
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return true;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
//bool save_file_with_timeout(const std::string& filename, const std::string& content, std::chrono::milliseconds timeout) {
|
|
||||||
// // Запускаем асинхронную задачу для записи файла
|
|
||||||
// auto future = std::async(std::launch::async, &FileProcessingService::save_file, this, filename, content);
|
|
||||||
|
|
||||||
// // Ожидаем завершения задачи или истечения тайм-аута
|
|
||||||
// if (future.wait_for(timeout) == std::future_status::ready) {
|
|
||||||
// return future.get(); // Возвращаем результат записи
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// std::cerr << "Timeout occurred while saving file: " << filename << std::endl;
|
|
||||||
// return false; // Тайм-аут
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
//bool save_file_with_retry(const std::string& filename, const std::string& content, int max_retries = 5, std::chrono::milliseconds timeout = std::chrono::milliseconds(1000)) {
|
|
||||||
// for (int i = 0; i < max_retries; ++i) {
|
|
||||||
// if (save_file_with_timeout(filename, content, timeout)) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Пауза перед повторной попыткой
|
|
||||||
// }
|
|
||||||
// std::cerr << "Failed to save file after " << max_retries << " attempts: " << filename << std::endl;
|
|
||||||
// return false;
|
|
||||||
//}
|
|
||||||
|
|
||||||
void create_directory(const std::string& path) {
|
void create_directory(const std::string& path) {
|
||||||
std::filesystem::create_directories(path);
|
std::filesystem::create_directories(path);
|
||||||
}
|
}
|
||||||
@@ -108,7 +47,12 @@ public:
|
|||||||
|
|
||||||
//Linux version
|
//Linux version
|
||||||
//std::string command = "unzip " + file_path + " -d " + final_files_directory;
|
//std::string command = "unzip " + file_path + " -d " + final_files_directory;
|
||||||
std::string command = "unrar x" + file_path + " " + final_files_directory;
|
|
||||||
|
|
||||||
|
|
||||||
|
std::string command = "unrar x " + file_path + " " + final_files_directory;
|
||||||
|
|
||||||
|
logger_.log(INFO, "Start unzip command" + command);
|
||||||
|
|
||||||
std::thread commandThread(&CommandService::execute_command, command);
|
std::thread commandThread(&CommandService::execute_command, command);
|
||||||
|
|
||||||
|
|||||||
@@ -35,13 +35,18 @@ int main()
|
|||||||
|
|
||||||
AuthorizationService authorization_service;
|
AuthorizationService authorization_service;
|
||||||
|
|
||||||
//FileProcessingService file_processing(logger);
|
|
||||||
|
|
||||||
auto file_processing = std::make_shared<FileProcessingService>(logger);
|
auto file_processing = std::make_shared<FileProcessingService>(logger);
|
||||||
|
|
||||||
PublishController publish_controller(svr, authorization_service, file_processing, logger);
|
PublishController publish_controller(svr, authorization_service, file_processing, logger);
|
||||||
|
|
||||||
std::cout << "Server is running at http://localhost:8080" << '\n';
|
std::cout << "Server is running at http://localhost:8080" << '\n';
|
||||||
|
|
||||||
|
svr.Post("/publish", [&](const httplib::Request& req, httplib::Response& res)
|
||||||
|
{
|
||||||
|
logger.log(INFO, "Start publish from main");
|
||||||
|
|
||||||
|
publish_controller.process_publish(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
svr.listen("0.0.0.0", 8080);
|
svr.listen("0.0.0.0", 8080);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user