From 4a150a9fcb62352e866d15eed62805d373b30e7e Mon Sep 17 00:00:00 2001 From: Ivan Chvets Date: Thu, 26 Sep 2024 16:07:19 -0400 Subject: [PATCH 1/2] fix: file upload status https://telecominfraproject.atlassian.net/browse/WIFI-14134 Summary of changes: - Return 202 in case of file pening upload. Signed-off-by: Ivan Chvets --- src/RESTAPI/RESTAPI_file.cpp | 9 ++++++++- src/StorageService.h | 2 +- src/framework/RESTAPI_Handler.h | 5 +++++ src/storage/storage_command.cpp | 30 +++++++++++++++--------------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/RESTAPI/RESTAPI_file.cpp b/src/RESTAPI/RESTAPI_file.cpp index 08247c27..684a99aa 100644 --- a/src/RESTAPI/RESTAPI_file.cpp +++ b/src/RESTAPI/RESTAPI_file.cpp @@ -22,9 +22,16 @@ namespace OpenWifi { std::string FileType; std::string FileContent; - if (!StorageService()->GetAttachedFileContent(UUID, SerialNumber, FileContent, FileType) || FileContent.empty()) { + int WaitingForFile = 0; + if (!StorageService()->GetAttachedFileContent(UUID, SerialNumber, FileContent, FileType, WaitingForFile) && !WaitingForFile) { return NotFound(); } + else if (WaitingForFile) + { + // waiting for file to be uploaded, return Accepted + return Accepted(); + } + if (FileType == "pcap") { SendFileContent(FileContent, "application/vnd.tcpdump.pcap", UUID + ".pcap"); } diff --git a/src/StorageService.h b/src/StorageService.h index 18b977c0..10a3025f 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -243,7 +243,7 @@ namespace OpenWifi { const std::string &Type); bool CancelWaitFile(std::string &UUID, std::string &ErrorText); bool GetAttachedFileContent(std::string &UUID, const std::string &SerialNumber, - std::string &FileContent, std::string &Type); + std::string &FileContent, std::string &Type, int& WaitingForFile); bool RemoveAttachedFile(std::string &UUID); bool SetCommandResult(std::string &UUID, std::string &Result); bool GetNewestCommands(std::string &SerialNumber, uint64_t HowMany, diff --git a/src/framework/RESTAPI_Handler.h b/src/framework/RESTAPI_Handler.h index 3cd9aa07..1815993d 100644 --- a/src/framework/RESTAPI_Handler.h +++ b/src/framework/RESTAPI_Handler.h @@ -431,6 +431,11 @@ namespace OpenWifi { } } + inline void Accepted() { + PrepareResponse(Poco::Net::HTTPResponse::HTTP_ACCEPTED); + Response->send(); + } + inline void SendCompressedTarFile(const std::string &FileName, const std::string &Content) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); diff --git a/src/storage/storage_command.cpp b/src/storage/storage_command.cpp index d021f16d..3f8d497c 100644 --- a/src/storage/storage_command.cpp +++ b/src/storage/storage_command.cpp @@ -647,18 +647,6 @@ namespace OpenWifi { Sess.begin(); Poco::Data::Statement Statement(Sess); - std::string StatementStr; - - // Get the existing command - - StatementStr = - "UPDATE CommandList SET WaitingForFile=?, AttachDate=?, AttachSize=? WHERE UUID=?"; - - Statement << ConvertParams(StatementStr), Poco::Data::Keywords::use(WaitForFile), - Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Size), - Poco::Data::Keywords::use(UUID); - Statement.execute(); - Sess.commit(); if (Size < FileUploader()->MaxSize()) { Poco::Data::BLOB TheBlob; @@ -681,6 +669,18 @@ namespace OpenWifi { poco_warning(Logger(), fmt::format("File {} is too large.", UUID)); } Sess.commit(); + + // update CommandList here to ensure that file us uploaded + std::string StatementStr; + StatementStr = + "UPDATE CommandList SET WaitingForFile=?, AttachDate=?, AttachSize=? WHERE UUID=?"; + + Statement << ConvertParams(StatementStr), Poco::Data::Keywords::use(WaitForFile), + Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Size), + Poco::Data::Keywords::use(UUID); + Statement.execute(); + Sess.commit(); + return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -689,7 +689,7 @@ namespace OpenWifi { } bool Storage::GetAttachedFileContent(std::string &UUID, const std::string &SerialNumber, - std::string &FileContent, std::string &Type) { + std::string &FileContent, std::string &Type, int &WaitingForFile) { try { Poco::Data::BLOB L; /* @@ -702,10 +702,10 @@ namespace OpenWifi { Poco::Data::Statement Select1(Sess); std::string TmpSerialNumber; - std::string st1{"SELECT SerialNumber, Command FROM CommandList WHERE UUID=?"}; + std::string st1{"SELECT SerialNumber, Command , WaitingForFile FROM CommandList WHERE UUID=?"}; std::string Command; Select1 << ConvertParams(st1), Poco::Data::Keywords::into(TmpSerialNumber), - Poco::Data::Keywords::into(Command), Poco::Data::Keywords::use(UUID); + Poco::Data::Keywords::into(Command), Poco::Data::Keywords::into(WaitingForFile), Poco::Data::Keywords::use(UUID); Select1.execute(); if (TmpSerialNumber != SerialNumber) { From 4f00d77d2b9cd79b289316f2787c2f26c5077829 Mon Sep 17 00:00:00 2001 From: Ivan Chvets Date: Fri, 27 Sep 2024 10:34:45 -0400 Subject: [PATCH 2/2] fix: file upload status - code style change https://telecominfraproject.atlassian.net/browse/WIFI-14134 Summary of changes: - Code style change. Signed-off-by: Ivan Chvets --- src/RESTAPI/RESTAPI_file.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RESTAPI/RESTAPI_file.cpp b/src/RESTAPI/RESTAPI_file.cpp index 684a99aa..1285baaa 100644 --- a/src/RESTAPI/RESTAPI_file.cpp +++ b/src/RESTAPI/RESTAPI_file.cpp @@ -26,8 +26,7 @@ namespace OpenWifi { if (!StorageService()->GetAttachedFileContent(UUID, SerialNumber, FileContent, FileType, WaitingForFile) && !WaitingForFile) { return NotFound(); } - else if (WaitingForFile) - { + else if (WaitingForFile) { // waiting for file to be uploaded, return Accepted return Accepted(); }