fix: unblock stalled and frozen uploads

A TUS PATCH rejected before its body was read left the client still streaming
a chunk. net/http gives up draining an unread request body after 256KiB and
then closes the connection, so the status we replied with was lost and the
browser saw a bare transport error instead. The client could not tell a
conflict from a network fault, retried blindly, and the upload stalled after
the first chunk — while curl, which reads the response as it writes, still
reported the status and appeared to work. Drain what is left of a rejected
chunk so the answer arrives on a connection that stays usable. The explicit
r.Body.Close() had to go with it: it ran before the drain, and the server
closes the request body itself once the handler returns.

Pressing Upload awaited a recursive listing of the whole destination subtree
before a single byte was sent, with no sign that anything was happening. On a
large destination that reads as a dead button, and the walk kept running after
the client gave up, then reported the failed write as a 500. A flat upload can
only collide with a direct child of the destination, so walk recursively only
for folder uploads, stop the walk once the request is done, and show the action
as busy while the destination is checked.

Along the way: a failed upload was credited as fully sent, so the progress bar
reported 100% when nothing had been written; the progress timer was recreated
without clearing the previous one; and the chunk size field threw on a value
with no unit and raced its own debounce on submit, so it appeared to refuse
every change.

Fixes #6006
Refs #5987
This commit is contained in:
Henrique Dias committed 2026-07-27 16:28:17 +02:00
1 parent 9dd7c7d099
commit 0b925cbca9
12 files changed
+715 -156

No files matched your search

@@ -38,6 +38,7 @@ import { useRoute } from "vue-router";
import { useLayoutStore } from "@/stores/layout";
import * as upload from "@/utils/upload";
import buttons from "@/utils/buttons";
const { t } = useI18n();
const route = useRoute();
@@ -66,9 +67,13 @@ const uploadInput = async (event: Event) => {
const path = route.path.endsWith("/") ? route.path : route.path + "/";
// Checking the destination hits the server, so show it is working rather
// than leaving the action looking inert until the upload starts.
buttons.loading("upload");
const conflict = await upload.checkConflict(uploadFiles, path);
if (conflict.length > 0) {
buttons.done("upload");
layoutStore.showHover({
prompt: "resolve-conflict",
props: {
+26 -8
View File
@@ -103,15 +103,21 @@ export const useUploadStore = defineStore("upload", () => {
}
if (isActiveUploadsOnLimit() && hasPendingUploads()) {
if (!hasActiveUploads()) {
// Update the state in a fixed time interval
if (progressInterval === null) {
// Update the state in a fixed time interval. Guard on the handle, not
// on the active count: this runs again every time the queue drains to
// zero with work still pending, and reassigning would leak the timer.
progressInterval = window.setInterval(syncState, 1000);
}
const upload = nextUpload();
let succeeded = true;
if (upload.type === "dir") {
await api.post(upload.path).catch($showError);
await api.post(upload.path).catch((err) => {
succeeded = false;
$showError(err);
});
} else {
const onUpload = (event: ProgressEvent) => {
upload.rawProgress.sentBytes = event.loaded;
@@ -119,10 +125,13 @@ export const useUploadStore = defineStore("upload", () => {
await api
.post(upload.path, upload.file!, upload.overwrite, onUpload)
.catch((err) => err.message !== "Upload aborted" && $showError(err));
.catch((err) => {
succeeded = false;
if (err.message !== "Upload aborted") $showError(err);
});
}
finishUpload(upload);
finishUpload(upload, succeeded);
}
};
@@ -135,9 +144,18 @@ export const useUploadStore = defineStore("upload", () => {
return upload;
};
const finishUpload = (upload: Upload) => {
sentBytes.value += upload.totalBytes - upload.sentBytes;
upload.sentBytes = upload.totalBytes;
const finishUpload = (upload: Upload, succeeded: boolean) => {
if (succeeded) {
sentBytes.value += upload.totalBytes - upload.sentBytes;
upload.sentBytes = upload.totalBytes;
} else {
// Credit only what actually reached the server and drop the rest from the
// total. Counting a failed upload as fully sent makes the bar report 100%
// while nothing was written, which reads as success.
sentBytes.value += upload.rawProgress.sentBytes - upload.sentBytes;
totalBytes.value -= upload.totalBytes - upload.rawProgress.sentBytes;
upload.sentBytes = upload.rawProgress.sentBytes;
}
upload.file = null;
activeUploads.value.delete(upload);
@@ -20,6 +20,24 @@ vi.mock("@/stores/layout", () => ({ useLayoutStore: vi.fn() }));
vi.mock("@/stores/upload", () => ({ useUploadStore: vi.fn() }));
vi.mock("@/utils/url", () => ({ default: {} }));
type ServerEntry = {
path: string;
name: string;
size: number;
modified: string;
isDir: boolean;
};
// The destination's direct listing, used for copy/move and for flat uploads.
function mockListing(entries: ServerEntry[]) {
vi.mocked(api.fetch).mockResolvedValue({ items: entries } as Resource);
}
// The recursive walk of the destination, used only for nested (folder) uploads.
function mockRecursiveListing(entries: ServerEntry[]) {
vi.mocked(api.fetchAll).mockResolvedValue(entries);
}
// A move/copy/drag item carries name (raw) and to (URL-encoded) but no
// fullPath - mirroring what Move.vue / Copy.vue / ListingItem.vue build.
function moveItem(name: string, dest: string, size = 12) {
@@ -40,7 +58,7 @@ describe("checkConflict", () => {
});
it("detects a conflict for a plain filename", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: "/target/file.txt",
name: "file.txt",
@@ -59,6 +77,43 @@ describe("checkConflict", () => {
expect(conflicts[0].name).toBe("/target/file.txt");
});
// Regression for #6006: pressing Upload awaited a full server-side recursive
// walk of the destination before a single byte was sent, so the UI sat there
// doing nothing on a large destination. A flat upload can only collide with a
// direct child, which the plain listing already covers.
it("does not walk the destination tree for a flat upload", async () => {
mockListing([]);
await checkConflict(
[{ name: "file.txt", size: 12, isDir: false }],
"/files/target/"
);
expect(api.fetch).toHaveBeenCalledWith("/files/target/");
expect(api.fetchAll).not.toHaveBeenCalled();
});
// ...but a folder upload lands entries below the destination, so it still
// needs the recursive listing to see them.
it("walks the destination tree for a nested upload", async () => {
mockRecursiveListing([]);
await checkConflict(
[
{
name: "file.txt",
size: 12,
isDir: false,
fullPath: "folder/file.txt",
},
],
"/files/target/"
);
expect(api.fetchAll).toHaveBeenCalledWith("/files/target/");
expect(api.fetch).not.toHaveBeenCalled();
});
// Regression for #5957: names with encodable characters (spaces, "#",
// non-ASCII) were keyed by the URL-encoded `to` value and never matched the
// server's raw path, so the conflict modal was skipped and the backend
@@ -66,7 +121,7 @@ describe("checkConflict", () => {
it.each(["my file.txt", "résumé.pdf", "a#b.txt"])(
"detects a conflict for %s (encodable characters)",
async (name) => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: `/target/${name}`,
name,
@@ -87,7 +142,7 @@ describe("checkConflict", () => {
);
it("reports no conflict when the destination has no matching name", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: "/target/other.txt",
name: "other.txt",
@@ -106,7 +161,7 @@ describe("checkConflict", () => {
});
it("detects nested conflicts for folder uploads via fullPath", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockRecursiveListing([
{
path: "/target/folder",
name: "folder",
@@ -143,7 +198,7 @@ describe("checkConflict", () => {
// fullPath) and no directory entries. Conflict detection must still find a
// nested file even though its parent folder is not in the upload list.
it("detects nested conflicts when no directory entries are present", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockRecursiveListing([
{
path: "/target/folder/deep/file.txt",
name: "file.txt",
@@ -171,24 +226,22 @@ describe("checkConflict", () => {
// Copy/move only needs the target directory's direct children. A recursive
// walk can make the UI look frozen on large destinations (regression #6005).
it("checks only the direct destination listing for copy/move", async () => {
vi.mocked(api.fetch).mockResolvedValue({
items: [
{
path: "/target/file.txt",
name: "file.txt",
size: 10,
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);
mockListing([
{
path: "/target/file.txt",
name: "file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
{
path: "/target/folder",
name: "folder",
size: 0,
modified: "2026-06-04T00:00:00Z",
isDir: true,
},
]);
const items = [
moveItem("file.txt", "/files/target/"),
@@ -209,7 +262,7 @@ describe("checkConflict", () => {
// Uploads merge into an existing folder, so the directory itself must not be
// reported - only the files inside it can conflict.
it("ignores a directory conflict for uploads (default)", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: "/target/folder",
name: "folder",
@@ -228,8 +281,8 @@ describe("checkConflict", () => {
expect(conflicts).toHaveLength(0);
});
it("returns no conflicts when the recursive listing fails", async () => {
vi.mocked(api.fetchAll).mockRejectedValue(new Error("404"));
it("returns no conflicts when the destination listing fails", async () => {
vi.mocked(api.fetch).mockRejectedValue(new Error("404"));
const conflicts = await checkConflict(
[moveItem("file.txt", "/files/target/")],
@@ -240,11 +293,11 @@ describe("checkConflict", () => {
});
// Regression for #5980: a FileBrowser server running on Windows returns
// backslash-separated paths from the recursive listing. Without normalizing
// them, the prefix strip and key lookup never match, so the conflict modal is
// skipped and the backend returns a bare 409.
// backslash-separated paths, Without normalizing them, the prefix strip and
// key lookup never match, so the conflict modal is skipped and the backend
// returns a bare 409.
it("detects a conflict for backslash-separated server paths (Windows)", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: "\\target\\file.txt",
name: "file.txt",
@@ -263,7 +316,7 @@ describe("checkConflict", () => {
});
it("detects nested conflicts for backslash-separated server paths (Windows)", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockRecursiveListing([
{
path: "\\target\\folder\\nested file.txt",
name: "nested file.txt",
@@ -293,7 +346,7 @@ describe("checkConflict", () => {
])(
"detects conflicts below an encoded destination path with %s",
async (_label, serverPath) => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: serverPath,
name: "file.txt",
@@ -314,7 +367,7 @@ describe("checkConflict", () => {
);
it("detects nested folder-upload conflicts below an encoded destination path", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockRecursiveListing([
{
path: "\\target\\My SubDir\\folder\\nested file.txt",
name: "nested file.txt",
@@ -341,7 +394,7 @@ describe("checkConflict", () => {
});
it("leaves malformed destination path segments unchanged", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockListing([
{
path: "/target/bad%ZZ/file.txt",
name: "file.txt",
@@ -360,7 +413,7 @@ describe("checkConflict", () => {
});
it("does not match files outside the decoded destination path", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
mockRecursiveListing([
{
path: "/target/My SubDir/other.txt",
name: "other.txt",
@@ -369,7 +422,7 @@ describe("checkConflict", () => {
isDir: false,
},
{
path: "/target/My Other SubDir/file.txt",
path: "/target/My Other SubDir/folder/file.txt",
name: "file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
@@ -378,7 +431,14 @@ describe("checkConflict", () => {
]);
const conflicts = await checkConflict(
[moveItem("file.txt", "/files/target/My%20SubDir/")],
[
{
name: "file.txt",
size: 12,
isDir: false,
fullPath: "folder/file.txt",
},
],
"/files/target/My%20SubDir/"
);
+21 -8
View File
@@ -30,9 +30,9 @@ type ServerConflictEntry = {
async function fetchConflictEntries(
basePath: string,
includeDirectories: boolean
recursive: boolean
): Promise<ServerConflictEntry[]> {
if (!includeDirectories) {
if (recursive) {
return await api.fetchAll(basePath);
}
@@ -40,6 +40,14 @@ async function fetchConflictEntries(
return destination.items ?? [];
}
/**
* Whether any entry lands below the destination rather than directly in it.
* Only then does conflict detection need the whole destination tree.
*/
function hasNestedEntries(files: UploadList): boolean {
return files.some((file) => conflictKey(file).includes("/"));
}
function conflictPath(entry: ServerConflictEntry): string {
return entry.path.replace(/\\/g, "/");
}
@@ -84,11 +92,14 @@ function buildConflictMap(
* server, so the caller can prompt the user to overwrite/rename/skip.
*
* Directory handling differs by action, hence `includeDirectories`:
* - Upload (false): the destination tree is fetched recursively so nested
* file uploads can be checked; existing folders are silently merged.
* - Copy/move (true): only the destination directory itself is fetched. These
* operations move flat top-level selections, so a same-named direct child is
* the only preflight conflict the backend will reject.
* - Upload (false): existing folders are silently merged, so only files count.
* - Copy/move (true): these move flat top-level selections, so a same-named
* direct child is the only preflight conflict the backend will reject.
*
* The destination tree is only walked recursively when an entry actually lands
* below the destination (a folder upload). Walking it for a flat upload made
* pressing Upload wait on a full server-side walk of the whole subtree before
* a single byte was sent, which reads as a frozen UI on a large destination.
*
* @param files - flat upload list to check
* @param basePath - server destination path (e.g. "/files/uploads/")
@@ -101,9 +112,11 @@ export async function checkConflict(
): Promise<ConflictingResource[]> {
if (files.length === 0) return [];
const recursive = !includeDirectories && hasNestedEntries(files);
let serverEntries: ServerConflictEntry[];
try {
serverEntries = await fetchConflictEntries(basePath, includeDirectories);
serverEntries = await fetchConflictEntries(basePath, recursive);
} catch {
// The destination doesn't exist yet, so nothing can conflict.
return [];
+10
View File
@@ -348,6 +348,7 @@ import { useLayoutStore } from "@/stores/layout";
import { users, files as api } from "@/api";
import { enableExec } from "@/utils/constants";
import * as upload from "@/utils/upload";
import buttons from "@/utils/buttons";
import css from "@/utils/css";
import { throttle } from "lodash-es";
import { Base64 } from "js-base64";
@@ -821,11 +822,15 @@ const drop = async (event: DragEvent) => {
}
}
// Checking the destination hits the server, so show it is working rather
// than leaving the action looking inert until the upload starts.
buttons.loading("upload");
const conflict = await upload.checkConflict(files, path);
const preselect = removePrefix(path) + (files[0].fullPath || files[0].name);
if (conflict.length > 0) {
buttons.done("upload");
layoutStore.showHover({
prompt: "resolve-conflict",
props: {
@@ -879,9 +884,14 @@ const uploadInput = async (event: Event) => {
}
const path = route.path.endsWith("/") ? route.path : route.path + "/";
// Checking the destination hits the server, so show it is working rather
// than leaving the action looking inert until the upload starts.
buttons.loading("upload");
const conflict = await upload.checkConflict(uploadFiles, path);
if (conflict.length > 0) {
buttons.done("upload");
layoutStore.showHover({
prompt: "resolve-conflict",
props: {
@@ -51,6 +51,10 @@ vi.mock("@/utils/upload", () => ({
handleFiles: harness.handleFiles,
}));
vi.mock("@/utils/css", () => ({ default: vi.fn() }));
// Reaches document.querySelector, which the node test environment lacks.
vi.mock("@/utils/buttons", () => ({
default: { loading: vi.fn(), done: vi.fn(), success: vi.fn() },
}));
vi.mock("@/components/header/HeaderBar.vue", () => ({ default: {} }));
vi.mock("@/components/header/Action.vue", () => ({ default: {} }));
vi.mock("@/components/Search.vue", () => ({ default: {} }));
+28 -5
View File
@@ -262,6 +262,7 @@ const error = ref<StatusError | null>(null);
const originalSettings = ref<ISettings | null>(null);
const settings = ref<ISettings | null>(null);
const debounceTimeout = ref<number | null>(null);
const pendingChunkSize = ref<string | null>(null);
const commandObject = ref<{
[key: string]: string[] | string;
@@ -289,13 +290,30 @@ const formattedChunkSize = computed({
clearTimeout(debounceTimeout.value);
}
pendingChunkSize.value = value;
// Set a new timeout to apply the format after a short delay
debounceTimeout.value = window.setTimeout(() => {
if (settings.value) settings.value.tus.chunkSize = parseBytes(value);
}, 1500);
debounceTimeout.value = window.setTimeout(applyChunkSize, 1500);
},
});
// applyChunkSize commits what the user typed. Saving must flush it first:
// otherwise submitting within the debounce window persists the previous value,
// and the setting appears to refuse the change.
const applyChunkSize = () => {
if (debounceTimeout.value) {
clearTimeout(debounceTimeout.value);
debounceTimeout.value = null;
}
if (pendingChunkSize.value === null) return;
if (settings.value) {
settings.value.tus.chunkSize = parseBytes(pendingChunkSize.value);
}
pendingChunkSize.value = null;
};
// Define funcs
const capitalize = (name: string, where: string | RegExp = "_") => {
if (where === "caps") where = /(?=[A-Z])/;
@@ -311,6 +329,7 @@ const capitalize = (name: string, where: string | RegExp = "_") => {
const save = async () => {
if (settings.value === null) return false;
applyChunkSize();
const newSettings: ISettings = {
...settings.value,
shell:
@@ -362,8 +381,12 @@ const parseBytes = (input: string) => {
const matches = input.match(regex);
if (matches) {
const size = parseFloat(matches[1].concat(matches[2] || ""));
let unit: keyof SettingsUnit =
matches[3].toUpperCase() as keyof SettingsUnit;
// The unit is optional: a bare number is already a count of bytes. Reading
// it unguarded throws, and the throw happens inside the debounce callback,
// so the setting is silently never updated.
let unit: keyof SettingsUnit = (
matches[3] ?? "B"
).toUpperCase() as keyof SettingsUnit;
if (!unit.endsWith("B")) {
unit += "B";
}