create effect for file stream: fix #2777

Use this effect instead of the "file://" protocol
This commit is contained in:
Baltazar Gomez 2020-05-08 13:33:32 -05:00 committed by Sean Yesmunt
parent 98820ad1f4
commit dc10a2ddf1

View file

@ -0,0 +1,36 @@
// @flow
import React from 'react';
// Returns a blob from the download path
export default function useFileStream(fileStream: (?string) => any) {
const [state, setState] = React.useState({
error: false,
content: null,
loading: true,
});
React.useEffect(() => {
if (fileStream) {
let chunks = []
const stream = fileStream();
stream.on('data', chunk => {
chunks.push(chunk)
});
stream.on('end', () => {
const buffer = Buffer.concat(chunks)
const blob = new Blob([buffer])
setState({ content: blob, loading: false });
});
stream.on('error', () => {
setState({ error: true, loading: false });
});
}
}, []);
return state;
}