Skip to content

Commit

Permalink
Support new tar compression APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Aug 25, 2023
1 parent a91fc53 commit 03a365a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
10 changes: 6 additions & 4 deletions imports/dashboard/files/fileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useKy from '../../helpers/useKy'

import Editor from './editor'
import Overlay from './overlay'
import { joinPath, normalisePath, parentPath, uploadFormData } from './fileUtils'
import { archiveRegex, joinPath, normalisePath, parentPath, uploadFormData } from './fileUtils'
import UploadButton from './uploadButton'
import FileList, { type File } from './fileList'
import MassActionDialog from './massActionDialog'
Expand Down Expand Up @@ -283,10 +283,12 @@ const FileManager = (props: {
setMenuOpen('')
setFetching(true)
const a = await ky.post(`server/${server}/decompress?path=${euc(path + menuOpen)}`, {
body: path + menuOpen.split('.').slice(0, -1).join('.')
body: path + menuOpen.replace(archiveRegex, '')
})
.json<{ error: string }>()
if (a.error) setMessage(a.error)
if (a.error?.includes('ZIP file') && !menuOpen.endsWith('.zip')) {
setMessage('Archive failed to decompress! Update Octyne to v1.2+ to decompress this file.')
} else if (a.error) setMessage(a.error)
setFetching(false)
setMenuOpen('')
fetchFiles()
Expand Down Expand Up @@ -535,7 +537,7 @@ const FileManager = (props: {
<MenuItem onClick={() => setModifyFileDialogOpen('copy')}>Copy</MenuItem>
<MenuItem onClick={handleDeleteMenuButton}>Delete</MenuItem>
{!selectedFile.folder && <MenuItem onClick={handleDownloadMenuButton}>Download</MenuItem>}
{!selectedFile.folder && selectedFile.name.endsWith('.zip') && (
{!selectedFile.folder && archiveRegex.test(selectedFile.name) && (
<MenuItem onClick={handleDecompressMenuButton}>Decompress</MenuItem>
)}
</Menu>
Expand Down
2 changes: 2 additions & 0 deletions imports/dashboard/files/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export const uploadFormData = async (
xhr.setRequestHeader('Authorization', localStorage.getItem('ecthelion:token') ?? '')
xhr.send(formData)
})

export const archiveRegex = /(\.zip|\.tar(\.(gz|bz2|bz|xz|zst))?|\.tgz|\.tbz2|\.tbz|\.txz|\.tzst)$/i
34 changes: 31 additions & 3 deletions imports/dashboard/files/massActionDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState } from 'react'
import { type KyInstance } from 'ky/distribution/types/ky'
import {
Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button, TextField
Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button, TextField,
Select, InputLabel, FormControl, MenuItem
} from '@mui/material'

const MassActionDialog = ({
Expand All @@ -17,6 +18,7 @@ const MassActionDialog = ({
path: string
ky: KyInstance
}): JSX.Element => {
const [archiveType, setArchiveType] = useState<'zip' | 'tar' | 'tar.gz' | 'tar.xz' | 'tar.zst'>('zip')
const [newPath, setNewPath] = useState('')
const move = operation === 'move' ? 'Move' : operation === 'compress' ? 'Compress' : 'Copy'
const moved = operation === 'move' ? 'Moved' : operation === 'compress' ? 'Compressed' : 'Copied'
Expand All @@ -27,13 +29,22 @@ const MassActionDialog = ({
if (operation === 'compress') {
setOverlay(`Compressing ${files.length} files on the server.`)
const json = files.map(f => path + f)
ky.post(`${endpoint}?path=${encodeURIComponent(path + newPath + '.zip')}`, { json }).then(res => {
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('')
if (res.ok) {
reload()
setMessage('Compressed all files successfully!')
} else if (res.status === 404 && archiveTypeParam !== 'zip') {
setMessage('Compressing `tar` archives requires Octyne v1.2 or newer!')
} else setMessage('Failed to compress the files!')
}).catch(() => setMessage('Failed to compress the files!'))
}).catch(() => { setOverlay(''); setMessage('Failed to compress the files!') })
return
}
let left = files.length
Expand Down Expand Up @@ -80,6 +91,23 @@ const MassActionDialog = ({
onChange={e => setNewPath(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter') { e.preventDefault(); handleOperation() } }}
/>
{operation === 'compress' && (
<FormControl fullWidth sx={{ mt: 1 }}>
<InputLabel color='secondary'>Archive Type</InputLabel>
<Select
color='secondary'
value={archiveType}
label='Archive Type'
onChange={e => setArchiveType(e.target.value as any)}
>
<MenuItem value='zip'>zip</MenuItem>
<MenuItem value='tar'>tar</MenuItem>
<MenuItem value='tar.gz'>tar.gz</MenuItem>
<MenuItem value='tar.xz'>tar.xz</MenuItem>
<MenuItem value='tar.zst'>tar.zst</MenuItem>
</Select>
</FormControl>
)}
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color='secondary'>Cancel</Button>
Expand Down

0 comments on commit 03a365a

Please sign in to comment.