Files
pterodactyl-panel/resources/scripts/components/server/files/useFileManagerModel.ts
T

326 lines
12 KiB
TypeScript
Raw Normal View History

2026-10-12 15:20:31 -04:00
import { useCallback, useMemo } from 'react';
2026-10-05 18:10:18 -04:00
import { join, normalize } from 'pathe';
import { useNavigate } from '@tanstack/react-router';
import {
chmodFilesInput,
compressFilesInput,
copyFileInput,
createDirectoryInput,
decompressFileInput,
deleteFilesInput,
fileObjectKind,
2026-10-12 15:20:31 -04:00
isFileObjectArchiveType,
2026-10-05 18:10:18 -04:00
isFileObjectEditable,
renameFilesInput,
useChmodFiles,
useCompressFiles,
useCopyFile,
useCreateDirectory,
useDecompressFile,
useDeleteFiles,
useFileDownloadUrl,
useRenameFiles,
2026-10-12 15:20:31 -04:00
useServerFiles,
2026-10-05 18:10:18 -04:00
type FileObject,
} from '@/api/server/files/queries';
2026-10-12 15:20:31 -04:00
import { useCurrentServer } from '@/api/server/queries';
2026-10-05 18:10:18 -04:00
import type { FileManagerSlotData } from '@/extensions/registry';
2026-10-12 15:20:31 -04:00
import type { CorePanelDestination } from '@/sdk/navigation';
import { encodePathSegments, normalizeServerPath } from '@/helpers';
import { usePermissions } from '@/plugins/usePermissions';
import { useSelectedFiles, useServerDirectory, useServerStore } from '@/state/server';
import type { FileManagerEntry, FileManagerModel, FileUpload } from '@/views/server/files';
import { fileBreadcrumbs, fileListDestination } from './breadcrumbs';
2026-10-05 18:10:18 -04:00
import useFileUploader from './useFileUploader';
export const FILE_LIST_LIMIT = 250;
2026-10-12 15:20:31 -04:00
const noFiles: FileObject[] = [];
2026-10-05 18:10:18 -04:00
const sortFiles = (files: FileObject[]): FileObject[] => {
const sortedFiles: FileObject[] = files
.sort((a, b) => a.attributes.name.localeCompare(b.attributes.name))
2026-10-08 16:53:20 -04:00
// Directories before files.
.sort((a, b) => Number(a.attributes.is_file) - Number(b.attributes.is_file));
2026-10-05 18:10:18 -04:00
return sortedFiles.filter(
(file, index) => index === 0 || file.attributes.name !== sortedFiles[index - 1].attributes.name
);
};
function allow(granted: boolean, permission: string): void {
2026-10-08 16:53:20 -04:00
if (!granted) {
throw new Error(`This action requires the ${permission} permission.`);
}
2026-10-05 18:10:18 -04:00
}
2026-10-12 15:20:31 -04:00
/** Where opening an entry leads: its directory, the editor for a readable text file, or nowhere. */
function entryDestination(id: string, file: FileObject, path: string, openable: boolean): CorePanelDestination | null {
if (!openable) {
return null;
}
return file.attributes.is_file
? { to: '/server/$id/files/$action', params: { id, action: 'edit' }, hash: encodePathSegments(path) }
: fileListDestination(id, path);
}
/** The listing of the directory in the URL and its selection, as the file manager slots receive them. */
function useFileListing() {
const server = useCurrentServer();
const directory = useServerDirectory();
const { data, refetch, isFetching } = useServerFiles(server?.attributes.uuid ?? '', directory);
const setSelectedFiles = useServerStore((state) => state.files.setSelectedFiles);
const storedSelection = useSelectedFiles();
const files = data?.data ?? noFiles;
const listed = useMemo(() => new Set(files.slice(0, FILE_LIST_LIMIT).map((file) => file.attributes.name)), [files]);
const selection = useMemo(() => {
const present = storedSelection.filter((name) => listed.has(name));
return present.length === storedSelection.length ? storedSelection : present;
}, [storedSelection, listed]);
const select = useCallback(
(names: readonly string[]) => {
setSelectedFiles({ directory, files: [...new Set(names)].filter((name) => listed.has(name)) });
},
[listed, directory, setSelectedFiles]
);
const refresh = useCallback(async () => {
const result = await refetch();
if (result.error) {
throw result.error;
}
}, [refetch]);
const slot = useMemo<FileManagerSlotData | null>(
() =>
server
? { server, directory, files, selectedFiles: selection, isFetching, setSelectedFiles: select, refresh }
: null,
[server, directory, files, selection, isFetching, select, refresh]
);
if (!slot) {
throw new Error('The file manager renders once its server has loaded.');
}
return { slot, listed: data !== undefined };
}
function useUploads() {
const uploadsById = useServerStore((state) => state.files.uploads);
const cancelUpload = useServerStore((state) => state.files.cancelFileUpload);
const clearUploads = useServerStore((state) => state.files.clearFileUploads);
const uploads = useMemo(
() =>
Object.entries(uploadsById)
.sort(([a, first], [b, second]) => first.name.localeCompare(second.name) || a.localeCompare(b))
.map<FileUpload>(([id, { name, loaded, total }]) => ({ id, name, loaded, total })),
[uploadsById]
);
return { uploads, cancelUpload, clearUploads };
}
export default function useFileManagerModel(): FileManagerModel {
const { slot, listed } = useFileListing();
const { server, directory, selectedFiles, isFetching, setSelectedFiles, refresh } = slot;
2026-10-05 18:10:18 -04:00
const { identifier: id, uuid } = server.attributes;
const navigate = useNavigate();
const [canRead, canReadContent, canCreate, canUpdate, canDelete, canArchive] = usePermissions([
'file.read',
'file.read-content',
'file.create',
'file.update',
'file.delete',
'file.archive',
]);
const clearSelectedFiles = useServerStore((state) => state.files.clearSelectedFiles);
2026-10-12 15:20:31 -04:00
const removeSelectedFile = useServerStore((state) => state.files.removeSelectedFile);
2026-10-05 18:10:18 -04:00
const { mutateAsync: createDirectory } = useCreateDirectory();
const { mutateAsync: renameFiles } = useRenameFiles();
const { mutateAsync: deleteFiles } = useDeleteFiles();
const { mutateAsync: copyFile } = useCopyFile(directory);
const { mutateAsync: compressFiles } = useCompressFiles();
const { mutateAsync: decompressFile } = useDecompressFile();
const { mutateAsync: chmodFiles } = useChmodFiles();
const { mutateAsync: downloadUrl } = useFileDownloadUrl();
const upload = useFileUploader();
2026-10-12 15:20:31 -04:00
const { uploads, cancelUpload, clearUploads } = useUploads();
2026-10-05 18:10:18 -04:00
2026-10-12 15:20:31 -04:00
const files = useMemo(() => sortFiles(slot.files.slice(0, FILE_LIST_LIMIT)), [slot.files]);
2026-10-05 18:10:18 -04:00
const entries = useMemo(
() =>
2026-10-12 15:20:31 -04:00
files.map<FileManagerEntry>((file) => {
const path = join(directory, file.attributes.name);
const openable = file.attributes.is_file ? isFileObjectEditable(file) && canReadContent : canRead;
return {
name: file.attributes.name,
path,
kind: fileObjectKind(file),
size: file.attributes.size ?? 0,
mimetype: file.attributes.mimetype,
mode: file.attributes.mode ?? '',
modeBits: file.attributes.mode_bits ?? '',
modifiedAt: file.attributes.modified_at,
openable,
extractable: isFileObjectArchiveType(file),
destination: entryDestination(id, file, path, openable),
file,
};
}),
[files, id, directory, canRead, canReadContent]
2026-10-05 18:10:18 -04:00
);
const permissions = useMemo(
() => ({ create: canCreate, update: canUpdate, delete: canDelete, archive: canArchive }),
[canCreate, canUpdate, canDelete, canArchive]
);
const actions = useMemo<FileManagerModel['actions']>(() => {
const show = (target: string) =>
navigate({ to: '/server/$id/files', params: { id }, hash: encodePathSegments(target) });
2026-10-12 15:20:31 -04:00
const deselect = (names: readonly string[]) => {
for (const name of names) {
removeSelectedFile({ directory, name });
}
};
const rename = async (renamed: readonly { readonly from: string; readonly to: string }[]) => {
allow(canUpdate, 'file.update');
await renameFiles(
renameFilesInput(
uuid,
directory,
renamed.map(({ from, to }) => ({ from, to }))
)
);
clearSelectedFiles();
};
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return {
open: async (name) => {
const entry = entries.find((entry) => entry.name === name);
2026-10-08 16:53:20 -04:00
if (!entry?.openable) {
throw new Error(`"${name}" cannot be opened.`);
}
2026-10-05 18:10:18 -04:00
await (entry.kind === 'directory'
? show(entry.path)
: navigate({
to: '/server/$id/files/$action',
params: { id, action: 'edit' },
hash: encodePathSegments(entry.path),
}));
},
navigate: async (target) => {
await show(normalize(target.startsWith('/') ? target : join(directory, target)));
},
newFile: async () => {
allow(canCreate, 'file.create');
await navigate({
to: '/server/$id/files/$action',
params: { id, action: 'new' },
hash: encodePathSegments(directory),
});
},
select: setSelectedFiles,
refresh,
createDirectory: async (name) => {
allow(canCreate, 'file.create');
await createDirectory(createDirectoryInput(uuid, directory, name));
},
2026-10-12 15:20:31 -04:00
rename,
move: (names, to) => rename(names.map((from) => ({ from, to: names.length > 1 ? join(to, from) : to }))),
2026-10-05 18:10:18 -04:00
remove: async (names) => {
allow(canDelete, 'file.delete');
await deleteFiles(deleteFilesInput(uuid, directory, [...names]));
2026-10-12 15:20:31 -04:00
deselect(names);
2026-10-05 18:10:18 -04:00
},
copy: async (name) => {
allow(canCreate, 'file.create');
await copyFile(copyFileInput(uuid, join(directory, name)));
},
archive: async (names) => {
allow(canArchive, 'file.archive');
await compressFiles(compressFilesInput(uuid, directory, [...names]));
2026-10-12 15:20:31 -04:00
deselect(names);
2026-10-05 18:10:18 -04:00
},
extract: async (name) => {
allow(canCreate, 'file.create');
allow(canUpdate, 'file.update');
2026-10-05 18:10:18 -04:00
await decompressFile(decompressFileInput(uuid, directory, name));
},
chmod: async (changed) => {
allow(canUpdate, 'file.update');
await chmodFiles(
chmodFilesInput(
uuid,
directory,
changed.map(({ file, mode }) => ({ file, mode }))
)
);
2026-10-12 15:20:31 -04:00
clearSelectedFiles();
2026-10-05 18:10:18 -04:00
},
download: async (name) => {
window.location.assign(await downloadUrl({ uuid, file: join(directory, name) }));
},
upload: async (uploaded) => {
allow(canCreate, 'file.create');
allow(canUpdate, 'file.update');
2026-10-05 18:10:18 -04:00
await upload(uploaded);
},
2026-10-12 15:20:31 -04:00
cancelUpload,
clearUploads,
2026-10-05 18:10:18 -04:00
};
}, [
navigate,
id,
uuid,
directory,
entries,
canCreate,
canUpdate,
canDelete,
canArchive,
setSelectedFiles,
refresh,
clearSelectedFiles,
2026-10-12 15:20:31 -04:00
removeSelectedFile,
2026-10-05 18:10:18 -04:00
createDirectory,
renameFiles,
deleteFiles,
copyFile,
compressFiles,
decompressFile,
chmodFiles,
downloadUrl,
upload,
2026-10-12 15:20:31 -04:00
cancelUpload,
clearUploads,
2026-10-05 18:10:18 -04:00
]);
2026-10-12 15:20:31 -04:00
return useMemo<FileManagerModel>(
2026-10-05 18:10:18 -04:00
() => ({
directory,
2026-10-12 15:20:31 -04:00
breadcrumbs: fileBreadcrumbs(id, directory, false),
2026-10-05 18:10:18 -04:00
entries,
2026-10-12 15:20:31 -04:00
truncated: slot.files.length > FILE_LIST_LIMIT,
2026-10-05 18:10:18 -04:00
loading: !listed,
refreshing: listed && isFetching,
selection: selectedFiles,
permissions,
2026-10-12 15:20:31 -04:00
newFileDestination: {
to: '/server/$id/files/$action',
params: { id, action: 'new' },
hash: encodePathSegments(directory),
},
uploads,
resolve: (name) => normalizeServerPath(join(directory, name)),
2026-10-05 18:10:18 -04:00
actions,
2026-10-12 15:20:31 -04:00
slot,
2026-10-05 18:10:18 -04:00
}),
2026-10-12 15:20:31 -04:00
[id, directory, entries, slot, listed, isFetching, selectedFiles, permissions, uploads, actions]
2026-10-05 18:10:18 -04:00
);
}