import { davError, emptyResponse } from "@/worker/dav/http"; import type { SubjectRow } from "@/worker/db/d1/schema"; import type { AppContext } from "@/worker/types"; import { fileDavObject, resultError, type NormalizedFilesRequestPath } from "@/worker/webdav/object"; import { parseContentLength, readFileDavWriteRequest } from "@/worker/webdav/request"; export async function handleMkcol(c: AppContext, subject: SubjectRow, path: NormalizedFilesRequestPath) { const contentLength = parseContentLength(c.req.raw); if (contentLength !== null && contentLength > 0) { return davError(415, "MKCOL request bodies are not supported"); } if (contentLength === null && c.req.raw.body !== null && (await c.req.raw.arrayBuffer()).byteLength > 0) { return davError(415, "MKCOL request bodies are not supported"); } const writeRequest = readFileDavWriteRequest(c.req.raw); if (writeRequest instanceof Response) return writeRequest; const result = await fileDavObject(c.env, subject.storageId).mkcol({ subjectId: subject.id, storageId: subject.storageId, path: path.path, ifHeader: writeRequest.ifHeader, lockTokens: writeRequest.lockTokens, nowMs: writeRequest.nowMs, }); return result.ok ? emptyResponse(201) : resultError(result); }