Skip to content

Commit 98558a9

Browse files
authored
Merge pull request #7 from pooranjoyb/sristy17-patch-1
Feat:Create File
2 parents d9dbc06 + cc56e55 commit 98558a9

File tree

7 files changed

+83
-2
lines changed

7 files changed

+83
-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

docs/Storage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Works around with Storage Buckets and files in Appwrite
1515

1616
| Method Name | Usage | Link |
1717
|-------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------|
18+
| `createFile()` | Creates the file from the local in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/createFile.cpp) |
1819
| `getFile()` | Fetches the file from the bucket in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/getFile.cpp) |
1920
| `getFileView()` | Fetches the file from the bucket in the Appwrite project using the unique bucket ID. | [Example](/examples/storage/files/getFileView.cpp) |
2021
| `getFileDownload()` | Retrieves a file buffer for download from the buckets in the Appwrite project. | [Example](/examples/storage/files/getFileDownload.cpp) |

examples/storage/files/createFile.cpp

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

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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,38 @@ 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+
std::string boundary = "----AppwriteBoundary";
265+
std::ostringstream payload;
266+
267+
// preparing the multipart-formdata
268+
payload << "--" << boundary << "\r\n"
269+
<< "Content-Disposition: form-data; name=\"fileId\"\r\n\r\n"
270+
<< fileName << "\r\n";
271+
272+
payload << "--" << boundary << "\r\n"
273+
<< "Content-Disposition: form-data; name=\"file\"; filename=\"" << fileName << "\"\r\n"
274+
<< "Content-Type: text/plain\r\n\r\n"
275+
<< fileContent << "\r\n";
276+
277+
payload << "--" << boundary << "--\r\n";
278+
279+
std::vector<std::string> headers = Config::getHeaders(projectId);
280+
headers.push_back("X-Appwrite-Key: " + apiKey);
281+
headers.push_back("Content-Type: multipart/form-data; boundary=" + boundary);
282+
283+
std::string response;
284+
int statusCode = Utils::postRequest(url, payload.str(), headers, response);
285+
286+
if (statusCode == HttpStatus::OK || statusCode == HttpStatus::CREATED) {
287+
return response;
288+
} else {
289+
throw AppwriteException("Error creating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
290+
}
291+
}

tests/storage/files/createFile

1.34 MB
Binary file not shown.

0 commit comments

Comments
 (0)