Skip to content

Commit 7897dbc

Browse files
committed
Implement writeFile and writeFileArray
1 parent 3e8a457 commit 7897dbc

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

+88-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void RNFetchBlob::ConstantsViaConstantsProvider(winrt::Microsoft::ReactNative::R
6262
// createFile
6363
winrt::fire_and_forget RNFetchBlob::createFile(
6464
std::string path,
65-
std::string content,
65+
std::wstring content,
6666
std::string encoding,
6767
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
6868
try
@@ -75,10 +75,10 @@ try
7575

7676
bool isUTF8{ encoding.compare("utf8") == 0 };
7777

78-
winrt::hstring contentToInsert{ winrt::to_hstring(content) };
78+
//winrt::hstring contentToInsert{ content };
7979
Streams::IBuffer buffer{ isUTF8 ?
80-
CryptographicBuffer::ConvertStringToBinary(contentToInsert, BinaryStringEncoding::Utf8) :
81-
CryptographicBuffer::DecodeFromBase64String(contentToInsert) };
80+
CryptographicBuffer::ConvertStringToBinary(content, BinaryStringEncoding::Utf8) :
81+
CryptographicBuffer::DecodeFromBase64String(content) };
8282

8383
winrt::hstring directoryPath, fileName;
8484
splitPath(path, directoryPath, fileName);
@@ -93,7 +93,6 @@ try
9393
catch (...)
9494
{
9595
promise.Reject("EEXIST: File already exists."); // TODO: Include filepath
96-
//promise.Reject()
9796
co_return;
9897
}
9998
promise.Resolve(path);
@@ -161,20 +160,102 @@ catch (const hresult_error& ex)
161160
winrt::fire_and_forget RNFetchBlob::writeFile(
162161
std::string path,
163162
std::string encoding,
163+
std::wstring data,
164164
bool append,
165165
winrt::Microsoft::ReactNative::ReactPromise<int> promise) noexcept
166+
try
166167
{
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");
168214
}
169215

170216
winrt::fire_and_forget RNFetchBlob::writeFileArray(
171217
std::string path,
172218
winrt::Microsoft::ReactNative::JSValueArray dataArray,
173219
bool append,
174-
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
220+
winrt::Microsoft::ReactNative::ReactPromise<int> promise) noexcept
221+
try
175222
{
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+
176253
co_return;
177254
}
255+
catch (...)
256+
{
257+
promise.Reject("Failed to write");
258+
}
178259

179260

180261
// writeStream

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct RNFetchBlob
2121
REACT_METHOD(createFile);
2222
winrt::fire_and_forget createFile(
2323
std::string path,
24-
std::string content,
24+
std::wstring content,
2525
std::string encoding,
2626
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept;
2727

@@ -37,6 +37,7 @@ struct RNFetchBlob
3737
winrt::fire_and_forget writeFile(
3838
std::string path,
3939
std::string encoding,
40+
std::wstring data,
4041
bool append,
4142
winrt::Microsoft::ReactNative::ReactPromise<int> promise) noexcept;
4243

@@ -45,7 +46,7 @@ struct RNFetchBlob
4546
std::string path,
4647
winrt::Microsoft::ReactNative::JSValueArray dataArray,
4748
bool append,
48-
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept;
49+
winrt::Microsoft::ReactNative::ReactPromise<int> promise) noexcept;
4950

5051

5152
// writeStream

0 commit comments

Comments
 (0)