fix: avoid recursive conflict checks for copy and move (#6009)
This commit is contained in:
3 files changed
+97
-44
No files matched your search
@@ -71,7 +71,9 @@ const event = {
|
|||||||
describe("copy and move conflict prompts", () => {
|
describe("copy and move conflict prompts", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
vi.mocked(checkConflict).mockResolvedValue(conflict);
|
vi.mocked(checkConflict).mockResolvedValue(
|
||||||
|
conflict as ConflictingResource[]
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("waits for copy conflict detection before calling the copy API", async () => {
|
it("waits for copy conflict detection before calling the copy API", async () => {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { files as api } from "@/api";
|
|||||||
|
|
||||||
vi.mock("@/api", () => ({
|
vi.mock("@/api", () => ({
|
||||||
files: {
|
files: {
|
||||||
|
fetch: vi.fn(),
|
||||||
fetchAll: vi.fn(),
|
fetchAll: vi.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
@@ -19,8 +20,8 @@ vi.mock("@/stores/layout", () => ({ useLayoutStore: vi.fn() }));
|
|||||||
vi.mock("@/stores/upload", () => ({ useUploadStore: vi.fn() }));
|
vi.mock("@/stores/upload", () => ({ useUploadStore: vi.fn() }));
|
||||||
vi.mock("@/utils/url", () => ({ default: {} }));
|
vi.mock("@/utils/url", () => ({ default: {} }));
|
||||||
|
|
||||||
// A move/copy/drag item carries `name` (raw) and `to` (URL-encoded) but no
|
// A move/copy/drag item carries name (raw) and to (URL-encoded) but no
|
||||||
// `fullPath` — mirroring what Move.vue / Copy.vue / ListingItem.vue build.
|
// fullPath - mirroring what Move.vue / Copy.vue / ListingItem.vue build.
|
||||||
function moveItem(name: string, dest: string, size = 12) {
|
function moveItem(name: string, dest: string, size = 12) {
|
||||||
return {
|
return {
|
||||||
from: `/files/source/${encodeURIComponent(name)}`,
|
from: `/files/source/${encodeURIComponent(name)}`,
|
||||||
@@ -167,29 +168,46 @@ describe("checkConflict", () => {
|
|||||||
expect(conflicts[0].name).toBe("/target/folder/deep/file.txt");
|
expect(conflicts[0].name).toBe("/target/folder/deep/file.txt");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Copy/move stats the whole destination, so a same-named directory is a
|
// Copy/move only needs the target directory's direct children. A recursive
|
||||||
// conflict in its own right (regression for the directory case of #5957).
|
// walk can make the UI look frozen on large destinations (regression #6005).
|
||||||
it("reports a directory conflict for copy/move (includeDirectories)", async () => {
|
it("checks only the direct destination listing for copy/move", async () => {
|
||||||
vi.mocked(api.fetchAll).mockResolvedValue([
|
vi.mocked(api.fetch).mockResolvedValue({
|
||||||
{
|
items: [
|
||||||
path: "/target/folder",
|
{
|
||||||
name: "folder",
|
path: "/target/file.txt",
|
||||||
size: 0,
|
name: "file.txt",
|
||||||
modified: "2026-06-04T00:00:00Z",
|
size: 10,
|
||||||
isDir: true,
|
modified: "2026-06-04T00:00:00Z",
|
||||||
},
|
isDir: false,
|
||||||
]);
|
},
|
||||||
|
{
|
||||||
|
path: "/target/folder",
|
||||||
|
name: "folder",
|
||||||
|
size: 0,
|
||||||
|
modified: "2026-06-04T00:00:00Z",
|
||||||
|
isDir: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as Resource);
|
||||||
|
|
||||||
const items = [{ ...moveItem("folder", "/files/target/", 0), isDir: true }];
|
const items = [
|
||||||
|
moveItem("file.txt", "/files/target/"),
|
||||||
|
{ ...moveItem("folder", "/files/target/", 0), isDir: true },
|
||||||
|
];
|
||||||
|
|
||||||
const conflicts = await checkConflict(items, "/files/target/", true);
|
const conflicts = await checkConflict(items, "/files/target/", true);
|
||||||
|
|
||||||
expect(conflicts).toHaveLength(1);
|
expect(api.fetch).toHaveBeenCalledWith("/files/target/");
|
||||||
expect(conflicts[0].name).toBe("/target/folder");
|
expect(api.fetchAll).not.toHaveBeenCalled();
|
||||||
|
expect(conflicts).toHaveLength(2);
|
||||||
|
expect(conflicts.map((conflict) => conflict.name)).toEqual([
|
||||||
|
"/target/file.txt",
|
||||||
|
"/target/folder",
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Uploads merge into an existing folder, so the directory itself must not be
|
// Uploads merge into an existing folder, so the directory itself must not be
|
||||||
// reported — only the files inside it can conflict.
|
// reported - only the files inside it can conflict.
|
||||||
it("ignores a directory conflict for uploads (default)", async () => {
|
it("ignores a directory conflict for uploads (default)", async () => {
|
||||||
vi.mocked(api.fetchAll).mockResolvedValue([
|
vi.mocked(api.fetchAll).mockResolvedValue([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,20 +21,61 @@ function conflictKey(item: UploadEntry): string {
|
|||||||
return (item.fullPath || item.name).replace(/^\/+/, "");
|
return (item.fullPath || item.name).replace(/^\/+/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ServerConflictEntry = {
|
||||||
|
path: string;
|
||||||
|
name: string;
|
||||||
|
size: number;
|
||||||
|
modified: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function fetchConflictEntries(
|
||||||
|
basePath: string,
|
||||||
|
includeDirectories: boolean
|
||||||
|
): Promise<ServerConflictEntry[]> {
|
||||||
|
if (!includeDirectories) {
|
||||||
|
return await api.fetchAll(basePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const destination = await api.fetch(basePath);
|
||||||
|
return destination.items ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function conflictPath(entry: ServerConflictEntry): string {
|
||||||
|
return entry.path.replace(/\\/g, "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildConflictMap(
|
||||||
|
serverEntries: ServerConflictEntry[],
|
||||||
|
basePath: string,
|
||||||
|
includeDirectories: boolean
|
||||||
|
): Map<string, ServerConflictEntry> {
|
||||||
|
const serverMap = new Map<string, ServerConflictEntry>();
|
||||||
|
const normBase = removePrefix(basePath).replace(/\/+$/, "");
|
||||||
|
for (const entry of serverEntries) {
|
||||||
|
// A Windows server may return OS-native backslash separators; normalize to
|
||||||
|
// forward slashes so the prefix strip and key lookup line up.
|
||||||
|
const path = conflictPath(entry);
|
||||||
|
const key = includeDirectories
|
||||||
|
? entry.name
|
||||||
|
: path.startsWith(normBase)
|
||||||
|
? path.slice(normBase.length)
|
||||||
|
: path;
|
||||||
|
serverMap.set(key.replace(/^\/+/, ""), entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverMap;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the entries from `files` that already exist under `basePath` on the
|
* Return the entries from `files` that already exist under `basePath` on the
|
||||||
* server, so the caller can prompt the user to overwrite/rename/skip.
|
* server, so the caller can prompt the user to overwrite/rename/skip.
|
||||||
*
|
*
|
||||||
* The whole destination tree is fetched once and indexed by path relative to
|
|
||||||
* the destination, then every entry is looked up directly — no need to mirror
|
|
||||||
* the upload's folder structure.
|
|
||||||
*
|
|
||||||
* Directory handling differs by action, hence `includeDirectories`:
|
* Directory handling differs by action, hence `includeDirectories`:
|
||||||
* - Upload (false): an existing folder is silently merged, so only the
|
* - Upload (false): the destination tree is fetched recursively so nested
|
||||||
* individual files inside it can conflict.
|
* file uploads can be checked; existing folders are silently merged.
|
||||||
* - Copy/move (true): the server stats the destination and rejects it whole if
|
* - Copy/move (true): only the destination directory itself is fetched. These
|
||||||
* a same-named entry exists, so the directory itself is a conflict. The list
|
* operations move flat top-level selections, so a same-named direct child is
|
||||||
* only holds the top-level items being moved, so each is reported once.
|
* the only preflight conflict the backend will reject.
|
||||||
*
|
*
|
||||||
* @param files - flat upload list to check
|
* @param files - flat upload list to check
|
||||||
* @param basePath - server destination path (e.g. "/files/uploads/")
|
* @param basePath - server destination path (e.g. "/files/uploads/")
|
||||||
@@ -47,27 +88,19 @@ export async function checkConflict(
|
|||||||
): Promise<ConflictingResource[]> {
|
): Promise<ConflictingResource[]> {
|
||||||
if (files.length === 0) return [];
|
if (files.length === 0) return [];
|
||||||
|
|
||||||
let serverEntries: RecursiveEntry[];
|
let serverEntries: ServerConflictEntry[];
|
||||||
try {
|
try {
|
||||||
// Single API call: fetch the entire server tree under basePath.
|
serverEntries = await fetchConflictEntries(basePath, includeDirectories);
|
||||||
serverEntries = await api.fetchAll(basePath);
|
|
||||||
} catch {
|
} catch {
|
||||||
// The destination doesn't exist yet, so nothing can conflict.
|
// The destination doesn't exist yet, so nothing can conflict.
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// The server returns paths absolute within the user's scope
|
const serverMap = buildConflictMap(
|
||||||
// (e.g. "/uploads/sub/file.txt"). Strip the basePath prefix so the keys line
|
serverEntries,
|
||||||
// up with each entry's conflictKey, which is relative to the destination.
|
basePath,
|
||||||
const normBase = removePrefix(basePath).replace(/\/+$/, "");
|
includeDirectories
|
||||||
const serverMap = new Map<string, RecursiveEntry>();
|
);
|
||||||
for (const entry of serverEntries) {
|
|
||||||
// A Windows server may return OS-native backslash separators; normalize to
|
|
||||||
// forward slashes so the prefix strip and key lookup line up.
|
|
||||||
const path = entry.path.replace(/\\/g, "/");
|
|
||||||
const rel = path.startsWith(normBase) ? path.slice(normBase.length) : path;
|
|
||||||
serverMap.set(rel.replace(/^\/+/, ""), entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
const conflicts: ConflictingResource[] = [];
|
const conflicts: ConflictingResource[] = [];
|
||||||
files.forEach((file, index) => {
|
files.forEach((file, index) => {
|
||||||
@@ -78,7 +111,7 @@ export async function checkConflict(
|
|||||||
|
|
||||||
conflicts.push({
|
conflicts.push({
|
||||||
index,
|
index,
|
||||||
name: server.path,
|
name: conflictPath(server),
|
||||||
origin: { lastModified: file.file?.lastModified, size: file.size },
|
origin: { lastModified: file.file?.lastModified, size: file.size },
|
||||||
dest: { lastModified: server.modified, size: server.size },
|
dest: { lastModified: server.modified, size: server.size },
|
||||||
checked: ["origin"],
|
checked: ["origin"],
|
||||||
|
|||||||
Reference in new issue
Block a user