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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/Storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
41 changes: 41 additions & 0 deletions examples/storage/files/createFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "Appwrite.hpp"
#include <iostream>
#include <fstream>
#include <vector>

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<std::string> 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;
}
1 change: 1 addition & 0 deletions examples/storage/files/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example
3 changes: 2 additions & 1 deletion include/classes/Storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Storage
std::string updateFile(const std::string &bucketId, const std::string &fileId, const std::string &name = "", const std::vector<std::string> &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<std::string> &permissions);

private:
std::string apiKey;
std::string projectId;
Expand Down
36 changes: 35 additions & 1 deletion src/services/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

std::string Storage::createFile(const std::string &bucketId, const std::string &fileName, const std::string &fileContent, const std::vector<std::string> &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<std::string> 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);
}
}
Binary file added tests/storage/files/createFile
Binary file not shown.