From cdb87e09a1f7a7a663f5a78310971b7c089059a6 Mon Sep 17 00:00:00 2001 From: Hepatica Date: Sun, 11 Aug 2024 06:44:36 +0200 Subject: [PATCH] Fix unzip --- SpCloudMain/Controllers/PublishController.cpp | 22 ++---- SpCloudMain/Service/FileProcessingService.cpp | 68 ++----------------- SpCloudMain/SpCloudMain.cpp | 9 ++- 3 files changed, 18 insertions(+), 81 deletions(-) diff --git a/SpCloudMain/Controllers/PublishController.cpp b/SpCloudMain/Controllers/PublishController.cpp index 7029449..3f73056 100644 --- a/SpCloudMain/Controllers/PublishController.cpp +++ b/SpCloudMain/Controllers/PublishController.cpp @@ -26,20 +26,9 @@ public: PublishController(httplib::Server& svr, AuthorizationService authorization, std::shared_ptr 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) { if (this->authorization.is_user_authorized()) @@ -49,15 +38,13 @@ private: 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 + std::string random_string = generate_random_string(20);//TODO VERY IMPORTANT CHANGE THIS RANDOM GENERATING TO GENERATE UNIQUE STRING - //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"); } @@ -79,6 +66,7 @@ private: } } +private: void dotnet_publish(const std::string& path) { std::string dll_file_name = file_processing->find_file_by_suffix(path, "dll"); diff --git a/SpCloudMain/Service/FileProcessingService.cpp b/SpCloudMain/Service/FileProcessingService.cpp index 6f9c08b..678ae06 100644 --- a/SpCloudMain/Service/FileProcessingService.cpp +++ b/SpCloudMain/Service/FileProcessingService.cpp @@ -7,9 +7,6 @@ #include #include "CommandService.cpp" #include "Logger.cpp" -//#include -//#include -//#include class FileProcessingService { @@ -24,8 +21,6 @@ public: logger_.log(INFO, "Start saving file method"); - //std::lock_guard lock(file_mutex); // Блокируем мьютекс//Todo TEST STABILITY OF THIS - std::ofstream ofs(filename, std::ios::binary); logger_.log(INFO, "Create file stream"); @@ -39,62 +34,6 @@ public: 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) { std::filesystem::create_directories(path); } @@ -108,7 +47,12 @@ public: //Linux version //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); diff --git a/SpCloudMain/SpCloudMain.cpp b/SpCloudMain/SpCloudMain.cpp index 8461a76..4632a2a 100644 --- a/SpCloudMain/SpCloudMain.cpp +++ b/SpCloudMain/SpCloudMain.cpp @@ -35,13 +35,18 @@ int main() AuthorizationService authorization_service; - //FileProcessingService file_processing(logger); - auto file_processing = std::make_shared(logger); PublishController publish_controller(svr, authorization_service, file_processing, logger); 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); }