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/docs/Storage.md b/docs/Storage.md index e16eba8..21aa98a 100644 --- a/docs/Storage.md +++ b/docs/Storage.md @@ -15,6 +15,7 @@ Works around with Storage Buckets and files in Appwrite | Method Name | Usage | Link | |-------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------| +| `createFile()` | Creates the file from the local in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/createFile.cpp) | | `getFile()` | Fetches the file from the bucket in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/getFile.cpp) | | `getFileView()` | Fetches the file from the bucket in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/getFileView.cpp) | | `getFileDownload()` | Retrieves a file buffer for download from the buckets in the Appwrite project. | [Example](/examples/storage/files/getFileDownload.cpp) | diff --git a/examples/storage/files/createFile.cpp b/examples/storage/files/createFile.cpp new file mode 100644 index 0000000..2ce021c --- /dev/null +++ b/examples/storage/files/createFile.cpp @@ -0,0 +1,41 @@ +#include "Appwrite.hpp" +#include +#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 | std::ios::ate); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << filePath << std::endl; + return 1; + } + + auto fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + std::string fileContent(fileSize, '\0'); + if (!file.read(&fileContent[0], fileSize)) { + std::cerr << "Failed to read file content." << std::endl; + return 1; + } + + std::vector permissions = {"role:all"}; + std::string response = storage.createFile(bucketId, fileName, fileContent, permissions); + + 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..fd22f0b 100644 --- a/src/services/Storage.cpp +++ b/src/services/Storage.cpp @@ -254,4 +254,38 @@ 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"; + + std::string boundary = "----AppwriteBoundary"; + std::ostringstream payload; + + // preparing the multipart-formdata + payload << "--" << boundary << "\r\n" + << "Content-Disposition: form-data; name=\"fileId\"\r\n\r\n" + << fileName << "\r\n"; + + payload << "--" << boundary << "\r\n" + << "Content-Disposition: form-data; name=\"file\"; filename=\"" << fileName << "\"\r\n" + << "Content-Type: text/plain\r\n\r\n" + << fileContent << "\r\n"; + + payload << "--" << boundary << "--\r\n"; + + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + headers.push_back("Content-Type: multipart/form-data; boundary=" + boundary); + + std::string response; + int statusCode = Utils::postRequest(url, payload.str(), headers, response); + + if (statusCode == HttpStatus::OK || statusCode == HttpStatus::CREATED) { + 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..967f798 Binary files /dev/null and b/tests/storage/files/createFile differ