From 956c0c6f6596e29f027b46051426a104205e2cd7 Mon Sep 17 00:00:00 2001 From: 409 <409dev@protonmail.com> Date: Wed, 30 Jul 2025 21:56:50 +0200 Subject: [PATCH] refactor frontend warren move api --- frontend/components/DirectoryEntry.vue | 1 - frontend/lib/api/warrens.ts | 23 +++++------------ frontend/utils/files.ts | 35 +++++++++++++++++--------- frontend/utils/index.ts | 11 ++++++++ 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/frontend/components/DirectoryEntry.vue b/frontend/components/DirectoryEntry.vue index 413518f..cc62c66 100644 --- a/frontend/components/DirectoryEntry.vue +++ b/frontend/components/DirectoryEntry.vue @@ -10,7 +10,6 @@ import { deleteWarrenDirectory, deleteWarrenFile, fetchFile, - moveFile, } from '~/lib/api/warrens'; import type { DirectoryEntry } from '#shared/types'; diff --git a/frontend/lib/api/warrens.ts b/frontend/lib/api/warrens.ts index 6a69e89..818c734 100644 --- a/frontend/lib/api/warrens.ts +++ b/frontend/lib/api/warrens.ts @@ -303,34 +303,23 @@ export async function fetchFile( export async function moveFile( warrenId: string, - path: string, - fileName: string, + currentPath: string, targetPath: string ): Promise<{ success: boolean }> { - if (!path.endsWith('/')) { - path += '/'; - } - path += fileName; - - if (!targetPath.endsWith('/')) { - targetPath += '/'; - } - targetPath += fileName; - const { status } = await useFetch(getApiUrl(`warrens/files/mv`), { method: 'POST', headers: getApiHeaders(), body: JSON.stringify({ warrenId, - path, - targetPath, + path: currentPath, + targetPath: targetPath, }), }); if (status.value !== 'success') { toast.error('Move', { id: 'MOVE_FILE_TOAST', - description: `Failed to move ${fileName}`, + description: `Failed to move file`, }); return { success: false }; } @@ -338,8 +327,8 @@ export async function moveFile( await refreshNuxtData('current-directory'); toast.success('Rename', { - id: 'RENAME_FILE_TOAST', - description: `Successfully moved ${fileName} to ${targetPath}`, + id: 'MOVE_FILE_TOAST', + description: `Successfully moved file`, }); return { diff --git a/frontend/utils/files.ts b/frontend/utils/files.ts index f7da02a..0818192 100644 --- a/frontend/utils/files.ts +++ b/frontend/utils/files.ts @@ -1,6 +1,18 @@ import { moveFile } from '~/lib/api/warrens'; import type { DirectoryEntry } from '~/shared/types'; +export function joinPaths(path: string, ...other: string[]): string { + for (const p of other) { + if (!path.endsWith('/')) { + path += '/'; + } + + path += trim(p, '/'); + } + + return path; +} + export function getParentPath(path: string): string { const sliceEnd = Math.max(1, path.lastIndexOf('/')); return path.slice(0, sliceEnd); @@ -27,23 +39,22 @@ export function onDirectoryEntryDrop( return; } + const currentPath = joinPaths(warrenStore.current.path, fileName); let targetPath: string; if (isParent) { - targetPath = getParentPath(warrenStore.current.path); + targetPath = joinPaths( + getParentPath(warrenStore.current.path), + fileName + ); } else { - targetPath = warrenStore.current.path; - if (!targetPath.endsWith('/')) { - targetPath += '/'; - } - targetPath += entry.name; + targetPath = joinPaths( + warrenStore.current.path, + entry.name, + fileName + ); } - await moveFile( - warrenStore.current.warrenId, - warrenStore.current.path, - fileName, - targetPath - ); + await moveFile(warrenStore.current.warrenId, currentPath, targetPath); }; } diff --git a/frontend/utils/index.ts b/frontend/utils/index.ts index 70bf3f9..c43151b 100644 --- a/frontend/utils/index.ts +++ b/frontend/utils/index.ts @@ -16,3 +16,14 @@ export function splitOnce( return [str.slice(0, index), str.slice(index + 1)]; } + +export function trim(str: string, char: string) { + let start = 0; + let end = str.length; + + while (start < end && str[start] === char) ++start; + + while (end > start && str[end - 1] === char) --end; + + return start > 0 || end < str.length ? str.substring(start, end) : str; +}