Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/storage/updateBucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> permissions = {"read(\"any\")", "write(\"any\")"};
bool fileSecurity = false;
bool enabled = true;
int maximumFileSize = 30000000;
int maximumFileSize = 20000000;
std::vector<std::string> allowedFileExtensions = {"jpg", "png"};
std::string compression = "gzip";
bool antivirus = true;
Expand Down
1 change: 1 addition & 0 deletions include/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Utils {
static int getRequest(const std::string& url, const std::vector<std::string>& headers, std::string& response);
static int deleteRequest(const std::string &url, const std::vector<std::string> &headers, std::string &response);
static std::string urlEncode(const std::string& value);
static std::string escapeJsonString(const std::string &input);
};

#endif
60 changes: 54 additions & 6 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,66 @@ namespace Utils
return static_cast<int>(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<int>(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<int>(c);
}
else
{
ss << c;
}
}
}
return ss.str();
}
}
42 changes: 30 additions & 12 deletions src/services/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> headers = Config::getHeaders(projectId);
headers.push_back("X-Appwrite-Key: " + apiKey);

Expand Down
Binary file modified tests/storage/createBucket
Binary file not shown.
Binary file modified tests/storage/updateBucket
Binary file not shown.