diff --git a/resources/scripts/api/server/files.ts b/resources/scripts/api/server/files.ts index b86c6d510..cb71efcf2 100644 --- a/resources/scripts/api/server/files.ts +++ b/resources/scripts/api/server/files.ts @@ -1,5 +1,4 @@ -import http from '@/api/http'; -import { rawDataToFileObject } from '@/api/transformers'; +import http, { FractalResponseData } from '@/api/http'; export interface FileObject { key: string; @@ -16,6 +15,49 @@ export interface FileObject { isEditable: () => boolean; } +const rawDataToFileObject = (data: FractalResponseData): FileObject => ({ + key: `${data.attributes.is_file ? 'file' : 'dir'}_${data.attributes.name}`, + name: data.attributes.name, + mode: data.attributes.mode, + modeBits: data.attributes.mode_bits, + size: Number(data.attributes.size), + isFile: data.attributes.is_file, + isSymlink: data.attributes.is_symlink, + mimetype: data.attributes.mimetype, + createdAt: new Date(data.attributes.created_at), + modifiedAt: new Date(data.attributes.modified_at), + + isArchiveType: function () { + return this.isFile && [ + 'application/vnd.rar', // .rar + 'application/x-rar-compressed', // .rar (2) + 'application/x-tar', // .tar + 'application/x-br', // .tar.br + 'application/x-bzip2', // .tar.bz2, .bz2 + 'application/gzip', // .tar.gz, .gz + 'application/x-gzip', + 'application/x-lzip', // .tar.lz4, .lz4 (not sure if this mime type is correct) + 'application/x-sz', // .tar.sz, .sz (not sure if this mime type is correct) + 'application/x-xz', // .tar.xz, .xz + 'application/zstd', // .tar.zst, .zst + 'application/zip', // .zip + ].indexOf(this.mimetype) >= 0; + }, + + isEditable: function () { + if (this.isArchiveType() || !this.isFile) return false; + + const matches = [ + 'application/jar', + 'application/octet-stream', + 'inode/directory', + /^image\//, + ]; + + return matches.every(m => !this.mimetype.match(m)); + }, +}); + const chmodFiles = (uuid: string, directory: string, files: { file: string; mode: string }[]): Promise => { return new Promise((resolve, reject) => { http.post(`/api/client/servers/${uuid}/files/chmod`, { root: directory, files }) diff --git a/resources/scripts/api/transformers.ts b/resources/scripts/api/transformers.ts deleted file mode 100644 index 21e151fad..000000000 --- a/resources/scripts/api/transformers.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { FractalResponseData } from '@/api/http'; -import { FileObject } from '@/api/server/files/loadDirectory'; -import { Transformers } from '@definitions/user'; - -export const rawDataToFileObject = (data: FractalResponseData): FileObject => ({ - key: `${data.attributes.is_file ? 'file' : 'dir'}_${data.attributes.name}`, - name: data.attributes.name, - mode: data.attributes.mode, - modeBits: data.attributes.mode_bits, - size: Number(data.attributes.size), - isFile: data.attributes.is_file, - isSymlink: data.attributes.is_symlink, - mimetype: data.attributes.mimetype, - createdAt: new Date(data.attributes.created_at), - modifiedAt: new Date(data.attributes.modified_at), - - isArchiveType: function () { - return this.isFile && [ - 'application/vnd.rar', // .rar - 'application/x-rar-compressed', // .rar (2) - 'application/x-tar', // .tar - 'application/x-br', // .tar.br - 'application/x-bzip2', // .tar.bz2, .bz2 - 'application/gzip', // .tar.gz, .gz - 'application/x-gzip', - 'application/x-lzip', // .tar.lz4, .lz4 (not sure if this mime type is correct) - 'application/x-sz', // .tar.sz, .sz (not sure if this mime type is correct) - 'application/x-xz', // .tar.xz, .xz - 'application/zstd', // .tar.zst, .zst - 'application/zip', // .zip - ].indexOf(this.mimetype) >= 0; - }, - - isEditable: function () { - if (this.isArchiveType() || !this.isFile) return false; - - const matches = [ - 'application/jar', - 'application/octet-stream', - 'inode/directory', - /^image\//, - ]; - - return matches.every(m => !this.mimetype.match(m)); - }, -}); - -export const rawDataToServerAllocation = Transformers.toServerAllocation; diff --git a/resources/scripts/components/server/files/FileDropdownMenu.tsx b/resources/scripts/components/server/files/FileDropdownMenu.tsx index 94115827e..edd6eb624 100644 --- a/resources/scripts/components/server/files/FileDropdownMenu.tsx +++ b/resources/scripts/components/server/files/FileDropdownMenu.tsx @@ -15,12 +15,11 @@ import { import RenameFileModal from '@/components/server/files/RenameFileModal'; import { ServerContext } from '@/state/server'; import { join } from 'path'; -import { compressFiles, copyFiles, decompressFiles, deleteFiles, getFileDownloadUrl } from '@/api/server/files'; +import { compressFiles, copyFiles, decompressFiles, deleteFiles, getFileDownloadUrl, FileObject } from '@/api/server/files'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import Can from '@/components/elements/Can'; import useFlash from '@/plugins/useFlash'; import tw, { styled } from 'twin.macro'; -import { FileObject } from '@/api/server/files/loadDirectory'; import useFileManagerSwr from '@/plugins/useFileManagerSwr'; import DropdownMenu from '@/components/elements/DropdownMenu'; import useEventListener from '@/plugins/useEventListener'; diff --git a/resources/scripts/components/server/files/FileManagerContainer.tsx b/resources/scripts/components/server/files/FileManagerContainer.tsx index 4891f3a27..292154ec6 100644 --- a/resources/scripts/components/server/files/FileManagerContainer.tsx +++ b/resources/scripts/components/server/files/FileManagerContainer.tsx @@ -4,7 +4,7 @@ import { CSSTransition } from 'react-transition-group'; import Spinner from '@/components/elements/Spinner'; import FileObjectRow from '@/components/server/files/FileObjectRow'; import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs'; -import { FileObject } from '@/api/server/files/loadDirectory'; +import { FileObject } from '@/api/server/files'; import NewDirectoryButton from '@/components/server/files/NewDirectoryButton'; import { NavLink, useLocation } from 'react-router-dom'; import Can from '@/components/elements/Can'; diff --git a/resources/scripts/components/server/files/FileObjectRow.tsx b/resources/scripts/components/server/files/FileObjectRow.tsx index 7c376d672..3c58a47a8 100644 --- a/resources/scripts/components/server/files/FileObjectRow.tsx +++ b/resources/scripts/components/server/files/FileObjectRow.tsx @@ -3,7 +3,7 @@ import { faFileAlt, faFileArchive, faFileImport, faFolder } from '@fortawesome/f import { bytesToHuman, encodePathSegments } from '@/helpers'; import { differenceInHours, format, formatDistanceToNow } from 'date-fns'; import React, { memo } from 'react'; -import { FileObject } from '@/api/server/files/loadDirectory'; +import { FileObject } from '@/api/server/files'; import FileDropdownMenu from '@/components/server/files/FileDropdownMenu'; import { ServerContext } from '@/state/server'; import { NavLink, useRouteMatch } from 'react-router-dom'; @@ -14,7 +14,7 @@ import { usePermissions } from '@/plugins/usePermissions'; import { join } from 'path'; const Row = styled.div` - ${tw`flex items-center w-full h-10 px-3 rounded-sm cursor-pointer bg-neutral-700 hover:bg-neutral-600 mb-px`}; + ${tw`flex items-center w-full h-10 px-3 rounded-sm cursor-pointer bg-neutral-700 hover:bg-neutral-600 mb-px`}; `; const Clickable: React.FC<{ file: FileObject }> = memo(({ file, children }) => { @@ -70,7 +70,10 @@ const FileObjectRow = ({ file }: { file: FileObject }) => (
- + {Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48 ? format(file.modifiedAt, 'MMM do, yyyy h:mma') :