Skip to content

Commit dae3909

Browse files
committed
Implement test method for readStream
1 parent 99cc737 commit dae3909

File tree

3 files changed

+107
-28
lines changed

3 files changed

+107
-28
lines changed

RNFetchBlobWin/App.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const App: () => React$Node = () => {
5454
const [writeStreamParam, setWriteStreamParam] = useState('');
5555
const [writeEncodeStreamParam, setWriteStreamEncodeParam] = useState('utf8');
5656

57+
const [readStreamParam, setReadStreamParam] = useState('');
58+
const [readEncodeStreamParam, setReadStreamEncodeParam] = useState('utf8');
59+
5760
// Methods ********************************************************************
5861
// exists()
5962
const existsCall = () => {
@@ -331,7 +334,22 @@ const App: () => React$Node = () => {
331334
});
332335
}
333336
}
334-
}
337+
}
338+
339+
// readStream
340+
const readStreamCall = () => {
341+
RNFetchBlob.fs.readStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + readStreamParam, 'utf8')
342+
.then((stream) => {
343+
let data = '';
344+
stream.open();
345+
stream.onData((chunk) => {
346+
data += chunk;
347+
})
348+
stream.onEnd(() => {
349+
console.log('data:' + data);
350+
})
351+
});
352+
}
335353

336354
// App ************************************************************************
337355
return (
@@ -682,7 +700,6 @@ const App: () => React$Node = () => {
682700
<Picker.Item label="UTF8" value="utf8" />
683701
<Picker.Item label="Base64" value="base64" />
684702
<Picker.Item label="ASCII" value="ascii" />
685-
<Picker.Item label="URI" value="uri" />
686703
</Picker>
687704
</View>
688705
<Button
@@ -698,6 +715,38 @@ const App: () => React$Node = () => {
698715
</View>
699716
</View>
700717

718+
<View style={styles.body}>
719+
<View style={styles.sectionContainer}>
720+
<Text style={styles.sectionTitle}>
721+
{"ReadStream - readStream()"}
722+
</Text>
723+
<View style={styles.sectionDescription}>
724+
<TextInput style = {styles.input}
725+
placeholder = "Source path"
726+
onChangeText={readStreamParam => setReadStreamParam(readStreamParam)}
727+
placeholderTextColor = "#9a73ef"
728+
autoCapitalize = "none"
729+
/>
730+
731+
<Picker
732+
readEncodeStreamParam={readEncodeStreamParam}
733+
onChangeText={readPositionParam => setReadPositionParam(readPositionParam)}
734+
style={{ height: 50, width: 150 }}
735+
onValueChange={(itemValue, itemIndex) => setReadStreamEncodeParam(itemValue)}
736+
>
737+
<Picker.Item label="UTF8" value="utf8" />
738+
<Picker.Item label="Base64" value="base64" />
739+
<Picker.Item label="ASCII" value="ascii" />
740+
</Picker>
741+
</View>
742+
<Button
743+
title="Read"
744+
color="#9a73ef"
745+
onPress={readStreamCall}
746+
/>
747+
</View>
748+
</View>
749+
701750
</ScrollView>
702751
</SafeAreaView>
703752
</>

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.cpp

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -389,91 +389,108 @@ catch (const hresult_error& ex)
389389
}
390390

391391
// readStream - no promises, callbacks, only event emission
392-
winrt::fire_and_forget RNFetchBlob::readStream(
392+
void RNFetchBlob::readStream(
393393
std::string path,
394394
std::string encoding,
395395
uint32_t bufferSize,
396396
uint64_t tick,
397397
const std::string streamId) noexcept
398-
try
398+
try
399399
{
400400
EncodingOptions usedEncoding;
401-
if (encoding.compare("utf8"))
401+
if (encoding.compare("utf8") == 0)
402402
{
403403
usedEncoding = EncodingOptions::UTF8;
404404
}
405-
else if (encoding.compare("base64"))
405+
else if (encoding.compare("base64") == 0)
406406
{
407407
usedEncoding = EncodingOptions::BASE64;
408408
}
409-
else if (encoding.compare("ascii"))
409+
else if (encoding.compare("ascii") == 0)
410410
{
411411
usedEncoding = EncodingOptions::ASCII;
412412
}
413413
else
414414
{
415415
//Wrong encoding
416-
co_return;
416+
return;
417417
}
418418

419-
uint32_t chunkSize = usedEncoding == EncodingOptions::BASE64 ? 4095 : 4096;
419+
uint32_t chunkSize{ usedEncoding == EncodingOptions::BASE64 ? (uint32_t)4095 : (uint32_t)4096 };
420420
if (bufferSize > 0)
421421
{
422422
chunkSize = bufferSize;
423423
}
424424

425425
winrt::hstring directoryPath, fileName;
426426
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() };
429429

430-
Streams::IRandomAccessStream stream{ co_await file.OpenAsync(FileAccessMode::Read) };
430+
Streams::IRandomAccessStream stream{ file.OpenAsync(FileAccessMode::Read).get() };
431431
Buffer buffer{ chunkSize };
432432
const TimeSpan time{ tick };
433433
IAsyncAction timer;
434434

435435
for (;;)
436436
{
437-
auto readBuffer = co_await stream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None);
437+
auto readBuffer = stream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None).get();
438438
if (readBuffer.Length() == 0)
439439
{
440+
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", streamId,
441+
winrt::Microsoft::ReactNative::JSValueObject{
442+
{"event", "end"},
443+
});
440444
break;
441445
}
442446
if (usedEncoding == EncodingOptions::BASE64)
443447
{
444448
// TODO: Investigate returning wstrings as parameters
445449
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)},
452461
});
453462
}
454463
else
455464
{
456-
// TODO: Investigate returning wstrings as parameters
465+
// TODO: Sending events not working as necessary with writers
457466
std::string utf8Content{ winrt::to_string(Cryptography::CryptographicBuffer::ConvertBinaryToString(BinaryStringEncoding::Utf8, readBuffer)) };
458467
if (usedEncoding == EncodingOptions::ASCII)
459468
{
460469

461470
//std::string asciiContent{ winrt::to_string(utf8Content) };
462471
std::string asciiContent{ utf8Content };
463-
//std::wstring asciiResult{ winrt::to_hstring(asciiContent) };
464472
// emit ascii content
465473
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", streamId,
466474
winrt::Microsoft::ReactNative::JSValueObject{
467-
{"data", asciiContent},
475+
{"event", "data"},
476+
{"detail", asciiContent},
468477
});
469478
}
470479
else
471480
{
472481
//emit utf8 content
473482
m_reactContext.CallJSFunction(L"RCTDeviceEventEmitter", L"emit", streamId,
474483
winrt::Microsoft::ReactNative::JSValueObject{
475-
{"data", utf8Content},
484+
{"event", "data"},
485+
{"detail", utf8Content},
476486
});
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+
// });
477494
}
478495
}
479496
// sleep
@@ -485,10 +502,23 @@ winrt::fire_and_forget RNFetchBlob::readStream(
485502
}
486503
catch (const hresult_error& ex)
487504
{
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+
}
492522
}
493523

494524

RNFetchBlobWin/windows/RNFetchBlobWin/RNFetchBlob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct RNFetchBlob
8686

8787
// readStream
8888
REACT_METHOD(readStream);
89-
winrt::fire_and_forget RNFetchBlob::readStream(
89+
void RNFetchBlob::readStream(
9090
std::string path,
9191
std::string encoding,
9292
uint32_t bufferSize,

0 commit comments

Comments
 (0)