lbry-desktop/ui/effects/use-stream-file.js
Baltazar Gomez dc10a2ddf1 create effect for file stream: fix #2777
Use this effect instead of the "file://" protocol
2020-05-13 10:18:36 -04:00

36 lines
746 B
JavaScript

// @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;
}