import { useState, useEffect } from 'react'; import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'; import { ImSpinner9 } from "react-icons/im"; import { VscFileSubmodule } from "react-icons/vsc"; import { FaFolderClosed } from "react-icons/fa6"; import { RiFolderReceivedLine } from "react-icons/ri"; import { FileIcon, defaultStyles } from 'react-file-icon'; import { ToastContainer, toast } from 'react-toastify'; import { MdInfoOutline } from "react-icons/md"; const Filemanager = () => { const [files, setFiles] = useState([]); const [path, setPath] = useState([]); const [goBack, setGoBack] = useState(false); const [spinner, showSpinner] = useState(true); useEffect(() => { 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) { // Parse the error message from the response body const errorData = await response.json(); const errorMessage = errorData.error || response.statusText; // Display the error message in a toast toast(errorMessage, { type: 'error' }); showSpinner(false); return; } const reader = response.body.getReader(); const decoder = new TextDecoder(); let chunks = ''; // Read the stream progressively while (true) { const { value, done } = await reader.read(); if (done) break; try { chunks += decoder.decode(value, { stream: true }); const data = JSON.parse(chunks); setFiles(data.files); setGoBack(data.goBack); console.log(data); } catch (error) { console.log(error.toString()); toast(error.toString(), { type: 'error' }); } } showSpinner(false); }; return (

Filemanager

} >
Double-Click on a directory icon to enter / cd into it
{spinner && (
)} {goBack && goBack != "" && (
)} {files.sort((a, b) => { if (a.type === 'dir' && b.type !== 'dir') return -1; if (a.type !== 'dir' && b.type === 'dir') return 1; return 0; }).map((file, index) => (
{file.type === "dir" ? (<> ) : (
)}
{file.path}
))}
); } export default Filemanager