diff --git a/imports/dashboard/files/massActionDialog.tsx b/imports/dashboard/files/massActionDialog.tsx index efa679f..dbb6ecb 100644 --- a/imports/dashboard/files/massActionDialog.tsx +++ b/imports/dashboard/files/massActionDialog.tsx @@ -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 }