Skip to content

Commit fe57335

Browse files
committed
Feat:Create File
1 parent d9dbc06 commit fe57335

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ deleteFile: $(SRCS) $(EXAMPLES_DIR)/storage/files/deleteFile.cpp
189189
$(CXX) $(CXXFLAGS) -o tests/storage/files/deleteFile $(SRCS) $(EXAMPLES_DIR)/storage/files/deleteFile.cpp $(LDFLAGS)
190190
getFileDownload: $(SRCS) $(EXAMPLES_DIR)/storage/files/getFileDownload.cpp
191191
$(CXX) $(CXXFLAGS) -o tests/storage/files/getFileDownload $(SRCS) $(EXAMPLES_DIR)/storage/files/getFileDownload.cpp $(LDFLAGS)
192+
createFile: $(SRCS) $(EXAMPLES_DIR)/storage/files/createFile.cpp
193+
$(CXX) $(CXXFLAGS) -o tests/storage/files/createFile $(SRCS) $(EXAMPLES_DIR)/storage/files/createFile.cpp $(LDFLAGS)
194+
192195

193196
# Health
194197
getHealth: $(SRCS) $(EXAMPLES_DIR)/health/getHealth.cpp

examples/storage/files/createFile.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
int main() {
6+
std::string projectId = "66fbb5a100070a3a1d19";
7+
std::string apiKey = "";
8+
std::string bucketId = "bucket12322";
9+
std::string fileName = "example.txt";
10+
std::string filePath = "examples/storage/files/example.txt";
11+
12+
Appwrite appwrite(projectId);
13+
Storage& storage = appwrite.getStorage();
14+
15+
storage.setup(apiKey, projectId);
16+
17+
try {
18+
std::ifstream file(filePath, std::ios::binary);
19+
if (!file.is_open()) {
20+
std::cerr << "Failed to open file: " << filePath << std::endl;
21+
return 1;
22+
}
23+
24+
std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
25+
26+
std::vector<std::string> mimeTypes = {"text/plain"};
27+
std::string response = storage.createFile(bucketId, fileName, fileContent, mimeTypes);
28+
std::cout << "File created successfully! \n\nResponse: " << response << std::endl;
29+
} catch (const AppwriteException& ex) {
30+
std::cerr << "Exception: " << ex.what() << std::endl;
31+
}
32+
33+
return 0;
34+
}
35+

examples/storage/files/example.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example

include/classes/Storage.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Storage
2727
std::string updateFile(const std::string &bucketId, const std::string &fileId, const std::string &name = "", const std::vector<std::string> &permissions = {});
2828
std::string deleteFile(const std::string &bucketId, const std::string &fileId);
2929
std::string getFileDownload(const std::string &bucketId, const std::string &fileId);
30-
30+
std::string createFile(const std::string &bucketId, const std::string &fileName, const std::string &fileContent, const std::vector<std::string> &permissions);
31+
3132
private:
3233
std::string apiKey;
3334
std::string projectId;

src/services/Storage.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,29 @@ std::string Storage::updateFile(const std::string &bucketId, const std::string &
254254
else {
255255
throw AppwriteException("Error updating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
256256
}
257-
}
257+
}
258+
259+
std::string Storage::createFile(const std::string &bucketId, const std::string &fileName, const std::string &fileContent, const std::vector<std::string> &permissions) {
260+
Validator::validateStorageParams(bucketId, fileName);
261+
262+
std::string url = Config::API_BASE_URL + "/storage/buckets/" + bucketId + "/files";
263+
264+
json payloadJson = {
265+
{"name", fileName},
266+
{"permissions", permissions}
267+
};
268+
269+
std::string payload = payloadJson.dump();
270+
271+
std::vector<std::string> headers = Config::getHeaders(projectId);
272+
headers.push_back("X-Appwrite-Key: " + apiKey);
273+
headers.push_back("Content-Type: multipart/form-data");
274+
std::string response;
275+
int statusCode = Utils::postRequest(url, payload, headers, response);
276+
277+
if (statusCode == HttpStatus::OK) {
278+
return response;
279+
} else {
280+
throw AppwriteException("Error creating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
281+
}
282+
}

tests/storage/files/createFile

1.34 MB
Binary file not shown.

0 commit comments

Comments
 (0)