Skip to content

Commit

Permalink
Support Octyne v1.2 async + basePath compression.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Aug 27, 2023
1 parent 03a365a commit a30e5d8
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,53 @@ const MassActionDialog = ({
handleClose()
if (operation === 'compress') {
setOverlay(`Compressing ${files.length} files on the server.`)
const json = files.map(f => path + f)
const archiveTypeParam = archiveType.startsWith('tar') ? '&archiveType=tar&compress=' + (
archiveType === 'tar.gz' ? 'gzip'
: archiveType === 'tar.xz' ? 'xz'
: archiveType === 'tar.zst' ? 'zstd'
: 'false'
) : ''
const uri = archiveTypeParam === 'zip' ? endpoint : endpoint + '/v2'
ky.post(`${uri}?path=${encodeURIComponent(path + newPath + '.' + archiveType)}${archiveTypeParam}`, { json }).then(res => {
setOverlay('')
ky.post(`${endpoint}/v2\
?async=true\
&path=${encodeURIComponent(path + newPath + '.' + archiveType)}${archiveTypeParam}\
&basePath=${encodeURIComponent(path)}`, { json: files }).then(res => {
if (res.ok) {
reload()
setMessage('Compressed all files successfully!')
} else if (res.status === 404 && archiveTypeParam !== 'zip') {
// 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 }>()
if (res.finished || res.error) {
reload()
setOverlay('')
setMessage(res.error ?? 'Compressed all files successfully!')
break
}
await new Promise(resolve => setTimeout(resolve, 1000))
}
}).catch(() => setMessage('Failed to compress the files!'))
} else if (res.status === 404 && archiveType !== 'zip') {
setOverlay('')
setMessage('Compressing `tar` archives requires Octyne v1.2 or newer!')
} else setMessage('Failed to compress the files!')
} 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!') })
} else {
setOverlay('')
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!') })
return
}
Expand Down

0 comments on commit a30e5d8

Please sign in to comment.