diff --git a/examples/storage/updateBucket.cpp b/examples/storage/updateBucket.cpp index 62ed068..73d8af3 100644 --- a/examples/storage/updateBucket.cpp +++ b/examples/storage/updateBucket.cpp @@ -4,15 +4,15 @@ int main() { std::string projectId = "66fbb5a100070a3a1d19"; std::string apiKey = ""; - std::string bucketId = "bucket12322"; - std::string name = "testBucketupdated"; - + Appwrite appwrite(projectId, apiKey); + std::string bucketId = "bucketnew"; + std::string name = "PEWPEWPEW"; std::vector permissions = {"read(\"any\")", "write(\"any\")"}; bool fileSecurity = false; bool enabled = true; - int maximumFileSize = 30000000; + int maximumFileSize = 20000000; std::vector allowedFileExtensions = {"jpg", "png"}; std::string compression = "gzip"; bool antivirus = true; diff --git a/include/Utils.hpp b/include/Utils.hpp index 82fea65..3e1a1fc 100644 --- a/include/Utils.hpp +++ b/include/Utils.hpp @@ -12,6 +12,7 @@ class Utils { static int getRequest(const std::string& url, const std::vector& headers, std::string& response); static int deleteRequest(const std::string &url, const std::vector &headers, std::string &response); static std::string urlEncode(const std::string& value); + static std::string escapeJsonString(const std::string &input); }; #endif diff --git a/src/Utils.cpp b/src/Utils.cpp index b8245db..fe84918 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -226,18 +226,66 @@ namespace Utils return static_cast(httpCode); } - std::string urlEncode(const std::string& value) { + std::string urlEncode(const std::string &value) + { std::ostringstream escaped; escaped << std::hex << std::setfill('0'); - - for (char c : value) { - if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + + for (char c : value) + { + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') + { escaped << c; - } else { + } + else + { escaped << '%' << std::setw(2) << (static_cast(c) & 0xFF); } } - + return escaped.str(); } + + std::string escapeJsonString(const std::string &input) + { + std::ostringstream ss; + for (auto c : input) + { + switch (c) + { + case '\"': + ss << "\\\""; + break; + case '\\': + ss << "\\\\"; + break; + case '\b': + ss << "\\b"; + break; + case '\f': + ss << "\\f"; + break; + case '\n': + ss << "\\n"; + break; + case '\r': + ss << "\\r"; + break; + case '\t': + ss << "\\t"; + break; + default: + if ('\x00' <= c && c <= '\x1f') + { + ss << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << static_cast(c); + } + else + { + ss << c; + } + } + } + return ss.str(); + } } diff --git a/src/services/Storage.cpp b/src/services/Storage.cpp index e11e1bf..36f490b 100644 --- a/src/services/Storage.cpp +++ b/src/services/Storage.cpp @@ -60,19 +60,37 @@ std::string Storage::updateBucket(const std::string& bucketId, const std::string std::string url = Config::API_BASE_URL + "/storage/buckets/" + bucketId; - json payloadJson = { - {"name", name}, - {"permissions", permissions}, - {"fileSecurity", fileSecurity}, - {"enabled", enabled}, - {"maximumFileSize", maximumFileSize}, - {"allowedFileExtensions", allowedFileExtensions}, - {"compression", compression}, - {"encryption", encryption}, - {"antivirus", antivirus} - }; + auto boolToString = [](bool value) { + return value ? "true" : "false"; + }; + + std::string permissionsStr = "["; + for (const auto &perm : permissions) { + permissionsStr += "\"" + Utils::escapeJsonString(perm) + "\","; + } + + if (!permissions.empty()) permissionsStr.pop_back(); + permissionsStr += "]"; + + std::string extensionsStr = "["; + for (const auto &ext : allowedFileExtensions) { + extensionsStr += "\"" + Utils::escapeJsonString(ext) + "\","; + } + + if (!allowedFileExtensions.empty()) extensionsStr.pop_back(); + extensionsStr += "]"; + + std::string payload = R"({"name":")" + + Utils::escapeJsonString(name) + R"(",)" + + R"("permissions":)" + permissionsStr + "," + + R"("fileSecurity":)" + boolToString(fileSecurity) + "," + + R"("enabled":)" + boolToString(enabled) + "," + + R"("maximumFileSize":)" + std::to_string(maximumFileSize) + "," + + R"("allowedFileExtensions":)" + extensionsStr + "," + + R"("compression":")" + Utils::escapeJsonString(compression) + R"(",)" + + R"("encryption":)" + boolToString(encryption) + "," + + R"("antivirus":)" + boolToString(antivirus) + "}"; - std::string payload = payloadJson.dump(); std::vector headers = Config::getHeaders(projectId); headers.push_back("X-Appwrite-Key: " + apiKey); diff --git a/tests/storage/createBucket b/tests/storage/createBucket index cc2abe4..d64f44e 100755 Binary files a/tests/storage/createBucket and b/tests/storage/createBucket differ diff --git a/tests/storage/updateBucket b/tests/storage/updateBucket index 8b68e82..b49be3b 100755 Binary files a/tests/storage/updateBucket and b/tests/storage/updateBucket differ