| 1 |
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; |
| 2 |
import { createWebDAVServer } from '../services/webdav-server.js'; |
| 3 |
import { createServer } from 'node:http'; |
| 4 |
|
| 5 |
export async function webdavRoutes(fastify: FastifyInstance) { |
| 6 |
if (!fastify.config.webdavEnabled) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
// Create WebDAV server instance |
| 11 |
const webdavServer = createWebDAVServer(fastify.zephyrfs, { |
| 12 |
username: 'zephyrfs', |
| 13 |
password: 'webdav', // In production, use proper authentication |
| 14 |
}); |
| 15 |
|
| 16 |
// Handle WebDAV requests by proxying to the WebDAV server |
| 17 |
fastify.all(`${fastify.config.webdavPath}/*`, async (request: FastifyRequest, reply: FastifyReply) => { |
| 18 |
return new Promise((resolve, reject) => { |
| 19 |
// Create a minimal HTTP server request/response for the WebDAV server |
| 20 |
const req = { |
| 21 |
method: request.method, |
| 22 |
url: request.url.replace(fastify.config.webdavPath, ''), |
| 23 |
headers: request.headers, |
| 24 |
on: (event: string, handler: Function) => { |
| 25 |
if (event === 'data') { |
| 26 |
// Handle request body |
| 27 |
request.raw.on('data', handler); |
| 28 |
} else if (event === 'end') { |
| 29 |
request.raw.on('end', handler); |
| 30 |
} |
| 31 |
}, |
| 32 |
pipe: (destination: any) => { |
| 33 |
request.raw.pipe(destination); |
| 34 |
}, |
| 35 |
}; |
| 36 |
|
| 37 |
const res = { |
| 38 |
writeHead: (statusCode: number, headers?: any) => { |
| 39 |
reply.code(statusCode); |
| 40 |
if (headers) { |
| 41 |
Object.entries(headers).forEach(([key, value]) => { |
| 42 |
reply.header(key, value as string); |
| 43 |
}); |
| 44 |
} |
| 45 |
}, |
| 46 |
write: (chunk: any) => { |
| 47 |
if (!reply.sent) { |
| 48 |
reply.raw.write(chunk); |
| 49 |
} |
| 50 |
}, |
| 51 |
end: (chunk?: any) => { |
| 52 |
if (!reply.sent) { |
| 53 |
if (chunk) { |
| 54 |
reply.raw.write(chunk); |
| 55 |
} |
| 56 |
reply.raw.end(); |
| 57 |
resolve(undefined); |
| 58 |
} |
| 59 |
}, |
| 60 |
setHeader: (name: string, value: string) => { |
| 61 |
reply.header(name, value); |
| 62 |
}, |
| 63 |
}; |
| 64 |
|
| 65 |
try { |
| 66 |
// Process the request through the WebDAV server |
| 67 |
webdavServer.executeRequest(req as any, res as any); |
| 68 |
} catch (error) { |
| 69 |
reject(error); |
| 70 |
} |
| 71 |
}); |
| 72 |
}); |
| 73 |
|
| 74 |
// WebDAV discovery endpoint |
| 75 |
fastify.get('/webdav', async (request: FastifyRequest, reply: FastifyReply) => { |
| 76 |
return { |
| 77 |
service: 'ZephyrFS WebDAV', |
| 78 |
url: `${request.protocol}://${request.hostname}:${fastify.config.port}${fastify.config.webdavPath}/`, |
| 79 |
authentication: 'Basic', |
| 80 |
username: 'zephyrfs', |
| 81 |
password: 'webdav', |
| 82 |
description: 'Mount ZephyrFS as a network drive', |
| 83 |
instructions: { |
| 84 |
windows: 'Map Network Drive → Connect to a Web site → Enter the URL above', |
| 85 |
macos: 'Finder → Go → Connect to Server → Enter the URL above', |
| 86 |
linux: 'File Manager → Other Locations → Connect to Server → Enter the URL above', |
| 87 |
}, |
| 88 |
}; |
| 89 |
}); |
| 90 |
} |