Created
April 11, 2025 20:24
-
-
Save jadsongmatos/3930fecc1092c24b7048714e877cc44c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/api/webdav/[[...file]].ts | |
import type { NextApiRequest, NextApiResponse } from 'next'; | |
import { v2 as webdav } from 'webdav-server'; | |
export const config = { | |
api: { | |
// Desabilita o bodyParser para preservar o corpo "cru" da requisição, essencial para WebDAV. | |
bodyParser: false, | |
}, | |
}; | |
// Configura o WebDAV com o sistema de arquivos físico (certifique-se de que o diretório exista) | |
const webdavServer = new webdav.WebDAVServer(); | |
webdavServer.setFileSystem( | |
'/', | |
new webdav.PhysicalFileSystem('./webdav_files'), | |
(success: boolean) => { | |
console.log(success ? 'Filesystem configurado com sucesso!' : 'Falha na configuração do Filesystem.'); | |
} | |
); | |
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | |
// Removendo o prefixo '/api/webdav' para mapear corretamente os caminhos no WebDAV | |
if (req.url) { | |
req.url = req.url.replace(/^\/api\/webdav/, '') || '/'; | |
} | |
// Envolvendo a execução do request em uma Promise para aguardar o término real da resposta | |
return new Promise<void>((resolve, reject) => { | |
// Dispara a resolução quando a resposta for finalizada | |
res.once('finish', resolve); | |
res.once('close', resolve); | |
// Executa a requisição com o servidor WebDAV, que será responsável por finalizar a resposta | |
webdavServer.executeRequest(req, res); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment