@@ -389,91 +389,108 @@ catch (const hresult_error& ex)
389
389
}
390
390
391
391
// readStream - no promises, callbacks, only event emission
392
- winrt::fire_and_forget RNFetchBlob::readStream (
392
+ void RNFetchBlob::readStream (
393
393
std::string path,
394
394
std::string encoding,
395
395
uint32_t bufferSize,
396
396
uint64_t tick,
397
397
const std::string streamId) noexcept
398
- try
398
+ try
399
399
{
400
400
EncodingOptions usedEncoding;
401
- if (encoding.compare (" utf8" ))
401
+ if (encoding.compare (" utf8" ) == 0 )
402
402
{
403
403
usedEncoding = EncodingOptions::UTF8;
404
404
}
405
- else if (encoding.compare (" base64" ))
405
+ else if (encoding.compare (" base64" ) == 0 )
406
406
{
407
407
usedEncoding = EncodingOptions::BASE64;
408
408
}
409
- else if (encoding.compare (" ascii" ))
409
+ else if (encoding.compare (" ascii" ) == 0 )
410
410
{
411
411
usedEncoding = EncodingOptions::ASCII;
412
412
}
413
413
else
414
414
{
415
415
// Wrong encoding
416
- co_return ;
416
+ return ;
417
417
}
418
418
419
- uint32_t chunkSize = usedEncoding == EncodingOptions::BASE64 ? 4095 : 4096 ;
419
+ uint32_t chunkSize{ usedEncoding == EncodingOptions::BASE64 ? ( uint32_t ) 4095 : ( uint32_t ) 4096 } ;
420
420
if (bufferSize > 0 )
421
421
{
422
422
chunkSize = bufferSize;
423
423
}
424
424
425
425
winrt::hstring directoryPath, fileName;
426
426
splitPath (path, directoryPath, fileName);
427
- StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
428
- StorageFile file{ co_await folder.GetFileAsync (fileName) };
427
+ StorageFolder folder{ StorageFolder::GetFolderFromPathAsync (directoryPath). get ( ) };
428
+ StorageFile file{ folder.GetFileAsync (fileName). get ( ) };
429
429
430
- Streams::IRandomAccessStream stream{ co_await file.OpenAsync (FileAccessMode::Read) };
430
+ Streams::IRandomAccessStream stream{ file.OpenAsync (FileAccessMode::Read). get ( ) };
431
431
Buffer buffer{ chunkSize };
432
432
const TimeSpan time{ tick };
433
433
IAsyncAction timer;
434
434
435
435
for (;;)
436
436
{
437
- auto readBuffer = co_await stream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None);
437
+ auto readBuffer = stream.ReadAsync (buffer, buffer.Capacity (), InputStreamOptions::None). get ( );
438
438
if (readBuffer.Length () == 0 )
439
439
{
440
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
441
+ winrt::Microsoft::ReactNative::JSValueObject{
442
+ {" event" , " end" },
443
+ });
440
444
break ;
441
445
}
442
446
if (usedEncoding == EncodingOptions::BASE64)
443
447
{
444
448
// TODO: Investigate returning wstrings as parameters
445
449
winrt::hstring base64Content{ Cryptography::CryptographicBuffer::EncodeToBase64String (readBuffer) };
446
- m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , [&streamId, &base64Content](React::IJSValueWriter const & argWriter) {
447
- WriteValue (argWriter, streamId);
448
- argWriter.WriteObjectBegin ();
449
- React::WriteProperty (argWriter, " event" , L" data" );
450
- React::WriteProperty (argWriter, " detail" , base64Content);
451
- argWriter.WriteObjectEnd ();
450
+ // m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", [&streamId, &base64Content](React::IJSValueWriter const& argWriter) {
451
+ // WriteValue(argWriter, streamId);
452
+ // argWriter.WriteObjectBegin();
453
+ // React::WriteProperty(argWriter, "event", L"data");
454
+ // React::WriteProperty(argWriter, "detail", base64Content);
455
+ // argWriter.WriteObjectEnd();
456
+ // });
457
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
458
+ winrt::Microsoft::ReactNative::JSValueObject{
459
+ {" event" , " data" },
460
+ {" detail" , winrt::to_string (base64Content)},
452
461
});
453
462
}
454
463
else
455
464
{
456
- // TODO: Investigate returning wstrings as parameters
465
+ // TODO: Sending events not working as necessary with writers
457
466
std::string utf8Content{ winrt::to_string (Cryptography::CryptographicBuffer::ConvertBinaryToString (BinaryStringEncoding::Utf8, readBuffer)) };
458
467
if (usedEncoding == EncodingOptions::ASCII)
459
468
{
460
469
461
470
// std::string asciiContent{ winrt::to_string(utf8Content) };
462
471
std::string asciiContent{ utf8Content };
463
- // std::wstring asciiResult{ winrt::to_hstring(asciiContent) };
464
472
// emit ascii content
465
473
m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
466
474
winrt::Microsoft::ReactNative::JSValueObject{
467
- {" data" , asciiContent},
475
+ {" event" , " data" },
476
+ {" detail" , asciiContent},
468
477
});
469
478
}
470
479
else
471
480
{
472
481
// emit utf8 content
473
482
m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
474
483
winrt::Microsoft::ReactNative::JSValueObject{
475
- {" data" , utf8Content},
484
+ {" event" , " data" },
485
+ {" detail" , utf8Content},
476
486
});
487
+ // m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", [&streamId, &utf8Content](React::IJSValueWriter const& argWriter) {
488
+ // WriteValue(argWriter, streamId);
489
+ // argWriter.WriteObjectBegin();
490
+ // React::WriteProperty(argWriter, "event", L"data");
491
+ // React::WriteProperty(argWriter, "detail", utf8Content);
492
+ // argWriter.WriteObjectEnd();
493
+ // });
477
494
}
478
495
}
479
496
// sleep
@@ -485,10 +502,23 @@ winrt::fire_and_forget RNFetchBlob::readStream(
485
502
}
486
503
catch (const hresult_error& ex)
487
504
{
488
- m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , L" DownloadBegin" ,
489
- winrt::Microsoft::ReactNative::JSValueObject{
490
- {streamId, winrt::to_string (ex.message ()).c_str ()},
491
- });
505
+ hresult result{ ex.code () };
506
+ if (result == 0x80070002 ) // FileNotFoundException
507
+ {
508
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
509
+ winrt::Microsoft::ReactNative::JSValueObject{
510
+ {" event" , " error" },
511
+ {" ENOENT" , " No such file '" + path + " '" },
512
+ });
513
+ }
514
+ else
515
+ {
516
+ m_reactContext.CallJSFunction (L" RCTDeviceEventEmitter" , L" emit" , streamId,
517
+ winrt::Microsoft::ReactNative::JSValueObject{
518
+ {" event" , " error" },
519
+ {" EUNSPECIFIED" , winrt::to_string (ex.message ())},
520
+ });
521
+ }
492
522
}
493
523
494
524
0 commit comments