|
1 | 1 | #include "pch.h"
|
2 | 2 | #include "RNFetchBlob.h"
|
3 | 3 |
|
| 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 | + |
4 | 11 | #include <filesystem>
|
5 | 12 |
|
| 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 | + |
6 | 22 | // createFile
|
7 | 23 | winrt::fire_and_forget RNFetchBlob::createFile(
|
8 | 24 | std::string path,
|
9 | 25 | std::string content,
|
10 |
| - std::string encode, |
| 26 | + std::string encoding, |
11 | 27 | 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 | + } |
14 | 71 | }
|
15 | 72 |
|
16 | 73 | winrt::fire_and_forget RNFetchBlob::createFileASCII(
|
@@ -175,3 +232,12 @@ void RNFetchBlob::splitPath(const std::string& fullPath, winrt::hstring& directo
|
175 | 232 | directoryPath = path.has_parent_path() ? winrt::to_hstring(path.parent_path().c_str()) : L"";
|
176 | 233 | fileName = path.has_filename() ? winrt::to_hstring(path.filename().c_str()) : L"";
|
177 | 234 | }
|
| 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 | +} |
0 commit comments