41 lines
1.8 KiB
TypeScript
41 lines
1.8 KiB
TypeScript
// P4 Files (AK-P4a) §3 — chunked-upload chunk endpoint (resource route, action
|
|
// only). The browser uploader PUTs/POSTs raw chunk bytes; we store them as a
|
|
// temp object and track the FileUploadChunk. Gated: auth -> module -> files.write.
|
|
|
|
import type { ActionFunctionArgs } from "react-router";
|
|
import { fileActionErrorResponse, loadFilesContext, requireFilesPermission } from "~/utils/integrations/fileAccess.server";
|
|
import { FILE_UPLOAD_CHUNK_SIZE_BYTES } from "@toolctrl/view-models";
|
|
import { storeUploadChunk } from "~/utils/integrations/fileService.server";
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
try {
|
|
if (request.method !== "POST" && request.method !== "PUT") {
|
|
return new Response("Method Not Allowed", { status: 405 });
|
|
}
|
|
const ctx = requireFilesPermission(await loadFilesContext({ request, params, requireModule: true }), "files.write");
|
|
const uploadId = params.uploadId ?? "";
|
|
const chunkIndex = Number.parseInt(params.index ?? "", 10);
|
|
if (!uploadId || !Number.isInteger(chunkIndex)) {
|
|
return Response.json({ error: "files.error.invalidUploadRequest" }, { status: 400 });
|
|
}
|
|
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
if (bytes.byteLength === 0) {
|
|
return Response.json({ error: "files.error.emptyChunk" }, { status: 400 });
|
|
}
|
|
// Defensive upper bound so a malformed client cannot stream an oversized part.
|
|
if (bytes.byteLength > FILE_UPLOAD_CHUNK_SIZE_BYTES) {
|
|
return Response.json({ error: "files.error.chunkTooLarge" }, { status: 400 });
|
|
}
|
|
const result = await storeUploadChunk({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
uploadId,
|
|
chunkIndex,
|
|
bytes,
|
|
});
|
|
return Response.json(result);
|
|
} catch (error) {
|
|
return fileActionErrorResponse(error);
|
|
}
|
|
};
|