Skip to content

Commit

Permalink
Add (de)compression support.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Mar 30, 2021
1 parent 29a96a7 commit 080601f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
29 changes: 27 additions & 2 deletions imports/dashboard/files/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const Files = (props: { path: string }) => {
const [folderPromptOpen, setFolderPromptOpen] = useState(false)
const [massActionMenuOpen, setMassActionMenuOpen] = useState<HTMLButtonElement | null>(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'
Expand Down Expand Up @@ -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 && (
Expand All @@ -405,6 +407,7 @@ const Files = (props: { path: string }) => {
<MenuItem onClick={() => setMassActionDialogOpen('move')}>Move</MenuItem>
<MenuItem onClick={() => setMassActionDialogOpen('copy')}>Copy</MenuItem>
<MenuItem onClick={async () => handleFilesDelete()}>Delete</MenuItem>
<MenuItem onClick={() => setMassActionDialogOpen('compress')}>Compress</MenuItem>
</Menu>
)}
{menuOpen && (
Expand Down Expand Up @@ -451,6 +454,28 @@ const Files = (props: { path: string }) => {
Download
</MenuItem>
)}
{(() => {
const file = files && files.find(e => e.name === menuOpen)
return file && !file.folder && file.name.endsWith('.zip')
})() && (
<MenuItem
onClick={async () => {
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
</MenuItem>
)}
</Menu>
)}
{message && <Message message={message} setMessage={setMessage} />}
Expand Down
35 changes: 28 additions & 7 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = []
Expand All @@ -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)
}))
Expand All @@ -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. */}
<Dialog open onClose={handleClose}>
<DialogTitle>{move} Files (WIP)</DialogTitle>
<DialogContent>
<DialogContentText>Enter path of folder to {operation} to:</DialogContentText>
<DialogContentText>{prompt}</DialogContentText>
<TextField
autoFocus
fullWidth
Expand Down

0 comments on commit 080601f

Please sign in to comment.