Files
Docker/Voltaserve/webdav/src/handler/handle-delete.ts
2024-04-17 20:22:30 +02:00

35 lines
835 B
TypeScript

import { IncomingMessage, ServerResponse } from 'http'
import { FileAPI } from '@/client/api'
import { Token } from '@/client/idp'
import { handleError } from '@/infra/error'
/*
This method deletes a resource identified by the URL.
Example implementation:
- Extract the file path from the URL.
- Use fs.unlink() to delete the file.
- Set the response status code to 204 if successful or an appropriate error code if the file is not found.
- Return the response.
*/
async function handleDelete(
req: IncomingMessage,
res: ServerResponse,
token: Token
) {
try {
const api = new FileAPI(token)
const file = await api.getByPath(decodeURIComponent(req.url))
await api.delete(file.id)
res.statusCode = 204
res.end()
} catch (err) {
handleError(err, res)
}
}
export default handleDelete