diff --git a/Makefile b/Makefile index b016591..f95ed8b 100644 --- a/Makefile +++ b/Makefile @@ -189,6 +189,9 @@ deleteFile: $(SRCS) $(EXAMPLES_DIR)/storage/files/deleteFile.cpp $(CXX) $(CXXFLAGS) -o tests/storage/files/deleteFile $(SRCS) $(EXAMPLES_DIR)/storage/files/deleteFile.cpp $(LDFLAGS) getFileDownload: $(SRCS) $(EXAMPLES_DIR)/storage/files/getFileDownload.cpp $(CXX) $(CXXFLAGS) -o tests/storage/files/getFileDownload $(SRCS) $(EXAMPLES_DIR)/storage/files/getFileDownload.cpp $(LDFLAGS) +createFile: $(SRCS) $(EXAMPLES_DIR)/storage/files/createFile.cpp + $(CXX) $(CXXFLAGS) -o tests/storage/files/createFile $(SRCS) $(EXAMPLES_DIR)/storage/files/createFile.cpp $(LDFLAGS) + # Health getHealth: $(SRCS) $(EXAMPLES_DIR)/health/getHealth.cpp diff --git a/examples/storage/files/createFile.cpp b/examples/storage/files/createFile.cpp new file mode 100644 index 0000000..29731b0 --- /dev/null +++ b/examples/storage/files/createFile.cpp @@ -0,0 +1,35 @@ +#include "Appwrite.hpp" +#include +#include + +int main() { + std::string projectId = "66fbb5a100070a3a1d19"; + std::string apiKey = ""; + std::string bucketId = "bucket12322"; + std::string fileName = "example.txt"; + std::string filePath = "examples/storage/files/example.txt"; + + Appwrite appwrite(projectId); + Storage& storage = appwrite.getStorage(); + + storage.setup(apiKey, projectId); + + try { + std::ifstream file(filePath, std::ios::binary); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << filePath << std::endl; + return 1; + } + + std::string fileContent((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + + std::vector mimeTypes = {"text/plain"}; + std::string response = storage.createFile(bucketId, fileName, fileContent, mimeTypes); + std::cout << "File created successfully! \n\nResponse: " << response << std::endl; + } catch (const AppwriteException& ex) { + std::cerr << "Exception: " << ex.what() << std::endl; + } + + return 0; +} + diff --git a/examples/storage/files/example.txt b/examples/storage/files/example.txt new file mode 100644 index 0000000..96236f8 --- /dev/null +++ b/examples/storage/files/example.txt @@ -0,0 +1 @@ +example \ No newline at end of file diff --git a/include/classes/Storage.hpp b/include/classes/Storage.hpp index 37c0625..8e7ea19 100644 --- a/include/classes/Storage.hpp +++ b/include/classes/Storage.hpp @@ -27,7 +27,8 @@ class Storage std::string updateFile(const std::string &bucketId, const std::string &fileId, const std::string &name = "", const std::vector &permissions = {}); std::string deleteFile(const std::string &bucketId, const std::string &fileId); std::string getFileDownload(const std::string &bucketId, const std::string &fileId); - + std::string createFile(const std::string &bucketId, const std::string &fileName, const std::string &fileContent, const std::vector &permissions); + private: std::string apiKey; std::string projectId; diff --git a/src/services/Storage.cpp b/src/services/Storage.cpp index 4631e00..e5a1a16 100644 --- a/src/services/Storage.cpp +++ b/src/services/Storage.cpp @@ -254,4 +254,29 @@ std::string Storage::updateFile(const std::string &bucketId, const std::string & else { throw AppwriteException("Error updating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response); } -} \ No newline at end of file +} + +std::string Storage::createFile(const std::string &bucketId, const std::string &fileName, const std::string &fileContent, const std::vector &permissions) { + Validator::validateStorageParams(bucketId, fileName); + + std::string url = Config::API_BASE_URL + "/storage/buckets/" + bucketId + "/files"; + + json payloadJson = { + {"name", fileName}, + {"permissions", permissions} + }; + + std::string payload = payloadJson.dump(); + + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + headers.push_back("Content-Type: multipart/form-data"); + std::string response; + int statusCode = Utils::postRequest(url, payload, headers, response); + + if (statusCode == HttpStatus::OK) { + return response; + } else { + throw AppwriteException("Error creating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response); + } +} diff --git a/tests/storage/files/createFile b/tests/storage/files/createFile new file mode 100755 index 0000000..bd1e9a2 Binary files /dev/null and b/tests/storage/files/createFile differ