import type { DavPatAuth } from "@/worker/auth/basic"; import { davError, emptyResponse, methodNotAllowed } from "@/worker/dav/http"; import { normalizeFilesPath } from "@/worker/dav/paths"; import type { SubjectRow } from "@/worker/db/d1/schema"; import type { AppContext } from "@/worker/types"; import { hasFilesScope, requiresFileWrite } from "@/worker/webdav/access"; import { FILES_DAV_ALLOW, FILES_DAV_OPTIONS_HEADERS } from "@/worker/webdav/constants"; import { handleCopyMove } from "@/worker/webdav/copy-move"; import { handleDelete } from "@/worker/webdav/delete"; import { handleGetHead } from "@/worker/webdav/get-head"; import { handleLock, handleUnlock } from "@/worker/webdav/lock"; import { handleMkcol } from "@/worker/webdav/mkcol"; import { handlePropfind } from "@/worker/webdav/propfind"; import { handleProppatch } from "@/worker/webdav/proppatch"; import { handlePut } from "@/worker/webdav/put"; export { FILES_DAV_OPTIONS_HEADERS }; export async function handleWebDavFiles(c: AppContext, subject: SubjectRow, auth: DavPatAuth): Promise { if ( requiresFileWrite(c.req.method) ? !hasFilesScope(auth.scopes, "dav:files:write") : !hasFilesScope(auth.scopes, "dav:files:read") ) { return davError(403, "PAT scope does not allow this method"); } const path = normalizeFilesPath(new URL(c.req.url).pathname); if (!path.ok) return davError(path.status, path.message); switch (c.req.method) { case "OPTIONS": return emptyResponse(204, FILES_DAV_OPTIONS_HEADERS); case "PROPFIND": return await handlePropfind(c, subject, path); case "PROPPATCH": return await handleProppatch(c, subject, path); case "MKCOL": return await handleMkcol(c, subject, path); case "GET": case "HEAD": return await handleGetHead(c, subject, path); case "PUT": return await handlePut(c, subject, path); case "DELETE": return await handleDelete(c, subject, path); case "COPY": case "MOVE": return await handleCopyMove(c, subject, path); case "LOCK": return await handleLock(c, subject, path); case "UNLOCK": return await handleUnlock(c, subject, path); default: return methodNotAllowed(FILES_DAV_ALLOW); } }