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 Checkbox from '@/Components/Checkbox'; import CreateFile from './Components/CreateFile'; import EditFile from './Components/EditFile'; import DeleteFiles from './Components/DeleteFiles'; import RenameFile from './Components/RenameFile'; import UploadFile from './Components/UploadFile'; import { LuFolderPlus } from "react-icons/lu"; import { LuFilePlus2 } from "react-icons/lu"; import { LuDelete } from "react-icons/lu"; import { MdOutlineDriveFileRenameOutline } from "react-icons/md"; import { IoMdCut } from "react-icons/io"; import { MdCopyAll } from "react-icons/md"; import { BiPaste } from "react-icons/bi"; import { TbFileTypeZip } from "react-icons/tb"; import { VscFileZip } from "react-icons/vsc"; import { FaUpload } from "react-icons/fa"; import { PiSelectionAllBold } from "react-icons/pi"; const Filemanager = () => { const [files, setFiles] = useState([]); const [path, setPath] = useState("/"); const [goBack, setGoBack] = useState(false); const [spinner, showSpinner] = useState(true); const [selectedPaths, setSelectedPaths] = useState([]); const [createFileType, setCreateFileType] = useState(false); const [editFile, setEditFile] = useState(false); const [showConfirmDelete, setShowConfirmDelete] = useState(false); const [renameFile, setRenameFile] = useState(false); const [showUploadFile, setShowUploadFile] = useState(false); const [copyFiles, setCopyFiles] = useState(false); const [cutFiles, setCutFiles] = useState(false); useEffect(() => { cdIntoPath(path); }, [editFile]); const cdIntoPath = async (path) => { setPath(path); showSpinner(true); try { const response = await fetch(`/filemanager/get-directory-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 handleFileClick = (file) => { setSelectedPaths((prevSelected) => prevSelected.includes(file.path) ? prevSelected.filter((path) => path !== file.path) : [...prevSelected, file.path] ); }; const handleDoubleClick = (file) => { if (cutFiles && selectedPaths.includes(file.path)) { toast('Cannot cut files into a path that already contains them', { type: 'error' }); return; } if (file.type == "dir") { cdIntoPath(file.path); } // do not edit images/videos based on extensions const extension = file.path.split('.').pop(); const imagesAndVideos = ['jpg', 'jpeg', 'png', 'gif', 'mp4', 'mp3', 'webm', 'wav', 'ogg', 'flac', 'mkv', 'mov', 'avi', 'wmv', 'm4v']; if (file.type == "file" && !imagesAndVideos.includes(extension)) { setEditFile(file.path); } } const selectAll = () => { // toggle between all selected paths if (selectedPaths.length === files.length) { setSelectedPaths([]); } else { setSelectedPaths(files.map((file) => file.path)); } }; const pasteFiles = async (pasteFromAction) => { window.axios.patch('/filemanager/paste-files', { filesToPaste: selectedPaths, intoPath: path, pasteFromAction }).then((response) => { setSelectedPaths([]); setCutFiles(false); cdIntoPath(path); toast(response.data.message, { type: 'success' }) }).catch((error) => { if (error?.response?.data?.error) { toast(error.response.data.error, { type: 'error' }); } else { toast(error.message, { type: 'error' }); } console.log(error); }); } const formatBytes = (bytes, decimals = 2) => { if (bytes === 0) return "0 B"; const sizes = ["B", "KB", "MB", "GB", "TB"]; const factor = Math.floor(Math.log(bytes) / Math.log(1024)); return `${parseFloat((bytes / Math.pow(1024, factor)).toFixed(decimals))} ${sizes[factor]}`; }; const confirmDelete = () => { setShowConfirmDelete(true); }; if (spinner) { return (

Filemanager

} >
Loading files list...
) }; return (

Filemanager

} >
{!cutFiles ? ( ) : ( ) } {cutFiles && ( )} {copyFiles && ( )} {/* here make it if its only one file selected and it ends in .zip*/}
{goBack && goBack != "" && (
Path: {path}
)} {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) => (
handleDoubleClick(file)} >
handleFileClick(file)} className="-mt-1 mr-1" />
{file.type === "dir" ? (
) : (
)}
{file.path.split('/').pop()} {selectedPaths.includes(file.path) && cutFiles && }
{typeof file.file_size == "undefined" ? "--" : formatBytes(file.file_size)}
))}
); } export default Filemanager