7
7
#include < winrt/Windows.Storage.Streams.h>
8
8
#include < winrt/Windows.Storage.h>
9
9
10
+ #include < winrt/Windows.Web.Http.h>
11
+ #include < winrt/Windows.Web.Http.Headers.h>
12
+
10
13
#include < filesystem>
11
14
12
15
using namespace winrt ;
@@ -58,7 +61,6 @@ winrt::fire_and_forget RNFetchBlob::createFile(
58
61
winrt::Microsoft::ReactNative::ReactPromise<std::string> promise) noexcept
59
62
try
60
63
{
61
- bool isUTF8{ encoding.compare (" utf8" ) == 0 };
62
64
bool shouldExit{ false };
63
65
Streams::IBuffer buffer;
64
66
if (encoding.compare (" uri" ) == 0 )
@@ -448,11 +450,13 @@ try
448
450
// TODO: Investigate returning wstrings as parameters
449
451
winrt::hstring base64Content{ Cryptography::CryptographicBuffer::EncodeToBase64String (readBuffer) };
450
452
// m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", [&streamId, &base64Content](React::IJSValueWriter const& argWriter) {
453
+ // argWriter.WriteArrayBegin();
451
454
// WriteValue(argWriter, streamId);
452
455
// argWriter.WriteObjectBegin();
453
- // React::WriteProperty(argWriter, "event", L "data");
456
+ // React::WriteProperty(argWriter, "event", "data");
454
457
// React::WriteProperty(argWriter, "detail", base64Content);
455
458
// argWriter.WriteObjectEnd();
459
+ // argWriter.WriteArrayEnd();
456
460
// });
457
461
m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
458
462
winrt::Microsoft::ReactNative::JSValueObject{
@@ -825,7 +829,7 @@ winrt::fire_and_forget RNFetchBlob::lstat(
825
829
826
830
callback (" " , resultsArray);
827
831
}
828
- catch (const hresult_error& ex )
832
+ catch (... )
829
833
{
830
834
// "Failed to read directory."
831
835
winrt::Microsoft::ReactNative::JSValueArray emptyArray;
@@ -948,35 +952,110 @@ catch (...)
948
952
}
949
953
950
954
951
- void RNFetchBlob::fetchBlob (
955
+ winrt::fire_and_forget RNFetchBlob::fetchBlob (
952
956
winrt::Microsoft::ReactNative::JSValueObject options,
953
957
std::string taskId,
954
958
std::string method,
955
- std::string url,
959
+ std::wstring url,
956
960
winrt::Microsoft::ReactNative::JSValueObject headers,
957
961
std::string body,
958
962
std::function<void (std::string, std::string, std::string)> callback) noexcept
959
963
{
964
+ // winrt::Windows::Web::Http::HttpMethod::Hea
965
+ // Delete, Patch, Post, Put, Get, Options, Head
966
+ // Method
967
+ RNFetchBlobConfig config;
968
+ config.appendExt = options[" appendExt" ].AsString ();
969
+ config.fileCache = options[" fileCache" ].AsBoolean ();
970
+ config.followRedirect = options[" followRedirect" ].IsNull () ? true : options[" followRedirect" ].AsBoolean ();
971
+ config.overwrite = options[" overwrite" ].AsBoolean ();
972
+ config.path = options[" path" ].AsString ();
973
+ config.timeout = options[" timeout" ].AsInt32 ();
974
+ config.trusty = options[" trusty" ].AsBoolean ();;
960
975
976
+
977
+ winrt::Windows::Web::Http::HttpMethod httpMethod{ winrt::Windows::Web::Http::HttpMethod::Post () };
978
+ std::string methodUpperCase{ method };
979
+ for (auto & c : methodUpperCase)
980
+ {
981
+ toupper (c);
982
+ }
983
+
984
+ if (methodUpperCase.compare (" DELETE" ) == 0 )
985
+ {
986
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Delete ();
987
+ }
988
+ else if (methodUpperCase.compare (" PATCH" ) == 0 )
989
+ {
990
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Patch ();
991
+ }
992
+ else if (methodUpperCase.compare (" PUT" ) == 0 )
993
+ {
994
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Put ();
995
+ }
996
+ else if (methodUpperCase.compare (" GET" ) == 0 )
997
+ {
998
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Get ();
999
+ }
1000
+ else if (methodUpperCase.compare (" OPTIONS" ) == 0 )
1001
+ {
1002
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Options ();
1003
+ }
1004
+ else if (methodUpperCase.compare (" HEAD" ) == 0 )
1005
+ {
1006
+ httpMethod = winrt::Windows::Web::Http::HttpMethod::Head ();
1007
+ }
1008
+ else if (methodUpperCase.compare (" POST" ) != 0 )
1009
+ {
1010
+ // Method not supported by winrt
1011
+ co_return ;
1012
+ }
1013
+
1014
+ winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1015
+ winrt::Windows::Web::Http::HttpMultipartFormDataContent requestContent{ L" -----" };
1016
+
1017
+ // co_await requestMessage.Content().;
1018
+
1019
+ for (auto const & entry : headers)
1020
+ {
1021
+ if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1022
+ {
1023
+ requestContent.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1024
+ }
1025
+ }
1026
+
1027
+ std::string temp{ body };
1028
+ if (body.length () > 0 ) {
1029
+ winrt::Windows::Web::Http::HttpBufferContent content{ CryptographicBuffer::ConvertStringToBinary (winrt::to_hstring (body), BinaryStringEncoding::Utf8) };
1030
+ requestMessage.Content (content);
1031
+ }
1032
+
1033
+
1034
+ winrt::Windows::Web::Http::HttpResponseMessage response = co_await m_httpClient.SendRequestAsync (requestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead);
1035
+
1036
+ co_return ;
961
1037
}
962
1038
963
1039
void RNFetchBlob::fetchBlobForm (
964
1040
winrt::Microsoft::ReactNative::JSValueObject options,
965
1041
std::string taskId,
966
1042
std::string method,
967
- std::string url,
1043
+ std::wstring url,
968
1044
winrt::Microsoft::ReactNative::JSValueObject headers,
969
1045
winrt::Microsoft::ReactNative::JSValueArray body,
970
1046
std::function<void (std::string)> callback) noexcept
971
1047
{
972
-
1048
+ return ;
973
1049
}
974
1050
1051
+
1052
+
975
1053
void RNFetchBlob::enableProgressReport (
976
1054
std::string taskId,
977
1055
int interval,
978
1056
int count) noexcept
979
1057
{
1058
+ return ;
980
1059
}
981
1060
982
1061
// enableUploadProgressReport
@@ -985,20 +1064,22 @@ void RNFetchBlob::enableUploadProgressReport(
985
1064
int interval,
986
1065
int count) noexcept
987
1066
{
1067
+ return ;
988
1068
}
989
1069
990
1070
// cancelRequest
991
1071
void RNFetchBlob::cancelRequest (
992
1072
std::string taskId,
993
1073
std::function<void (std::string)> callback) noexcept
994
1074
{
1075
+ return ;
995
1076
}
996
1077
997
1078
void RNFetchBlob::removeSession (
998
1079
winrt::Microsoft::ReactNative::JSValueObject paths,
999
1080
std::function<void (std::string)> callback) noexcept
1000
1081
{
1001
-
1082
+ return ;
1002
1083
}
1003
1084
1004
1085
void RNFetchBlob::closeStream (
@@ -1009,10 +1090,11 @@ try
1009
1090
auto stream{ m_streamMap.find (streamId)->second };
1010
1091
stream.streamInstance .Close ();
1011
1092
m_streamMap.extract (streamId);
1093
+ callback (" " );
1012
1094
}
1013
- catch (... )
1095
+ catch (const hresult_error& ex )
1014
1096
{
1015
-
1097
+ callback ( winrt::to_string (ex. message ()). c_str ());
1016
1098
}
1017
1099
1018
1100
0 commit comments