better handling of stream buffer

This commit is contained in:
Alex
2025-02-02 13:24:01 +02:00
parent d5edb4ce1d
commit 06daecd3de

View File

@@ -20,11 +20,50 @@ const Filemanager = () => {
cdIntoPath('/');
}, []);
const cdIntoPath = async (path) => {
setPath(path);
showSpinner(true);
try {
const response = await fetch(`/filemanager/get-contents?path=${path}`);
if (!response.ok) {
const errorData = await response.json();
const errorMessage = errorData.error || response.statusText;
toast(errorMessage, { type: 'error' });
showSpinner(false);
return;
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
// Decode the chunk and append to the buffer
buffer += decoder.decode(value, { stream: true });
}
const json = JSON.parse(buffer.trim());
setFiles(json.files);
setGoBack(json.goBack);
} catch (error) {
// Handle network errors or other exceptions
toast(error.message, { type: 'error' });
} finally {
showSpinner(false);
}
};
const cdIntoPathOK = async (path) => {
setPath(path);
showSpinner(true);
const response = await fetch(`/filemanager/get-contents?path=${path}`);
if (!response.ok) {