Skip to content

Commit

Permalink
Add mass deletion progress, fixup mass action code
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Dec 14, 2024
1 parent 5b013f3 commit c419fb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
9 changes: 6 additions & 3 deletions imports/dashboard/files/fileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const FileManager = (props: {
const handleFilesDelete = (): void => {
setMassActionMenuOpen(null)
let total = filesSelected.length
setOverlay(`Deleting ${total} out of ${filesSelected.length} files.`)
setOverlay({ text: `Deleting ${total} out of ${filesSelected.length} files.`, progress: 0 })
const ops = []
for (let i = 0; i < filesSelected.length; i++) {
const file = filesSelected[i]
Expand All @@ -230,7 +230,10 @@ const FileManager = (props: {
ops.push(ky.delete(`server/${server}/file?path=${euc(path + file)}`).then(async r => {
if (r.status !== 200) {
setMessage(`Error deleting ${file}\n${(await r.json<{ error: string }>()).error}`)
} else setOverlay(`Deleting ${--total} out of ${filesSelected.length} files.`)
} else {
const progress = (filesSelected.length - total) * 100 / filesSelected.length
setOverlay({ text: `Deleting ${--total} out of ${filesSelected.length} files.`, progress })
}
if (localStorage.getItem('ecthelion:logAsyncMassActions')) console.log('Deleted ' + file)
}).catch(e => setMessage(`Error deleting ${file}\n${e}`)))
}
Expand Down Expand Up @@ -513,6 +516,7 @@ const FileManager = (props: {
<MassActionDialog
ky={ky}
path={path}
server={server ?? ''}
files={filesSelected}
reload={fetchFiles}
setOverlay={setOverlay}
Expand All @@ -522,7 +526,6 @@ const FileManager = (props: {
setMassActionMenuOpen(null)
setMassActionDialogOpen(false)
}}
endpoint={`server/${server}/${massActionDialogOpen === 'compress' ? 'compress' : 'file'}`}
/>
)}
{massActionMenuOpen && (
Expand Down
38 changes: 20 additions & 18 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
} from '@mui/material'

const MassActionDialog = ({
operation, reload, files, endpoint, ky, handleClose, path, setOverlay, setMessage
operation, reload, files, server, ky, handleClose, path, setOverlay, setMessage
}: {
reload: () => void
operation: 'move' | 'copy' | 'compress'
setOverlay: (message: string | { text: string, progress: number }) => void
setMessage: (message: string) => void
handleClose: () => void
endpoint: string
server: string
files: string[]
path: string
ky: KyInstance
Expand All @@ -33,15 +33,16 @@ const MassActionDialog = ({
: archiveType === 'tar.zst' ? 'zstd'
: 'false'
) : ''
ky.post(`${endpoint}/v2\
ky.post(`server/${server}/compress/v2\
?async=true\
&path=${encodeURIComponent(path + newPath + '.' + archiveType)}${archiveTypeParam}\
&basePath=${encodeURIComponent(path)}`, { json: files }).then(res => {
if (res.ok) {
// Poll the token every second until the compression is finished.
res.json<{ token: string }>().then(async ({ token }) => {
while (true) {
const res = await ky.get(`${endpoint}/v2?token=${token}`).json<{ finished: boolean, error: string }>()
const res = await ky.get(`server/${server}/compress/v2?token=${token}`)
.json<{ finished: boolean, error: string }>()
if (res.finished || res.error) {
reload()
setOverlay('')
Expand All @@ -57,17 +58,18 @@ const MassActionDialog = ({
} else if (res.status === 404) {
// Fallback to v1 API without async compression and basePath.
const json = files.map(f => path + f)
ky.post(`${endpoint}?path=${encodeURIComponent(path + newPath + '.zip')}`, { json }).then(res => {
setOverlay('')
if (res.ok) {
reload()
setMessage('Compressed all files successfully!')
} else {
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
ky.post(`server/${server}/compress?path=${encodeURIComponent(path + newPath + '.zip')}`, { json })
.then(res => {
setOverlay('')
if (res.ok) {
reload()
setMessage('Compressed all files successfully!')
} else {
res.json<{ error: string }>()
.then(({ error }) => setMessage(error ?? 'Failed to compress the files!'))
.catch(() => setMessage('Failed to compress the files!'))
}
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
} else {
setOverlay('')
res.json<{ error: string }>()
Expand All @@ -80,13 +82,13 @@ const MassActionDialog = ({
const handleMoveCopyOperation = (): void => {
let left = files.length
setOverlay({ text: `${moving} ${left} out of ${files.length} files.`, progress: 0 })
const operations = []
const requests = []
for (let i = 0; i < files.length; i++) {
const file = files[i]
// setOverlay(file)
const slash = newPath.endsWith('/') ? '' : '/'
const body = `${operation === 'move' ? 'mv' : 'cp'}\n${path}${file}\n${newPath}${slash}${file}`
operations.push(ky.patch(`${endpoint}?path=${encodeURIComponent(path + file)}`, { body })
requests.push(ky.patch(`server/${server}/file?path=${encodeURIComponent(path + file)}`, { body })
.then(async r => {
if (r.status !== 200) {
setMessage(`Error ${movingl} ${file}\n${(await r.json<{ error: string }>()).error}`)
Expand All @@ -97,7 +99,7 @@ const MassActionDialog = ({
})
.catch(e => setMessage(`Error ${movingl} ${file}\n${e}`)))
}
Promise.allSettled(operations).then(() => {
Promise.allSettled(requests).then(() => {
reload()
setOverlay('')
setMessage(moved + ' all files successfully!')
Expand Down

0 comments on commit c419fb3

Please sign in to comment.