File
Blob: src/worker/webdav/handler.ts
| 1 | import type { DavPatAuth } from "@/worker/auth/basic"; |
| 2 | import { davError, emptyResponse, methodNotAllowed } from "@/worker/dav/http"; |
| 3 | import { normalizeFilesPath } from "@/worker/dav/paths"; |
| 4 | import type { SubjectRow } from "@/worker/db/d1/schema"; |
| 5 | import type { AppContext } from "@/worker/types"; |
| 6 | import { hasFilesScope, requiresFileWrite } from "@/worker/webdav/access"; |
| 7 | import { FILES_DAV_ALLOW, FILES_DAV_OPTIONS_HEADERS } from "@/worker/webdav/constants"; |
| 8 | import { handleCopyMove } from "@/worker/webdav/copy-move"; |
| 9 | import { handleDelete } from "@/worker/webdav/delete"; |
| 10 | import { handleGetHead } from "@/worker/webdav/get-head"; |
| 11 | import { handleLock, handleUnlock } from "@/worker/webdav/lock"; |
| 12 | import { handleMkcol } from "@/worker/webdav/mkcol"; |
| 13 | import { handlePropfind } from "@/worker/webdav/propfind"; |
| 14 | import { handleProppatch } from "@/worker/webdav/proppatch"; |
| 15 | import { handlePut } from "@/worker/webdav/put"; |
| 16 | |
| 17 | export { FILES_DAV_OPTIONS_HEADERS }; |
| 18 | |
| 19 | export 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 | } |