Skip to content
File

Blob: src/worker/webdav/handler.ts

typescript58 lines
1import type { DavPatAuth } from "@/worker/auth/basic";
2import { davError, emptyResponse, methodNotAllowed } from "@/worker/dav/http";
3import { normalizeFilesPath } from "@/worker/dav/paths";
4import type { SubjectRow } from "@/worker/db/d1/schema";
5import type { AppContext } from "@/worker/types";
6import { hasFilesScope, requiresFileWrite } from "@/worker/webdav/access";
7import { FILES_DAV_ALLOW, FILES_DAV_OPTIONS_HEADERS } from "@/worker/webdav/constants";
8import { handleCopyMove } from "@/worker/webdav/copy-move";
9import { handleDelete } from "@/worker/webdav/delete";
10import { handleGetHead } from "@/worker/webdav/get-head";
11import { handleLock, handleUnlock } from "@/worker/webdav/lock";
12import { handleMkcol } from "@/worker/webdav/mkcol";
13import { handlePropfind } from "@/worker/webdav/propfind";
14import { handleProppatch } from "@/worker/webdav/proppatch";
15import { handlePut } from "@/worker/webdav/put";
16 
17export { FILES_DAV_OPTIONS_HEADERS };
18 
19export async function handleWebDavFiles(c: AppContext, subject: SubjectRow, auth: DavPatAuth): Promise<Response> {
20 if (
21 requiresFileWrite(c.req.method)
22 ? !hasFilesScope(auth.scopes, "dav:files:write")
23 : !hasFilesScope(auth.scopes, "dav:files:read")
24 ) {
25 return davError(403, "PAT scope does not allow this method");
26 }
27 
28 const path = normalizeFilesPath(new URL(c.req.url).pathname);
29 if (!path.ok) return davError(path.status, path.message);
30 
31 switch (c.req.method) {
32 case "OPTIONS":
33 return emptyResponse(204, FILES_DAV_OPTIONS_HEADERS);
34 case "PROPFIND":
35 return await handlePropfind(c, subject, path);
36 case "PROPPATCH":
37 return await handleProppatch(c, subject, path);
38 case "MKCOL":
39 return await handleMkcol(c, subject, path);
40 case "GET":
41 case "HEAD":
42 return await handleGetHead(c, subject, path);
43 case "PUT":
44 return await handlePut(c, subject, path);
45 case "DELETE":
46 return await handleDelete(c, subject, path);
47 case "COPY":
48 case "MOVE":
49 return await handleCopyMove(c, subject, path);
50 case "LOCK":
51 return await handleLock(c, subject, path);
52 case "UNLOCK":
53 return await handleUnlock(c, subject, path);
54 default:
55 return methodNotAllowed(FILES_DAV_ALLOW);
56 }
57}