From 080601fe156fddb3217c91af1318f3ab3215cbf3 Mon Sep 17 00:00:00 2001 From: Ibrahim Ansari Date: Tue, 30 Mar 2021 22:56:39 +0530 Subject: [PATCH] Add (de)compression support. --- imports/dashboard/files/files.tsx | 29 ++++++++++++++-- imports/dashboard/files/massActionDialog.tsx | 35 ++++++++++++++++---- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/imports/dashboard/files/files.tsx b/imports/dashboard/files/files.tsx index 149b31a..52129cd 100644 --- a/imports/dashboard/files/files.tsx +++ b/imports/dashboard/files/files.tsx @@ -67,7 +67,7 @@ const Files = (props: { path: string }) => { const [folderPromptOpen, setFolderPromptOpen] = useState(false) const [massActionMenuOpen, setMassActionMenuOpen] = useState(null) const [modifyFileDialogOpen, setModifyFileDialogOpen] = useState<''|'move'|'copy'|'rename'>('') - const [massActionDialogOpen, setMassActionDialogOpen] = useState<'move' | 'copy' | false>(false) + const [massActionDialogOpen, setMassActionDialogOpen] = useState<'move' | 'copy' | 'compress' | false>(false) const opip = !!(fetching) const serverIp = typeof router.query.node === 'string' @@ -392,7 +392,9 @@ const Files = (props: { path: string }) => { setMassActionMenuOpen(null) setMassActionDialogOpen(false) }} - endpoint={`${serverIp}/server/${router.query.server}/file`} + endpoint={`${serverIp}/server/${router.query.server}/${ + massActionDialogOpen === 'compress' ? 'compress' : 'file' + }`} /> )} {massActionMenuOpen && ( @@ -405,6 +407,7 @@ const Files = (props: { path: string }) => { setMassActionDialogOpen('move')}>Move setMassActionDialogOpen('copy')}>Copy handleFilesDelete()}>Delete + setMassActionDialogOpen('compress')}>Compress )} {menuOpen && ( @@ -451,6 +454,28 @@ const Files = (props: { path: string }) => { Download )} + {(() => { + const file = files && files.find(e => e.name === menuOpen) + return file && !file.folder && file.name.endsWith('.zip') + })() && ( + { + setMenuOpen('') + setFetching(true) + const a = await request( + serverIp, + `/server/${router.query.server}/decompress?path=${euc(path + menuOpen)}`, + { method: 'POST' } + ).then(async e => e.json()) + if (a.error) setMessage(a.error) + setFetching(false) + setMenuOpen('') + fetchFiles() + }} + > + Decompress + + )} )} {message && } diff --git a/imports/dashboard/files/massActionDialog.tsx b/imports/dashboard/files/massActionDialog.tsx index 6c33680..0bda721 100644 --- a/imports/dashboard/files/massActionDialog.tsx +++ b/imports/dashboard/files/massActionDialog.tsx @@ -4,9 +4,11 @@ import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button, TextField } from '@material-ui/core' -const MassActionDialog = ({ operation, reload, files, endpoint, handleClose, path, setOverlay, setMessage }: { +const MassActionDialog = ({ + operation, reload, files, endpoint, handleClose, path, setOverlay, setMessage +}: { reload: () => void, - operation: 'move' | 'copy', + operation: 'move' | 'copy' | 'compress', setOverlay: (message: string) => void, setMessage: (message: string) => void, handleClose: () => void, @@ -15,11 +17,28 @@ const MassActionDialog = ({ operation, reload, files, endpoint, handleClose, pat path: string }) => { const [newPath, setNewPath] = useState('') - const move = operation === 'move' ? 'Move' : 'Copy' - const moved = operation === 'move' ? 'Moved' : 'Copied' - const moving = operation === 'move' ? 'moving' : 'copying' + const move = operation === 'move' ? 'Move' : operation === 'compress' ? 'Compress' : 'Copy' + const moved = operation === 'move' ? 'Moved' : operation === 'compress' ? 'Compressed' : 'Copied' + const moving = operation === 'move' ? 'Moving' : operation === 'compress' ? 'Compressing ' : 'Copying' + const movingl = operation === 'move' ? 'moving' : operation === 'compress' ? 'compressing ' : 'copying' const handleOperation = () => { handleClose() + if (operation === 'compress') { + setOverlay(`Compressing ${files.length} files on the server.`) + const authorization = localStorage.getItem('token') + if (!authorization) return + fetch( + `${endpoint}?path=${encodeURIComponent(path + newPath)}`, + { method: 'POST', body: JSON.stringify(files.map(f => path + f)), headers: { authorization } } + ).then(res => { + setOverlay('') + if (res.ok) { + reload() + setMessage('Compressed all files successfully!') + } else setMessage('Failed to compress the files!') + }) + return + } let left = files.length setOverlay(`${moving} ${left} out of ${files.length} files.`) const operations = [] @@ -34,7 +53,7 @@ const MassActionDialog = ({ operation, reload, files, endpoint, handleClose, pat `${endpoint}?path=${encodeURIComponent(path + file)}`, { method: 'PATCH', body, headers: { Authorization: token } } ).then(async r => { - if (r.status !== 200) setMessage(`Error ${moving} ${file}\n${(await r.json()).error}`) + if (r.status !== 200) setMessage(`Error ${movingl} ${file}\n${(await r.json()).error}`) setOverlay(`${moving} ${--left} out of ${files.length} files.`) if (localStorage.getItem('logAsyncMassActions')) console.log(moved + ' ' + file) })) @@ -45,13 +64,15 @@ const MassActionDialog = ({ operation, reload, files, endpoint, handleClose, pat setMessage(moved + ' all files successfully!') }) } + const prompt = operation === 'compress' + ? 'Enter path to ZIP file to create:' : `Enter path of folder to ${operation} to:` return ( <> {/* Folder creation dialog. */} {move} Files (WIP) - Enter path of folder to {operation} to: + {prompt}