refactor frontend warren move api

This commit is contained in:
2025-07-30 21:56:50 +02:00
parent 3b141cc7cd
commit 956c0c6f65
4 changed files with 40 additions and 30 deletions

View File

@@ -10,7 +10,6 @@ import {
deleteWarrenDirectory, deleteWarrenDirectory,
deleteWarrenFile, deleteWarrenFile,
fetchFile, fetchFile,
moveFile,
} from '~/lib/api/warrens'; } from '~/lib/api/warrens';
import type { DirectoryEntry } from '#shared/types'; import type { DirectoryEntry } from '#shared/types';

View File

@@ -303,34 +303,23 @@ export async function fetchFile(
export async function moveFile( export async function moveFile(
warrenId: string, warrenId: string,
path: string, currentPath: string,
fileName: string,
targetPath: string targetPath: string
): Promise<{ success: boolean }> { ): Promise<{ success: boolean }> {
if (!path.endsWith('/')) {
path += '/';
}
path += fileName;
if (!targetPath.endsWith('/')) {
targetPath += '/';
}
targetPath += fileName;
const { status } = await useFetch(getApiUrl(`warrens/files/mv`), { const { status } = await useFetch(getApiUrl(`warrens/files/mv`), {
method: 'POST', method: 'POST',
headers: getApiHeaders(), headers: getApiHeaders(),
body: JSON.stringify({ body: JSON.stringify({
warrenId, warrenId,
path, path: currentPath,
targetPath, targetPath: targetPath,
}), }),
}); });
if (status.value !== 'success') { if (status.value !== 'success') {
toast.error('Move', { toast.error('Move', {
id: 'MOVE_FILE_TOAST', id: 'MOVE_FILE_TOAST',
description: `Failed to move ${fileName}`, description: `Failed to move file`,
}); });
return { success: false }; return { success: false };
} }
@@ -338,8 +327,8 @@ export async function moveFile(
await refreshNuxtData('current-directory'); await refreshNuxtData('current-directory');
toast.success('Rename', { toast.success('Rename', {
id: 'RENAME_FILE_TOAST', id: 'MOVE_FILE_TOAST',
description: `Successfully moved ${fileName} to ${targetPath}`, description: `Successfully moved file`,
}); });
return { return {

View File

@@ -1,6 +1,18 @@
import { moveFile } from '~/lib/api/warrens'; import { moveFile } from '~/lib/api/warrens';
import type { DirectoryEntry } from '~/shared/types'; 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 { export function getParentPath(path: string): string {
const sliceEnd = Math.max(1, path.lastIndexOf('/')); const sliceEnd = Math.max(1, path.lastIndexOf('/'));
return path.slice(0, sliceEnd); return path.slice(0, sliceEnd);
@@ -27,23 +39,22 @@ export function onDirectoryEntryDrop(
return; return;
} }
const currentPath = joinPaths(warrenStore.current.path, fileName);
let targetPath: string; let targetPath: string;
if (isParent) { if (isParent) {
targetPath = getParentPath(warrenStore.current.path); targetPath = joinPaths(
getParentPath(warrenStore.current.path),
fileName
);
} else { } else {
targetPath = warrenStore.current.path; targetPath = joinPaths(
if (!targetPath.endsWith('/')) { warrenStore.current.path,
targetPath += '/'; entry.name,
} fileName
targetPath += entry.name; );
} }
await moveFile( await moveFile(warrenStore.current.warrenId, currentPath, targetPath);
warrenStore.current.warrenId,
warrenStore.current.path,
fileName,
targetPath
);
}; };
} }

View File

@@ -16,3 +16,14 @@ export function splitOnce(
return [str.slice(0, index), str.slice(index + 1)]; 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;
}