@@ -22,6 +22,9 @@ using namespace winrt::Windows::Foundation;
22
22
using namespace winrt ::Windows::Security::Cryptography;
23
23
using namespace winrt ::Windows::Security::Cryptography::Core;
24
24
25
+
26
+ using namespace std ::chrono_literals;
27
+
25
28
CancellationDisposable::CancellationDisposable (IAsyncInfo const & async, std::function<void ()>&& onCancel) noexcept
26
29
: m_async{ async }
27
30
, m_onCancel{ std::move (onCancel) }
@@ -148,7 +151,7 @@ void RNFetchBlob::ConstantsViaConstantsProvider(winrt::Microsoft::ReactNative::R
148
151
constants.Add (L" MusicDir" , UserDataPaths::GetDefault ().Music ());
149
152
150
153
// RNFetchBlob.MovieDir
151
- constants.Add (L" MusicDir " , UserDataPaths::GetDefault ().Videos ());
154
+ constants.Add (L" MovieDir " , UserDataPaths::GetDefault ().Videos ());
152
155
153
156
// RNFetchBlob.DownloadDirectoryPath - IMPLEMENT for convenience? (absent in iOS and deprecated in Android)
154
157
constants.Add (L" DownloadDir" , UserDataPaths::GetDefault ().Downloads ());
@@ -1063,7 +1066,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1063
1066
std::wstring url,
1064
1067
winrt::Microsoft::ReactNative::JSValueObject headers,
1065
1068
std::string body,
1066
- std::function<void (std::string, std::string, std::string)> callback) noexcept
1069
+ std::function<void (std::string, std::string, std::string, std::string )> callback) noexcept
1067
1070
{
1068
1071
winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
1069
1072
@@ -1165,8 +1168,9 @@ winrt::fire_and_forget RNFetchBlob::fetchBlob(
1165
1168
co_return ;
1166
1169
}
1167
1170
1168
- winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1171
+
1169
1172
1173
+ winrt::Windows::Web::Http::HttpRequestMessage requestMessage{ httpMethod, Uri{url} };
1170
1174
bool pathToFile{ body.rfind (prefix, 0 ) == 0 };
1171
1175
winrt::hstring fileContent;
1172
1176
if (pathToFile)
@@ -1202,7 +1206,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
1202
1206
std::wstring url,
1203
1207
winrt::Microsoft::ReactNative::JSValueObject headers,
1204
1208
winrt::Microsoft::ReactNative::JSValueArray body,
1205
- std::function<void (std::string, std::string, std::string)> callback) noexcept
1209
+ std::function<void (std::string, std::string, std::string, std::string )> callback) noexcept
1206
1210
{
1207
1211
// createBlobForm(options, taskId, method, url, headers, "", body, callback);
1208
1212
// co_return;
@@ -1318,6 +1322,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
1318
1322
if (!requestMessage.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ())))
1319
1323
{
1320
1324
bool result = requestContent.Headers ().TryAppendWithoutValidation (winrt::to_hstring (entry.first ), winrt::to_hstring (entry.second .AsString ()));
1325
+ bool lol = true ;
1321
1326
}
1322
1327
}
1323
1328
@@ -1476,86 +1481,107 @@ winrt::Windows::Foundation::IAsyncAction RNFetchBlob::ProcessRequestAsync(
1476
1481
const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
1477
1482
winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
1478
1483
RNFetchBlobConfig& config,
1479
- std::function<void (std::string, std::string, std::string)> callback) noexcept
1484
+ std::function<void (std::string, std::string, std::string, std::string )> callback) noexcept
1480
1485
try
1481
1486
{
1482
1487
// TODO: implement timeouts
1483
1488
winrt::Windows::Web::Http::HttpClient httpClient{filter};
1484
- winrt::Windows::Web::Http::HttpResponseMessage response = co_await httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead);
1485
- IReference<uint64_t > contentLength{ response.Content ().Headers ().ContentLength () };
1489
+
1490
+ IAsyncOperationWithProgress async{ httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead) };
1491
+ if (async.wait_for (100s) == AsyncStatus::Completed) {
1492
+ winrt::Windows::Web::Http::HttpResponseMessage response{async.get ()};
1493
+ IReference<uint64_t > contentLength{ response.Content ().Headers ().ContentLength () };
1486
1494
1487
- if (config.fileCache )
1488
- {
1489
- if (config.path .empty ())
1495
+ if (config.fileCache )
1490
1496
{
1491
- config.path = winrt::to_string (ApplicationData::Current ().TemporaryFolder ().Path ()) + " \\ RNFetchBlobTmp_" + config.taskId ;
1492
- if (config.appendExt .length () > 0 )
1497
+ if (config.path .empty ())
1493
1498
{
1494
- config.path += " ." + config.appendExt ;
1499
+ config.path = winrt::to_string (ApplicationData::Current ().TemporaryFolder ().Path ()) + " \\ RNFetchBlobTmp_" + config.taskId ;
1500
+ if (config.appendExt .length () > 0 )
1501
+ {
1502
+ config.path += " ." + config.appendExt ;
1503
+ }
1495
1504
}
1496
- }
1497
1505
1498
- std::filesystem::path path{ config.path };
1499
- StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (ApplicationData::Current ().TemporaryFolder ().Path ()) };
1500
- StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists) };
1501
- IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
1502
- IOutputStream outputStream{ stream.GetOutputStreamAt (0 ) };
1506
+ std::filesystem::path path{ config.path };
1507
+ StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (ApplicationData::Current ().TemporaryFolder ().Path ()) };
1508
+ StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists) };
1509
+ IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
1510
+ IOutputStream outputStream{ stream.GetOutputStreamAt (0 ) };
1503
1511
1504
- auto contentStream{ co_await response.Content ().ReadAsInputStreamAsync () };
1505
- Buffer buffer{ 10 * 1024 };
1506
- uint64_t totalRead{ 0 };
1512
+ auto contentStream{ co_await response.Content ().ReadAsInputStreamAsync () };
1513
+ Buffer buffer{ 10 * 1024 };
1514
+ uint64_t totalRead{ 0 };
1507
1515
1508
- for (;;)
1509
- {
1510
- buffer.Length (0 );
1511
- auto readBuffer = co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
1512
- totalRead += readBuffer.Length ();
1513
- if (readBuffer.Length () == 0 )
1516
+ for (;;)
1514
1517
{
1515
- break ;
1518
+ buffer.Length (0 );
1519
+ auto readBuffer = co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
1520
+ totalRead += readBuffer.Length ();
1521
+
1522
+ auto it{ downloadProgressMap.find (taskId) };
1523
+ if (it != downloadProgressMap.end ()) {
1524
+ auto var{ downloadProgressMap[taskId] };
1525
+ std::string chunk{ winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer)) };
1526
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" RNFetchBlobProgress" ,
1527
+ Microsoft::ReactNative::JSValueObject{
1528
+ { " taskId" , taskId },
1529
+ { " written" , totalRead },
1530
+ { " total" , contentLength.Type () == PropertyType::UInt64 ?
1531
+ Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1532
+ Microsoft::ReactNative::JSValue{nullptr } },
1533
+ { " chunk" , chunk },
1534
+ });
1535
+ }
1536
+
1537
+ if (readBuffer.Length () == 0 )
1538
+ {
1539
+ break ;
1540
+ }
1541
+ co_await outputStream.WriteAsync (readBuffer);
1542
+
1543
+ // condition if taskId in a table where I need to report, then emit this
1544
+ // to do that, I need a map that with a mutex
1545
+ // mutex for all rn fetch blob main class
1546
+ // std::scoped_lock lock {m_mutex}
1547
+
1516
1548
}
1517
- co_await outputStream. WriteAsync (readBuffer );
1549
+ callback ( " " , " path " , config. path , " " );
1518
1550
1519
- // condition if taskId in a table where I need to report, then emit this
1520
- // to do that, I need a map that with a mutex
1521
- // mutex for all rn fetch blob main class
1522
- // std::scoped_lock lock {m_mutex}
1551
+ }
1552
+ else {
1553
+ std::string chunk{ winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, co_await response.Content ().ReadAsBufferAsync ())) };
1523
1554
auto it{ downloadProgressMap.find (taskId) };
1524
1555
if (it != downloadProgressMap.end ()) {
1556
+
1525
1557
auto var{ downloadProgressMap[taskId] };
1558
+
1526
1559
m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" RNFetchBlobProgress" ,
1527
1560
Microsoft::ReactNative::JSValueObject{
1528
- { " recieved" , totalRead },
1561
+ { " taskId" , taskId },
1562
+ { " written" , contentLength.Type () == PropertyType::UInt64 ?
1563
+ Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1564
+ Microsoft::ReactNative::JSValue{nullptr } },
1529
1565
{ " total" , contentLength.Type () == PropertyType::UInt64 ?
1530
- Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1531
- Microsoft::ReactNative::JSValue{nullptr } },
1566
+ Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1567
+ Microsoft::ReactNative::JSValue{nullptr } },
1568
+ { " chunk" , chunk },
1532
1569
});
1533
1570
}
1571
+ callback (chunk, " result" , config.path , " " );
1534
1572
}
1535
- callback (" " , " path" , config.path );
1536
-
1573
+
1537
1574
}
1538
- else {
1539
- auto it{ downloadProgressMap.find (taskId) };
1540
- if (it != downloadProgressMap.end ()) {
1541
1575
1542
- auto var{ downloadProgressMap[taskId] };
1543
1576
1544
- m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" RNFetchBlobProgress" ,
1545
- Microsoft::ReactNative::JSValueObject{
1546
- { " recieved" , 0 },
1547
- { " total" , 0 },
1548
- });
1549
- }
1550
- callback (winrt::to_string (co_await response.Content ().ReadAsStringAsync ()), " result" , config.path );
1551
- }
1552
- co_return ;
1577
+
1553
1578
}
1554
1579
catch (const hresult_error& ex)
1555
1580
{
1556
- callback (winrt::to_string (ex.message ().c_str ()), " error" , " " );
1581
+ callback (winrt::to_string (ex.message ().c_str ()), " error" , " " , " " );
1557
1582
}
1558
1583
1584
+
1559
1585
RNFetchBlobStream::RNFetchBlobStream (Streams::IRandomAccessStream& _streamInstance, EncodingOptions _encoding) noexcept
1560
1586
: streamInstance{ std::move (_streamInstance) }
1561
1587
, encoding{ _encoding }
0 commit comments