From 1f6dd89e5556aedbcca55c9ee5876fc75063a15f Mon Sep 17 00:00:00 2001 From: Baltazar Gomez Date: Fri, 8 May 2020 13:59:05 -0500 Subject: [PATCH] create effect to load blob from streaming url Use this on the web for files that need to wait for the full stream --- ui/effects/use-stream.js | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ui/effects/use-stream.js diff --git a/ui/effects/use-stream.js b/ui/effects/use-stream.js new file mode 100644 index 000000000..d317a2aab --- /dev/null +++ b/ui/effects/use-stream.js @@ -0,0 +1,45 @@ +// @flow +import React from 'react'; +import https from 'https'; + +// Returns web blob from the streaming url +export default function useStream(url: (?string) => any) { + + const [state, setState] = React.useState({ + error: false, + content: null, + loading: false, + }); + + React.useEffect(() => { + if (url) { + let chunks = []; + + // Start loading state + setState({loading: true}) + + https.get( + url, + response => { + if (response.statusCode >= 200 && response.statusCode < 300) { + let chunks = [] + response.on('data', function(chunk) { + chunks.push(chunk); + }); + response.on('end', () => { + const buffer = Buffer.concat(chunks) + const blob = new Blob([buffer]) + console.info(response) + setState({ content: blob, loading: false }); + }); + } else { + console.info(response) + setState({ error: true, loading: false }); + } + } + ); + } + }, []); + + return state; +}