import { IncomingMessage, ServerResponse } from 'http'
import { FileAPI, FileType } from '@/client/api'
import { Token } from '@/client/idp'
import { handleError } from '@/infra/error'
/*
This method retrieves properties and metadata of a resource.
Example implementation:
- Extract the file path from the URL.
- Use fs.stat() to retrieve the file metadata.
- Format the response body in the desired XML format with the properties and metadata.
- Set the response status code to 207 if successful or an appropriate error code if the file is not found or encountered an error.
- Set the Content-Type header to indicate the XML format.
- Return the response.
*/
async function handlePropfind(
req: IncomingMessage,
res: ServerResponse,
token: Token
) {
try {
const api = new FileAPI(token)
const file = await api.getByPath(decodeURIComponent(req.url))
if (file.type === FileType.File) {
const responseXml = `
${encodeURIComponent(file.name)}
${
file.original
? `${file.original.size}`
: ''
}
${new Date(
file.createTime
).toUTCString()}
${new Date(
file.updateTime
).toUTCString()}
HTTP/1.1 200 OK
`
res.statusCode = 207
res.setHeader('Content-Type', 'application/xml; charset=utf-8')
res.end(responseXml)
} else if (file.type === FileType.Folder) {
const list = await api.listByPath(decodeURIComponent(req.url))
const responseXml = `
${req.url}
0
${new Date(
file.updateTime
).toUTCString()}
${new Date(
file.createTime
).toUTCString()}
HTTP/1.1 200 OK
${list
.map((item) => {
return `
${req.url}${encodeURIComponent(item.name)}
${
item.type === FileType.Folder ? '' : ''
}
${
item.type === FileType.File && item.original
? `${item.original.size}`
: ''
}
${new Date(
item.updateTime
).toUTCString()}
${new Date(
item.createTime
).toUTCString()}
HTTP/1.1 200 OK
`
})
.join('')}
`
res.statusCode = 207
res.setHeader('Content-Type', 'application/xml; charset=utf-8')
res.end(responseXml)
}
} catch (err) {
handleError(err, res)
}
}
export default handlePropfind