Skip to content

Commit 342fda0

Browse files
committed
Implement initial createFile
1 parent b36939e commit 342fda0

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,73 @@
11
#include "pch.h"
22
#include "RNFetchBlob.h"
33

4+
#include <windows.h>
5+
#include <winrt/Windows.Security.Cryptography.h>
6+
#include <winrt/Windows.Security.Cryptography.Core.h>
7+
#include <winrt/Windows.Storage.FileProperties.h>
8+
#include <winrt/Windows.Storage.Streams.h>
9+
#include <winrt/Windows.Storage.h>
10+
411
#include <filesystem>
512

13+
using namespace winrt;
14+
using namespace winrt::Windows::ApplicationModel;
15+
using namespace winrt::Windows::Storage;
16+
using namespace winrt::Windows::Storage::Streams;
17+
using namespace winrt::Windows::Foundation;
18+
using namespace winrt::Windows::Security::Cryptography;
19+
using namespace winrt::Windows::Security::Cryptography::Core;
20+
21+
622
// createFile
723
winrt::fire_and_forget RNFetchBlob::createFile(
824
std::string path,
925
std::string content,
10-
std::string encode,
26+
std::string encoding,
1127
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
12-
{
13-
co_return;
28+
try
29+
{
30+
//if (encoding.compare("uri") == 0)
31+
//{
32+
// //get contents from path
33+
// co_return;
34+
//}
35+
36+
bool isUTF8{ encoding.compare("utf8") == 0 };
37+
38+
winrt::hstring contentToInsert{ winrt::to_hstring(content) };
39+
Streams::IBuffer buffer{ isUTF8 ?
40+
CryptographicBuffer::ConvertStringToBinary(contentToInsert, BinaryStringEncoding::Utf8) :
41+
CryptographicBuffer::DecodeFromBase64String(contentToInsert) };
42+
43+
winrt::hstring directoryPath, fileName;
44+
splitPath(path, directoryPath, fileName);
45+
46+
StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync(directoryPath) };
47+
try
48+
{
49+
StorageFile file{ co_await folder.CreateFileAsync(fileName, CreationCollisionOption::FailIfExists) };
50+
Streams::IRandomAccessStream stream{ co_await file.OpenAsync(FileAccessMode::ReadWrite) };
51+
co_await stream.WriteAsync(buffer);
52+
}
53+
catch (...)
54+
{
55+
promise.Reject("EEXIST: File already exists."); // TODO: Include filepath
56+
co_return;
57+
}
58+
promise.Resolve(path);
59+
}
60+
catch (const hresult_error& ex)
61+
{
62+
hresult result{ ex.code() };
63+
if (result == 0x80070002) // FileNotFoundException
64+
{
65+
promise.Reject("ENOENT: File does not exist and could not be created"); // TODO: Include filepath
66+
}
67+
else
68+
{
69+
promise.Reject("EUNSPECIFIED: Failed to write to file.");
70+
}
1471
}
1572

1673
winrt::fire_and_forget RNFetchBlob::createFileASCII(
@@ -175,3 +232,12 @@ void RNFetchBlob::splitPath(const std::string& fullPath, winrt::hstring& directo
175232
directoryPath = path.has_parent_path() ? winrt::to_hstring(path.parent_path().c_str()) : L"";
176233
fileName = path.has_filename() ? winrt::to_hstring(path.filename().c_str()) : L"";
177234
}
235+
236+
void RNFetchBlob::splitPath(const winrt::hstring& fullPath, winrt::hstring& directoryPath, winrt::hstring& folderName) noexcept
237+
{
238+
std::filesystem::path path(winrt::to_string(fullPath));
239+
path.make_preferred();
240+
241+
directoryPath = path.has_parent_path() ? winrt::to_hstring(path.parent_path().c_str()) : L"";
242+
folderName = path.has_filename() ? winrt::to_hstring(path.filename().c_str()) : L"";
243+
}

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct RNFetchBlob
1111
winrt::fire_and_forget createFile(
1212
std::string path,
1313
std::string content,
14-
std::string encode,
14+
std::string encoding,
1515
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept;
1616

1717
REACT_METHOD(createFileASCII);
@@ -145,4 +145,8 @@ struct RNFetchBlob
145145
winrt::hstring& directoryPath,
146146
winrt::hstring& fileName) noexcept;
147147

148+
void splitPath(const winrt::hstring& fullPath,
149+
winrt::hstring& directoryPath,
150+
winrt::hstring& folderName) noexcept;
151+
148152
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#pragma once
2+

0 commit comments

Comments
 (0)