@@ -62,7 +62,7 @@ void RNFetchBlob::ConstantsViaConstantsProvider(winrt::Microsoft::ReactNative::R
62
62
// createFile
63
63
winrt::fire_and_forget RNFetchBlob::createFile (
64
64
std::string path,
65
- std::string content,
65
+ std::wstring content,
66
66
std::string encoding,
67
67
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
68
68
try
75
75
76
76
bool isUTF8{ encoding.compare (" utf8" ) == 0 };
77
77
78
- winrt::hstring contentToInsert{ winrt::to_hstring ( content) };
78
+ // winrt::hstring contentToInsert{ content };
79
79
Streams::IBuffer buffer{ isUTF8 ?
80
- CryptographicBuffer::ConvertStringToBinary (contentToInsert , BinaryStringEncoding::Utf8) :
81
- CryptographicBuffer::DecodeFromBase64String (contentToInsert ) };
80
+ CryptographicBuffer::ConvertStringToBinary (content , BinaryStringEncoding::Utf8) :
81
+ CryptographicBuffer::DecodeFromBase64String (content ) };
82
82
83
83
winrt::hstring directoryPath, fileName;
84
84
splitPath (path, directoryPath, fileName);
93
93
catch (...)
94
94
{
95
95
promise.Reject (" EEXIST: File already exists." ); // TODO: Include filepath
96
- // promise.Reject()
97
96
co_return ;
98
97
}
99
98
promise.Resolve (path);
@@ -161,20 +160,102 @@ catch (const hresult_error& ex)
161
160
winrt::fire_and_forget RNFetchBlob::writeFile (
162
161
std::string path,
163
162
std::string encoding,
163
+ std::wstring data,
164
164
bool append,
165
165
winrt::Microsoft::ReactNative::ReactPromise<int > promise) noexcept
166
+ try
166
167
{
167
- co_return ;
168
+ Streams::IBuffer buffer;
169
+ if (encoding.compare (" utf8" ) == 0 )
170
+ {
171
+ buffer = Cryptography::CryptographicBuffer::ConvertStringToBinary (data, BinaryStringEncoding::Utf8);
172
+ }
173
+ else if (encoding.compare (" base64" ) == 0 )
174
+ {
175
+ buffer = Cryptography::CryptographicBuffer::DecodeFromBase64String (data);
176
+ }
177
+ else if (encoding.compare (" uri" ) == 0 )
178
+ {
179
+ winrt::hstring srcDirectoryPath, srcFileName;
180
+ splitPath (path, srcDirectoryPath, srcFileName);
181
+ StorageFolder srcFolder{ co_await StorageFolder::GetFolderFromPathAsync (srcDirectoryPath) };
182
+ StorageFile srcFile{ co_await StorageFile::GetFileFromPathAsync (srcDirectoryPath) };
183
+ buffer = co_await FileIO::ReadBufferAsync (srcFile);
184
+ }
185
+ else
186
+ {
187
+ promise.Reject (" Invalid encoding" );
188
+ }
189
+
190
+ winrt::hstring destDirectoryPath, destFileName;
191
+ splitPath (path, destDirectoryPath, destFileName);
192
+ StorageFolder destFolder{ co_await StorageFolder::GetFolderFromPathAsync (destDirectoryPath) };
193
+ StorageFile destFile{ nullptr };
194
+ if (append)
195
+ {
196
+ destFile = co_await destFolder.CreateFileAsync (destFileName, CreationCollisionOption::OpenIfExists);
197
+ }
198
+ else
199
+ {
200
+ destFile = co_await destFolder.CreateFileAsync (destFileName, CreationCollisionOption::ReplaceExisting);
201
+ }
202
+ Streams::IRandomAccessStream stream{ co_await destFile.OpenAsync (FileAccessMode::ReadWrite) };
203
+
204
+ if (append)
205
+ {
206
+ stream.Seek (UINT64_MAX);
207
+ }
208
+ co_await stream.WriteAsync (buffer);
209
+ promise.Resolve (buffer.Length ());
210
+ }
211
+ catch (...)
212
+ {
213
+ promise.Reject (" Failed to write" );
168
214
}
169
215
170
216
winrt::fire_and_forget RNFetchBlob::writeFileArray (
171
217
std::string path,
172
218
winrt::Microsoft::ReactNative::JSValueArray dataArray,
173
219
bool append,
174
- winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
220
+ winrt::Microsoft::ReactNative::ReactPromise<int > promise) noexcept
221
+ try
175
222
{
223
+ std::vector<byte> data;
224
+ data.reserve (dataArray.size ());
225
+ for (auto & var : dataArray)
226
+ {
227
+ data.push_back (var.AsInt8 ());
228
+ }
229
+
230
+ Streams::IBuffer buffer{ CryptographicBuffer::CreateFromByteArray (data) };
231
+
232
+ winrt::hstring destDirectoryPath, destFileName;
233
+ splitPath (path, destDirectoryPath, destFileName);
234
+ StorageFolder destFolder{ co_await StorageFolder::GetFolderFromPathAsync (destDirectoryPath) };
235
+ StorageFile destFile{ nullptr };
236
+ if (append)
237
+ {
238
+ destFile = co_await destFolder.CreateFileAsync (destFileName, CreationCollisionOption::OpenIfExists);
239
+ }
240
+ else
241
+ {
242
+ destFile = co_await destFolder.CreateFileAsync (destFileName, CreationCollisionOption::ReplaceExisting);
243
+ }
244
+ Streams::IRandomAccessStream stream{ co_await destFile.OpenAsync (FileAccessMode::ReadWrite) };
245
+
246
+ if (append)
247
+ {
248
+ stream.Seek (UINT64_MAX);
249
+ }
250
+ co_await stream.WriteAsync (buffer);
251
+ promise.Resolve (buffer.Length ());
252
+
176
253
co_return ;
177
254
}
255
+ catch (...)
256
+ {
257
+ promise.Reject (" Failed to write" );
258
+ }
178
259
179
260
180
261
// writeStream
0 commit comments