Skip to content

Commit 578c840

Browse files
committed
fix: createFile method
proper preparation of the multipart-formdataa
1 parent fe57335 commit 578c840

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
#include "Appwrite.hpp"
22
#include <iostream>
33
#include <fstream>
4+
#include <vector>
45

56
int main() {
6-
std::string projectId = "66fbb5a100070a3a1d19";
7-
std::string apiKey = "";
8-
std::string bucketId = "bucket12322";
9-
std::string fileName = "example.txt";
7+
std::string projectId = "66fbb5a100070a3a1d19";
8+
std::string apiKey = "";
9+
std::string bucketId = "bucket12322";
10+
std::string fileName = "example.txt";
1011
std::string filePath = "examples/storage/files/example.txt";
1112

1213
Appwrite appwrite(projectId);
1314
Storage& storage = appwrite.getStorage();
14-
1515
storage.setup(apiKey, projectId);
1616

1717
try {
18-
std::ifstream file(filePath, std::ios::binary);
18+
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
1919
if (!file.is_open()) {
2020
std::cerr << "Failed to open file: " << filePath << std::endl;
2121
return 1;
2222
}
2323

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;
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;
2936
} catch (const AppwriteException& ex) {
3037
std::cerr << "Exception: " << ex.what() << std::endl;
3138
}
3239

3340
return 0;
3441
}
35-

src/services/Storage.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,29 @@ std::string Storage::createFile(const std::string &bucketId, const std::string &
261261

262262
std::string url = Config::API_BASE_URL + "/storage/buckets/" + bucketId + "/files";
263263

264-
json payloadJson = {
265-
{"name", fileName},
266-
{"permissions", permissions}
267-
};
268-
269-
std::string payload = payloadJson.dump();
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";
270278

271279
std::vector<std::string> headers = Config::getHeaders(projectId);
272280
headers.push_back("X-Appwrite-Key: " + apiKey);
273-
headers.push_back("Content-Type: multipart/form-data");
281+
headers.push_back("Content-Type: multipart/form-data; boundary=" + boundary);
282+
274283
std::string response;
275-
int statusCode = Utils::postRequest(url, payload, headers, response);
284+
int statusCode = Utils::postRequest(url, payload.str(), headers, response);
276285

277-
if (statusCode == HttpStatus::OK) {
286+
if (statusCode == HttpStatus::OK || statusCode == HttpStatus::CREATED) {
278287
return response;
279288
} else {
280289
throw AppwriteException("Error creating file. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);

tests/storage/files/createFile

-60.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)