Skip to content

Commit

Permalink
try using axios for downloading zip
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-laplante committed May 11, 2024
1 parent 470e0f0 commit 61d8846
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
56 changes: 53 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/react": "^18.2.77",
"@types/react-dom": "^18.2.25",
"ace-builds": "^1.33.1",
"axios": "^1.6.8",
"classnames": "^2.5.1",
"immer": "^10.1.1",
"pyodide": "^0.25.1",
Expand Down
61 changes: 13 additions & 48 deletions src/components/FetchWithProgress.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import React, {useEffect, useState} from 'react';
import axios from "axios";


interface FetchProgressProps {
Expand All @@ -13,55 +14,19 @@ const FetchWithProgress: React.FC<FetchProgressProps> = (props: FetchProgressPro
const [error, setError] = useState<string | null>(null);

const fetchData = () => {
setLoading(true);
setError(null);
fetch(props.url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const contentLength = response.headers.get('Content-Length');
if (!contentLength) {
throw new Error("Missing Content-Length header.");
}
const total = parseInt(contentLength, 10);
let loaded = 0;
async function work() {
const ret = await axios.get(props.url, {
onDownloadProgress: progressEvent => {
const percentage = Math.round(progressEvent.loaded * 100) / progressEvent.total;
setProgress(percentage);
},
responseType: "arraybuffer"
});

const reader = response.body!.getReader();
const stream = new ReadableStream({
start(controller) {
function push() {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
loaded += value!.length;
setProgress(Math.round((loaded / total) * 100));
controller.enqueue(value);
push();
}).catch(err => {
setError(`Error reading data: ${err.message}`);
controller.error(err);
});
}
push();
}
});
props.setData(ret.data);
}

return new Response(stream, { headers: { "Content-Type": "application/octet-stream" } });
})
.then(response => response.arrayBuffer())
.then(async buffer => {
console.log("set buffer");
props.setData(buffer);
setLoading(false);
})
.catch(err => {
setError(err.message);
console.error(err);
setLoading(false);
});
work();
};

return (
Expand Down

0 comments on commit 61d8846

Please sign in to comment.