@@ -645,7 +645,7 @@ catch (const hresult_error& ex)
645
645
}
646
646
647
647
648
- // mkdir - Implemented, not tested
648
+ // mkdir
649
649
void RNFetchBlob::mkdir (
650
650
std::string path,
651
651
winrt::Microsoft::ReactNative::ReactPromise<bool > promise) noexcept
@@ -721,7 +721,7 @@ catch (const hresult_error& ex)
721
721
}
722
722
723
723
724
- // hash - Implemented, not tested
724
+ // hash
725
725
winrt::fire_and_forget RNFetchBlob::hash (
726
726
std::string path,
727
727
std::string algorithm,
@@ -775,7 +775,7 @@ catch (const hresult_error& ex)
775
775
}
776
776
777
777
778
- // ls - Implemented, not tested
778
+ // ls
779
779
winrt::fire_and_forget RNFetchBlob::ls (
780
780
std::string path,
781
781
winrt::Microsoft::ReactNative::ReactPromise<std::vector<std::string>> promise) noexcept
@@ -808,7 +808,7 @@ catch (const hresult_error& ex)
808
808
}
809
809
810
810
811
- // mv - Implemented, not tested
811
+ // mv
812
812
winrt::fire_and_forget RNFetchBlob::mv (
813
813
std::string src, // from
814
814
std::string dest, // to
@@ -844,7 +844,7 @@ winrt::fire_and_forget RNFetchBlob::mv(
844
844
}
845
845
846
846
847
- // cp - Implemented, not tested
847
+ // cp
848
848
winrt::fire_and_forget RNFetchBlob::cp (
849
849
std::string src, // from
850
850
std::string dest, // to
@@ -876,7 +876,7 @@ catch (const hresult_error& ex)
876
876
}
877
877
878
878
879
- // exists - Implemented, not tested
879
+ // exists
880
880
void RNFetchBlob::exists (
881
881
std::string path,
882
882
std::function<void (bool , bool )> callback) noexcept
@@ -889,7 +889,7 @@ void RNFetchBlob::exists(
889
889
}
890
890
891
891
892
- // unlink - Implemented, not tested
892
+ // unlink
893
893
winrt::fire_and_forget RNFetchBlob::unlink (
894
894
std::string path,
895
895
std::function<void (std::string, bool )> callback) noexcept
@@ -918,7 +918,7 @@ catch (const hresult_error& ex)
918
918
}
919
919
920
920
921
- // lstat - Implemented, not tested
921
+ // lstat
922
922
winrt::fire_and_forget RNFetchBlob::lstat (
923
923
std::string path,
924
924
std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueArray&)> callback) noexcept
@@ -956,7 +956,7 @@ catch (...)
956
956
}
957
957
958
958
959
- // stat - Implemented, not tested
959
+ // stat
960
960
winrt::fire_and_forget RNFetchBlob::stat (
961
961
std::string path,
962
962
std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept
@@ -1018,7 +1018,7 @@ catch (const hresult_error& ex)
1018
1018
}
1019
1019
1020
1020
1021
- // df - Implemented, not tested
1021
+ // df
1022
1022
winrt::fire_and_forget RNFetchBlob::df (
1023
1023
std::function<void (std::string, winrt::Microsoft::ReactNative::JSValueObject&)> callback) noexcept
1024
1024
try
@@ -1410,6 +1410,7 @@ winrt::fire_and_forget RNFetchBlob::fetchBlobForm(
1410
1410
}
1411
1411
}
1412
1412
1413
+ // TODO, set a timeout for cancellation
1413
1414
co_await m_tasks.Add (taskId, ProcessRequestAsync (taskId, filter, requestMessage, config, callback));
1414
1415
m_tasks.Cancel (taskId);
1415
1416
{
@@ -1515,111 +1516,107 @@ try
1515
1516
{
1516
1517
winrt::Windows::Web::Http::HttpClient httpClient{filter};
1517
1518
1518
- IAsyncOperationWithProgress async{ httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead) };
1519
- if (async.wait_for (config.timeout ) == AsyncStatus::Completed) {
1520
- winrt::Windows::Web::Http::HttpResponseMessage response{async.get ()};
1521
- IReference<uint64_t > contentLength{ response.Content ().Headers ().ContentLength () };
1519
+ winrt::Windows::Web::Http::HttpResponseMessage response{ co_await httpClient.SendRequestAsync (httpRequestMessage, winrt::Windows::Web::Http::HttpCompletionOption::ResponseHeadersRead) };
1520
+ IReference<uint64_t > contentLength{ response.Content ().Headers ().ContentLength () };
1522
1521
1523
- IOutputStream outputStream;
1522
+ IOutputStream outputStream;
1524
1523
1525
- if (config.fileCache )
1524
+ if (config.fileCache )
1525
+ {
1526
+ if (config.path .empty ())
1526
1527
{
1527
- if (config.path .empty ())
1528
+ config.path = winrt::to_string (ApplicationData::Current ().TemporaryFolder ().Path ()) + " \\ RNFetchBlobTmp_" + taskId;
1529
+ if (config.appendExt .length () > 0 )
1528
1530
{
1529
- config.path = winrt::to_string (ApplicationData::Current ().TemporaryFolder ().Path ()) + " \\ RNFetchBlobTmp_" + taskId;
1530
- if (config.appendExt .length () > 0 )
1531
- {
1532
- config.path += " ." + config.appendExt ;
1533
- }
1531
+ config.path += " ." + config.appendExt ;
1534
1532
}
1535
-
1536
- std::filesystem::path path{ config.path };
1537
- StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (ApplicationData::Current ().TemporaryFolder ().Path ()) };
1538
- StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists) };
1539
- IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
1540
- outputStream = stream.GetOutputStreamAt (0 ) ;
1541
1533
}
1542
1534
1543
- auto contentStream{ co_await response.Content ().ReadAsInputStreamAsync () };
1544
- Buffer buffer{ 10 * 1024 };
1545
- uint64_t read { 0 };
1546
- uint64_t totalRead{ 0 };
1535
+ std::filesystem::path path{ config.path };
1536
+ StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (ApplicationData::Current ().TemporaryFolder ().Path ()) };
1537
+ StorageFile storageFile{ co_await storageFolder.CreateFileAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists) };
1538
+ IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
1539
+ outputStream = stream.GetOutputStreamAt (0 ) ;
1540
+ }
1547
1541
1548
- RNFetchBlobProgressConfig progressInfo;
1549
- uint64_t progressInterval{ 0 };
1542
+ auto contentStream{ co_await response.Content ().ReadAsInputStreamAsync () };
1543
+ Buffer buffer{ 10 * 1024 };
1544
+ uint64_t read { 0 };
1545
+ uint64_t totalRead{ 0 };
1550
1546
1551
- std::stringstream chunkStream ;
1552
- std::stringstream resultOutput ;
1547
+ RNFetchBlobProgressConfig progressInfo ;
1548
+ uint64_t progressInterval{ 0 } ;
1553
1549
1554
- std::string readContents{" " };
1550
+ std::stringstream chunkStream;
1551
+ std::stringstream resultOutput;
1555
1552
1556
- auto exists{ downloadProgressMap.find (taskId) };
1557
- if (exists != downloadProgressMap.end () && contentLength.Type () == PropertyType::UInt64 ) {
1558
- progressInfo = downloadProgressMap[taskId];
1553
+ std::string readContents{" " };
1559
1554
1560
- if (progressInfo.count > -1 ) {
1561
- progressInterval = contentLength.Value () / 100 * progressInfo.count ;
1562
- }
1555
+ auto exists{ downloadProgressMap.find (taskId) };
1556
+ if (exists != downloadProgressMap.end () && contentLength.Type () == PropertyType::UInt64 ) {
1557
+ progressInfo = downloadProgressMap[taskId];
1558
+
1559
+ if (progressInfo.count > -1 ) {
1560
+ progressInterval = contentLength.Value () / 100 * progressInfo.count ;
1563
1561
}
1562
+ }
1564
1563
1565
- int64_t initialProgressTime{ winrt::clock::now ().time_since_epoch ().count () / 10000 };
1566
- int64_t currentProgressTime;
1564
+ int64_t initialProgressTime{ winrt::clock::now ().time_since_epoch ().count () / 10000 };
1565
+ int64_t currentProgressTime;
1567
1566
1568
- for (;;)
1569
- {
1570
- buffer.Length (0 );
1571
- auto readBuffer{ co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None) };
1572
- // readBuffer.
1573
- readContents = winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer));
1574
- read += readBuffer.Length ();
1575
- totalRead += read ;
1576
-
1577
- if (readBuffer.Length () == 0 )
1578
- {
1579
- break ;
1580
- }
1581
-
1582
- if (config.fileCache ) {
1583
- co_await outputStream.WriteAsync (readBuffer);
1584
- }
1585
- else {
1586
- resultOutput << readContents;
1587
- }
1567
+ for (;;)
1568
+ {
1569
+ buffer.Length (0 );
1570
+ auto readBuffer{ co_await contentStream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None) };
1588
1571
1589
- if (progressInfo.count > -1 || progressInfo.interval > -1 ) {
1590
- chunkStream << readContents;
1572
+ //
1573
+ read += readBuffer.Length ();
1574
+ totalRead += read ;
1591
1575
1592
- currentProgressTime = winrt::clock::now ().time_since_epoch ().count () / 10000 ;
1593
- if ((currentProgressTime - initialProgressTime >= progressInfo.interval && progressInfo.interval > -1 ) ||
1594
- (totalRead >= progressInterval && progressInfo.count > -1 )) {
1595
- m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" RNFetchBlobProgress" ,
1596
- Microsoft::ReactNative::JSValueObject{
1597
- { " taskId" , taskId },
1598
- { " written" , int64_t (totalRead) },
1599
- { " total" , contentLength.Type () == PropertyType::UInt64 ?
1600
- Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1601
- Microsoft::ReactNative::JSValue{nullptr } },
1602
- { " chunk" , chunkStream.str () },
1603
- });
1604
- chunkStream.clear ();
1605
- initialProgressTime = winrt::clock::now ().time_since_epoch ().count () / 10000 ;
1606
- if (progressInfo.count > -1 ) {
1607
- read = 0 ;
1608
- }
1609
- }
1610
- }
1576
+ if (readBuffer.Length () == 0 )
1577
+ {
1578
+ break ;
1611
1579
}
1612
-
1580
+
1613
1581
if (config.fileCache ) {
1614
- callback ( " " , " path " , config. path );
1582
+ co_await outputStream. WriteAsync (readBuffer );
1615
1583
}
1616
1584
else {
1617
- callback (" " , " result" , resultOutput.str ());
1585
+ readContents = winrt::to_string (CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer));
1586
+ resultOutput << readContents;
1587
+ }
1588
+
1589
+ if (progressInfo.count > -1 || progressInfo.interval > -1 ) {
1590
+ chunkStream << readContents;
1591
+
1592
+ currentProgressTime = winrt::clock::now ().time_since_epoch ().count () / 10000 ;
1593
+ if ((currentProgressTime - initialProgressTime >= progressInfo.interval && progressInfo.interval > -1 ) ||
1594
+ (totalRead >= progressInterval && progressInfo.count > -1 )) {
1595
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" RNFetchBlobProgress" ,
1596
+ Microsoft::ReactNative::JSValueObject{
1597
+ { " taskId" , taskId },
1598
+ { " written" , int64_t (totalRead) },
1599
+ { " total" , contentLength.Type () == PropertyType::UInt64 ?
1600
+ Microsoft::ReactNative::JSValue (contentLength.Value ()) :
1601
+ Microsoft::ReactNative::JSValue{nullptr } },
1602
+ { " chunk" , chunkStream.str () },
1603
+ });
1604
+ chunkStream.clear ();
1605
+ initialProgressTime = winrt::clock::now ().time_since_epoch ().count () / 10000 ;
1606
+ if (progressInfo.count > -1 ) {
1607
+ read = 0 ;
1608
+ }
1609
+ }
1618
1610
}
1619
1611
}
1612
+
1613
+ if (config.fileCache ) {
1614
+ callback (" " , " path" , config.path );
1615
+ }
1620
1616
else {
1621
- callback (" RNFetchBlob request timed out " , " error " , " " );
1617
+ callback (" " , " result " , resultOutput. str () );
1622
1618
}
1619
+ // callback("RNFetchBlob request timed out", "error", "");
1623
1620
}
1624
1621
catch (const hresult_error& ex)
1625
1622
{
0 commit comments