Show file mode on file listing, add ability to change file mode

This commit is contained in:
Matthew Penner 2020-11-29 14:46:35 -07:00
parent 8611ebb2d6
commit ed5613e207
9 changed files with 150 additions and 11 deletions

View file

@ -25,6 +25,7 @@ class FileObjectTransformer extends BaseDaemonTransformer
return [ return [
'name' => Arr::get($item, 'name'), 'name' => Arr::get($item, 'name'),
'mode' => Arr::get($item, 'mode'), 'mode' => Arr::get($item, 'mode'),
'mode_bits' => Arr::get($item, 'mode_bits'),
'size' => Arr::get($item, 'size'), 'size' => Arr::get($item, 'size'),
'is_file' => Arr::get($item, 'file', true), 'is_file' => Arr::get($item, 'file', true),
'is_symlink' => Arr::get($item, 'symlink', false), 'is_symlink' => Arr::get($item, 'symlink', false),

View file

@ -0,0 +1,14 @@
import http from '@/api/http';
interface Data {
file: string;
mode: string;
}
export default (uuid: string, directory: string, files: Data[]): Promise<void> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/files/chmod`, { root: directory, files })
.then(() => resolve())
.catch(reject);
});
};

View file

@ -5,6 +5,7 @@ export interface FileObject {
key: string; key: string;
name: string; name: string;
mode: string; mode: string;
modeBits: string,
size: number; size: number;
isFile: boolean; isFile: boolean;
isSymlink: boolean; isSymlink: boolean;

View file

@ -16,6 +16,7 @@ export const rawDataToFileObject = (data: FractalResponseData): FileObject => ({
key: `${data.attributes.is_file ? 'file' : 'dir'}_${data.attributes.name}`, key: `${data.attributes.is_file ? 'file' : 'dir'}_${data.attributes.name}`,
name: data.attributes.name, name: data.attributes.name,
mode: data.attributes.mode, mode: data.attributes.mode,
modeBits: data.attributes.mode_bits,
size: Number(data.attributes.size), size: Number(data.attributes.size),
isFile: data.attributes.is_file, isFile: data.attributes.is_file,
isSymlink: data.attributes.is_symlink, isSymlink: data.attributes.is_symlink,

View file

@ -0,0 +1,75 @@
import { fileBitsToString } from '@/helpers';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import React from 'react';
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
import { Form, Formik, FormikHelpers } from 'formik';
import Field from '@/components/elements/Field';
import chmodFiles from '@/api/server/files/chmodFiles';
import { ServerContext } from '@/state/server';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import useFlash from '@/plugins/useFlash';
interface FormikValues {
mode: string;
}
interface File {
file: string,
mode: string,
}
type OwnProps = RequiredModalProps & { files: File[] };
const ChmodFileModal = ({ files, ...props }: OwnProps) => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const { mutate } = useFileManagerSwr();
const { clearFlashes, clearAndAddHttpError } = useFlash();
const directory = ServerContext.useStoreState(state => state.files.directory);
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const submit = ({ mode }: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => {
clearFlashes('files');
mutate(data => data.map(f => f.name === files[0].file ? { ...f, mode: fileBitsToString(mode, !f.isFile), modeBits: mode } : f), false);
const data = files.map(f => ({ file: f.file, mode: mode }));
chmodFiles(uuid, directory, data)
.then((): Promise<any> => files.length > 0 ? mutate() : Promise.resolve())
.then(() => setSelectedFiles([]))
.catch(error => {
mutate();
setSubmitting(false);
clearAndAddHttpError({ key: 'files', error });
})
.then(() => props.onDismissed());
};
return (
<Formik onSubmit={submit} initialValues={{ mode: files.length > 1 ? '' : (files[0].mode || '') }}>
{({ isSubmitting }) => (
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
<Form css={tw`m-0`}>
<div css={tw`flex flex-wrap items-end`}>
<div css={tw`w-full sm:flex-1 sm:mr-4`}>
<Field
type={'string'}
id={'file_mode'}
name={'mode'}
label={'File Mode'}
autoFocus
/>
</div>
<div css={tw`w-full sm:w-auto mt-4 sm:mt-0`}>
<Button css={tw`w-full`}>Update</Button>
</div>
</div>
</Form>
</Modal>
)}
</Formik>
);
};
export default ChmodFileModal;

View file

@ -5,6 +5,7 @@ import {
faCopy, faCopy,
faEllipsisH, faEllipsisH,
faFileArchive, faFileArchive,
faFileCode,
faFileDownload, faFileDownload,
faLevelUpAlt, faLevelUpAlt,
faPencilAlt, faPencilAlt,
@ -30,8 +31,9 @@ import compressFiles from '@/api/server/files/compressFiles';
import decompressFiles from '@/api/server/files/decompressFiles'; import decompressFiles from '@/api/server/files/decompressFiles';
import isEqual from 'react-fast-compare'; import isEqual from 'react-fast-compare';
import ConfirmationModal from '@/components/elements/ConfirmationModal'; import ConfirmationModal from '@/components/elements/ConfirmationModal';
import ChmodFileModal from '@/components/server/files/ChmodFileModal';
type ModalType = 'rename' | 'move'; type ModalType = 'rename' | 'move' | 'chmod';
const StyledRow = styled.div<{ $danger?: boolean }>` const StyledRow = styled.div<{ $danger?: boolean }>`
${tw`p-2 flex items-center rounded`}; ${tw`p-2 flex items-center rounded`};
@ -140,14 +142,23 @@ const FileDropdownMenu = ({ file }: { file: FileObject }) => {
renderToggle={onClick => ( renderToggle={onClick => (
<div css={tw`p-3 hover:text-white`} onClick={onClick}> <div css={tw`p-3 hover:text-white`} onClick={onClick}>
<FontAwesomeIcon icon={faEllipsisH}/> <FontAwesomeIcon icon={faEllipsisH}/>
{!!modal && {modal ?
<RenameFileModal modal === 'chmod' ?
visible <ChmodFileModal
appear visible
files={[ file.name ]} appear
useMoveTerminology={modal === 'move'} files={[ { file: file.name, mode: file.modeBits } ]}
onDismissed={() => setModal(null)} onDismissed={() => setModal(null)}
/> />
:
<RenameFileModal
visible
appear
files={[ file.name ]}
useMoveTerminology={modal === 'move'}
onDismissed={() => setModal(null)}
/>
: null
} }
<SpinnerOverlay visible={showSpinner} fixed size={'large'}/> <SpinnerOverlay visible={showSpinner} fixed size={'large'}/>
</div> </div>
@ -156,6 +167,7 @@ const FileDropdownMenu = ({ file }: { file: FileObject }) => {
<Can action={'file.update'}> <Can action={'file.update'}>
<Row onClick={() => setModal('rename')} icon={faPencilAlt} title={'Rename'}/> <Row onClick={() => setModal('rename')} icon={faPencilAlt} title={'Rename'}/>
<Row onClick={() => setModal('move')} icon={faLevelUpAlt} title={'Move'}/> <Row onClick={() => setModal('move')} icon={faLevelUpAlt} title={'Move'}/>
<Row onClick={() => setModal('chmod')} icon={faFileCode} title={'Permissions'}/>
</Can> </Can>
{file.isFile && {file.isFile &&
<Can action={'file.create'}> <Can action={'file.create'}>

View file

@ -64,7 +64,11 @@ const FileObjectRow = ({ file }: { file: FileObject }) => (
> >
<SelectFileCheckbox name={file.name}/> <SelectFileCheckbox name={file.name}/>
<Clickable file={file}> <Clickable file={file}>
<div css={tw`flex-none self-center text-neutral-400 mr-4 text-lg pl-3 ml-6`}> <div css={tw`w-24 ml-6 pl-3 hidden md:block`}>
{file.mode}
</div>
<div css={tw`flex-none self-center text-neutral-400 ml-6 md:ml-0 mr-4 text-lg pl-3`}>
{file.isFile ? {file.isFile ?
<FontAwesomeIcon icon={file.isSymlink ? faFileImport : file.isArchiveType() ? faFileArchive : faFileAlt}/> <FontAwesomeIcon icon={file.isSymlink ? faFileImport : file.isArchiveType() ? faFileArchive : faFileAlt}/>
: :

View file

@ -25,7 +25,8 @@ const schema = object().shape({
const generateDirectoryData = (name: string): FileObject => ({ const generateDirectoryData = (name: string): FileObject => ({
key: `dir_${name.split('/', 1)[0] ?? name}`, key: `dir_${name.split('/', 1)[0] ?? name}`,
name: name.replace(/^(\/*)/, '').split('/', 1)[0] ?? name, name: name.replace(/^(\/*)/, '').split('/', 1)[0] ?? name,
mode: '0644', mode: 'drwxr-xr-x',
modeBits: '0755',
size: 0, size: 0,
isFile: false, isFile: false,
isSymlink: false, isSymlink: false,

View file

@ -20,3 +20,33 @@ export const randomInt = (low: number, high: number) => Math.floor(Math.random()
export const cleanDirectoryPath = (path: string) => path.replace(/(^#\/*)|(\/(\/*))|(^$)/g, '/'); export const cleanDirectoryPath = (path: string) => path.replace(/(^#\/*)|(\/(\/*))|(^$)/g, '/');
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase(); export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
export function fileBitsToString (mode: string, directory: boolean): string {
const m = parseInt(mode, 8);
let buf = '';
'dalTLDpSugct?'.split('').forEach((c, i) => {
if ((m & (1 << (32 - 1 - i))) !== 0) {
buf = buf + c;
}
});
if (buf.length === 0) {
// If the file is directory, make sure it has the directory flag.
if (directory) {
buf = 'd';
} else {
buf = '-';
}
}
'rwxrwxrwx'.split('').forEach((c, i) => {
if ((m & (1 << (9 - 1 - i))) !== 0) {
buf = buf + c;
} else {
buf = buf + '-';
}
});
return buf;
}