({ request, user, match }: RequestHandlerParams)
| 16 | import { ensureUserPathIsValidAndSecurelyAccessible, FileModel } from '/lib/models/files.ts'; |
| 17 | |
| 18 | async function handler({ request, user, match }: RequestHandlerParams): Promise<Response> { |
| 19 | if (!user) { |
| 20 | return new Response('Unauthorized', { |
| 21 | status: 401, |
| 22 | headers: { 'www-authenticate': 'Basic realm="bewCloud", charset="UTF-8"' }, |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | if ( |
| 27 | !(await AppConfig.isAppEnabled('files')) && !(await AppConfig.isAppEnabled('photos')) && |
| 28 | !(await AppConfig.isAppEnabled('notes')) |
| 29 | ) { |
| 30 | return new Response('Forbidden', { status: 403 }); |
| 31 | } |
| 32 | |
| 33 | let { filePath } = match.pathname.groups; |
| 34 | |
| 35 | if (!filePath) { |
| 36 | filePath = '/'; |
| 37 | } |
| 38 | |
| 39 | filePath = decodeURIComponent(filePath); |
| 40 | |
| 41 | const userId = user!.id; |
| 42 | |
| 43 | const rootPath = join(await AppConfig.getFilesRootPath(), userId); |
| 44 | |
| 45 | if (request.method === 'OPTIONS') { |
| 46 | const headers = new Headers({ |
| 47 | DAV: '1, 2', |
| 48 | 'Ms-Author-Via': 'DAV', |
| 49 | Allow: 'OPTIONS, GET, HEAD, PUT, DELETE, COPY, MOVE, MKCOL, PROPFIND, LOCK, UNLOCK', |
| 50 | 'Content-Length': '0', |
| 51 | Date: new Date().toUTCString(), |
| 52 | }); |
| 53 | |
| 54 | return new Response(null, { status: 200, headers }); |
| 55 | } |
| 56 | |
| 57 | if (request.method === 'GET') { |
| 58 | try { |
| 59 | const fileResult = await FileModel.get(userId, filePath); |
| 60 | |
| 61 | if (!fileResult.success) { |
| 62 | return new Response('Not Found', { status: 404 }); |
| 63 | } |
| 64 | |
| 65 | return new Response(fileResult.contents! as BodyInit, { |
| 66 | status: 200, |
| 67 | headers: { |
| 68 | 'cache-control': 'no-cache, no-store, must-revalidate', |
| 69 | 'content-type': fileResult.contentType!, |
| 70 | 'content-length': fileResult.byteSize!.toString(), |
| 71 | }, |
| 72 | }); |
| 73 | } catch (error) { |
| 74 | return handleWebDavError(error); |
| 75 | } |
nothing calls this directly
no test coverage detected