diff --git a/app/Transformers/Daemon/FileObjectTransformer.php b/app/Transformers/Daemon/FileObjectTransformer.php index b2c1deed3..f19d9028c 100644 --- a/app/Transformers/Daemon/FileObjectTransformer.php +++ b/app/Transformers/Daemon/FileObjectTransformer.php @@ -23,7 +23,7 @@ class FileObjectTransformer extends BaseDaemonTransformer public function transform(array $item) { return [ - 'name' => rawurlencode(Arr::get($item, 'name')), + 'name' => Arr::get($item, 'name'), 'mode' => Arr::get($item, 'mode'), 'mode_bits' => Arr::get($item, 'mode_bits'), 'size' => Arr::get($item, 'size'), diff --git a/resources/scripts/api/server/files/loadDirectory.ts b/resources/scripts/api/server/files/loadDirectory.ts index d53a2634f..ba137e285 100644 --- a/resources/scripts/api/server/files/loadDirectory.ts +++ b/resources/scripts/api/server/files/loadDirectory.ts @@ -18,8 +18,6 @@ export interface FileObject { export default async (uuid: string, directory?: string): Promise => { const { data } = await http.get(`/api/client/servers/${uuid}/files/list`, { - // At this point the directory is still encoded so we need to decode it since axios - // will automatically re-encode this value before sending it along in the request. params: { directory: directory ?? '/' }, }); diff --git a/resources/scripts/components/server/files/FileEditContainer.tsx b/resources/scripts/components/server/files/FileEditContainer.tsx index f26baf8b0..670870122 100644 --- a/resources/scripts/components/server/files/FileEditContainer.tsx +++ b/resources/scripts/components/server/files/FileEditContainer.tsx @@ -17,6 +17,8 @@ import modes from '@/modes'; import useFlash from '@/plugins/useFlash'; import { ServerContext } from '@/state/server'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; +import { encodePathSegments, hashToPath } from '@/helpers'; +import { dirname } from 'path'; const LazyCodemirrorEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/CodemirrorEditor')); @@ -43,8 +45,9 @@ export default () => { setError(''); setLoading(true); - setDirectory(hash.replace(/^#/, '').split('/').filter(v => !!v).slice(0, -1).join('/')); - getFileContents(uuid, hash.replace(/^#/, '')) + const path = hashToPath(hash); + setDirectory(dirname(path)); + getFileContents(uuid, path) .then(setContent) .catch(error => { console.error(error); @@ -61,10 +64,10 @@ export default () => { setLoading(true); clearFlashes('files:view'); fetchFileContent() - .then(content => saveFileContents(uuid, name || decodeURI(hash.replace(/^#/, '')), content)) + .then(content => saveFileContents(uuid, name || hashToPath(hash), content)) .then(() => { if (name) { - history.push(`/server/${id}/files/edit#/${name}`); + history.push(`/server/${id}/files/edit#/${encodePathSegments(name)}`); return; } diff --git a/resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx b/resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx index 7353edadf..0a4f46910 100644 --- a/resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx +++ b/resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; import { NavLink, useLocation } from 'react-router-dom'; -import { cleanDirectoryPath } from '@/helpers'; +import { encodePathSegments, hashToPath } from '@/helpers'; import tw from 'twin.macro'; interface Props { @@ -17,22 +17,10 @@ export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => { const { hash } = useLocation(); useEffect(() => { - let pathHash = cleanDirectoryPath(hash); - try { - pathHash = decodeURI(pathHash); - } catch (e) { - console.warn('Error decoding URL parts in hash:', e); - } + const path = hashToPath(hash); if (withinFileEditor && !isNewFile) { - let name = pathHash.split('/').pop() || null; - if (name) { - try { - name = decodeURIComponent(name); - } catch (e) { - console.warn('Error decoding filename:', e); - } - } + const name = path.split('/').pop() || null; setFile(name); } }, [ withinFileEditor, isNewFile, hash ]); @@ -62,14 +50,14 @@ export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => { crumb.path ? - {decodeURIComponent(crumb.name)} + {crumb.name} / : - {decodeURIComponent(crumb.name)} + {crumb.name} )) } {file && diff --git a/resources/scripts/components/server/files/FileManagerContainer.tsx b/resources/scripts/components/server/files/FileManagerContainer.tsx index 864d4746a..fe3460fbf 100644 --- a/resources/scripts/components/server/files/FileManagerContainer.tsx +++ b/resources/scripts/components/server/files/FileManagerContainer.tsx @@ -19,6 +19,7 @@ import ServerContentBlock from '@/components/elements/ServerContentBlock'; import { useStoreActions } from '@/state/hooks'; import ErrorBoundary from '@/components/elements/ErrorBoundary'; import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox'; +import { hashToPath } from '@/helpers'; const sortFiles = (files: FileObject[]): FileObject[] => { return files.sort((a, b) => a.name.localeCompare(b.name)) @@ -39,7 +40,7 @@ export default () => { useEffect(() => { clearFlashes('files'); setSelectedFiles([]); - setDirectory(hash.length > 0 ? hash : '/'); + setDirectory(hashToPath(hash)); }, [ hash ]); useEffect(() => { diff --git a/resources/scripts/components/server/files/FileObjectRow.tsx b/resources/scripts/components/server/files/FileObjectRow.tsx index fd18bd7ba..628bdc4ee 100644 --- a/resources/scripts/components/server/files/FileObjectRow.tsx +++ b/resources/scripts/components/server/files/FileObjectRow.tsx @@ -1,17 +1,18 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faFileAlt, faFileArchive, faFileImport, faFolder } from '@fortawesome/free-solid-svg-icons'; -import { bytesToHuman, cleanDirectoryPath } from '@/helpers'; +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 FileDropdownMenu from '@/components/server/files/FileDropdownMenu'; import { ServerContext } from '@/state/server'; -import { NavLink, useHistory, useRouteMatch } from 'react-router-dom'; +import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import isEqual from 'react-fast-compare'; import styled from 'styled-components/macro'; import SelectFileCheckbox from '@/components/server/files/SelectFileCheckbox'; import { usePermissions } from '@/plugins/usePermissions'; +import { join } from 'path'; const Row = styled.div` ${tw`flex bg-neutral-700 rounded-sm mb-px text-sm hover:text-neutral-100 cursor-pointer items-center no-underline hover:bg-neutral-600`}; @@ -21,23 +22,8 @@ const Clickable: React.FC<{ file: FileObject }> = memo(({ file, children }) => { const [ canReadContents ] = usePermissions([ 'file.read-content' ]); const directory = ServerContext.useStoreState(state => state.files.directory); - const history = useHistory(); const match = useRouteMatch(); - const destination = cleanDirectoryPath(`${directory}/${file.name}`).split('/').join('/'); - - const onRowClick = (e: React.MouseEvent) => { - // Don't rely on the onClick to work with the generated URL. Because of the way this - // component re-renders you'll get redirected into a nested directory structure since - // it'll cause the directory variable to update right away when you click. - // - // Just trust me future me, leave this be. - if (!file.isFile) { - e.preventDefault(); - history.push(`#${destination}`); - } - }; - return ( (!canReadContents || (file.isFile && !file.isEditable())) ?
@@ -45,9 +31,8 @@ const Clickable: React.FC<{ file: FileObject }> = memo(({ file, children }) => {
: {children} @@ -72,7 +57,7 @@ const FileObjectRow = ({ file }: { file: FileObject }) => ( }
- {decodeURIComponent(file.name)} + {file.name}
{file.isFile &&