Skip to content

Commit

Permalink
Feat:Create File
Browse files Browse the repository at this point in the history
  • Loading branch information
sristy17 committed Dec 17, 2024
1 parent d9dbc06 commit fe57335
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
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
35 changes: 35 additions & 0 deletions examples/storage/files/createFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "Appwrite.hpp"
#include <iostream>
#include <fstream>

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<char>(file)), std::istreambuf_iterator<char>());

std::vector<std::string> 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;
}

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
27 changes: 26 additions & 1 deletion src/services/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

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";

json payloadJson = {
{"name", fileName},
{"permissions", permissions}
};

std::string payload = payloadJson.dump();

std::vector<std::string> 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);
}
}
Binary file added tests/storage/files/createFile
Binary file not shown.

0 comments on commit fe57335

Please sign in to comment.