diff --git a/services/backup/src/Tools.cpp b/services/backup/src/Tools.cpp index 44188c3f1..33938ed57 100644 --- a/services/backup/src/Tools.cpp +++ b/services/backup/src/Tools.cpp @@ -1,35 +1,57 @@ #include "Tools.h" +#include "GlobalConstants.h" #include "GlobalTools.h" #include #include #include +#include +#include namespace comm { namespace network { namespace tools { std::string generateRandomString(std::size_t length) { const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; thread_local std::random_device generator; std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1); std::string random_string; for (std::size_t i = 0; i < length; ++i) { random_string += CHARACTERS[distribution(generator)]; } return random_string; } std::string generateHolder( const std::string &blobHash, const std::string &backupID, const std::string &resourceID) { return backupID + ID_SEPARATOR + resourceID + ID_SEPARATOR + blobHash + ID_SEPARATOR + tools::generateUUID(); } +std::string validateAttachmentHolders(const std::string &holders) { + std::stringstream stream(holders); + std::string item; + std::string result; + + while (std::getline(stream, item, ATTACHMENT_DELIMITER)) { + if (item.empty()) { + throw std::runtime_error("empty holder detected"); + } + result += item; + result += ATTACHMENT_DELIMITER; + } + if (result.empty()) { + throw std::runtime_error("parse attachment holders failed"); + } + + return result; +} + } // namespace tools } // namespace network } // namespace comm diff --git a/services/backup/src/Tools.h b/services/backup/src/Tools.h index 47fc5584b..619d66398 100644 --- a/services/backup/src/Tools.h +++ b/services/backup/src/Tools.h @@ -1,18 +1,20 @@ #pragma once #include namespace comm { namespace network { namespace tools { std::string generateRandomString(std::size_t length = 20); std::string generateHolder( const std::string &blobHash, const std::string &backupID, const std::string &resourceID = ""); +std::string validateAttachmentHolders(const std::string &holders); + } // namespace tools } // namespace network } // namespace comm diff --git a/services/lib/src/GlobalConstants.h b/services/lib/src/GlobalConstants.h index 7dbd9394c..fa26224db 100644 --- a/services/lib/src/GlobalConstants.h +++ b/services/lib/src/GlobalConstants.h @@ -1,28 +1,28 @@ #pragma once #include namespace comm { namespace network { // 4MB limit // WARNING: use keeping in mind that grpc adds its own headers to messages // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md // so the message that actually is being sent over the network looks like this // [Compressed-Flag] [Message-Length] [Message] // [Compressed-Flag] 1 byte - added by grpc // [Message-Length] 4 bytes - added by grpc // [Message] N bytes - actual data // so for every message we get 5 additional bytes of data // as mentioned here // https://github.com/grpc/grpc/issues/15734#issuecomment-396962671 // grpc stream may contain more than one message const size_t GRPC_CHUNK_SIZE_LIMIT = 4 * 1024 * 1024; const size_t GRPC_METADATA_SIZE_PER_MESSAGE = 5; const std::string AWS_REGION = "us-east-2"; -const std::string ATTACHMENT_DELIMITER = ";"; +const char ATTACHMENT_DELIMITER = ';'; } // namespace network } // namespace comm