create effect for file stream: fix #2777
Use this effect instead of the "file://" protocol
This commit is contained in:
parent
98820ad1f4
commit
dc10a2ddf1
1 changed files with 36 additions and 0 deletions
36
ui/effects/use-stream-file.js
Normal file
36
ui/effects/use-stream-file.js
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue