fix(frontend): encode uploaded filenames in delete requests (#4312)

This commit is contained in:
Ryker_Feng 2026-07-20 20:22:58 +08:00 committed by GitHub
parent cd34a1a504
commit 16377b1206
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 1 deletions

View File

@ -116,8 +116,9 @@ export async function deleteUploadedFile(
threadId: string,
filename: string,
): Promise<{ success: boolean; message: string }> {
const encodedFilename = encodeURIComponent(filename);
const response = await fetch(
`${getBackendBaseURL()}/api/threads/${threadId}/uploads/${filename}`,
`${getBackendBaseURL()}/api/threads/${threadId}/uploads/${encodedFilename}`,
{
method: "DELETE",
},

View File

@ -0,0 +1,49 @@
import { beforeEach, describe, expect, rs, test } from "@rstest/core";
rs.mock("@/core/api/fetcher", () => ({
fetch: rs.fn(),
}));
rs.mock("@/core/config", () => ({
getBackendBaseURL: () => "/backend",
}));
import { fetch as fetcher } from "@/core/api/fetcher";
import { deleteUploadedFile } from "@/core/uploads/api";
const mockedFetch = rs.mocked(fetcher);
function jsonResponse(status: number, body: unknown): Response {
return new Response(JSON.stringify(body), {
status,
statusText: status >= 400 ? "Error" : "OK",
headers: { "Content-Type": "application/json" },
});
}
beforeEach(() => {
mockedFetch.mockReset();
});
describe("uploads api", () => {
test("encodes uploaded filenames in delete request paths", async () => {
mockedFetch.mockResolvedValueOnce(
jsonResponse(200, {
success: true,
message: "Deleted report#1?.txt",
}),
);
await expect(
deleteUploadedFile("thread-1", "report#1?.txt"),
).resolves.toEqual({
success: true,
message: "Deleted report#1?.txt",
});
expect(mockedFetch).toHaveBeenCalledWith(
"/backend/api/threads/thread-1/uploads/report%231%3F.txt",
{ method: "DELETE" },
);
});
});