diff options
Diffstat (limited to 'src/routes.ts')
| -rw-r--r-- | src/routes.ts | 99 |
1 files changed, 53 insertions, 46 deletions
diff --git a/src/routes.ts b/src/routes.ts index 1c8bd07..a9006e5 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -89,61 +89,68 @@ const routes = (metadata: Metadata): Router => { ); // Upload file - router.post("/v1/:org/:repo/:branch/:commit.:ext(html|xml)", (req, res) => { - const { org, repo, branch, commit } = req.params; - - const { token, format } = req.query; - if (token != metadata.getToken()) { - return res.status(401).send(Messages.InvalidToken); - } + router.post( + "/v1/:org/:repo/:branch/:commit.:ext(html|xml)", + async (req, res) => { + const { org, repo, branch, commit } = req.params; - if (typeof format !== "string" || !formats.listFormats().includes(format)) { - return res.status(406).send(Messages.InvalidFormat); - } + const { token, format } = req.query; + const clarifiedToken = (token ?? "").toString(); + if (!(await metadata.checkToken(clarifiedToken))) { + return res.status(401).send(Messages.InvalidToken); + } - const limit = metadata.getUploadLimit(); - if (Number(req.headers["content-length"] ?? 0) > limit) { - return res.status(413).send(Messages.FileTooLarge); - } + if ( + typeof format !== "string" || + !formats.listFormats().includes(format) + ) { + return res.status(406).send(Messages.InvalidFormat); + } - let contents = ""; - req.on("data", (raw) => { - if (contents.length <= limit) { - contents += raw; + const limit = metadata.getUploadLimit(); + if (Number(req.headers["content-length"] ?? 0) > limit) { + return res.status(413).send(Messages.FileTooLarge); } - }); - req.on("end", async () => { - const formatter = formats.getFormat(format); - const identity = { - organization: org, - repository: repo, - branch, - head: { commit, format }, - }; - try { - const result = await commitFormatDocs(contents, identity, formatter); + let contents = ""; + req.on("data", (raw) => { + if (contents.length <= limit) { + contents += raw; + } + }); + req.on("end", async () => { + const formatter = formats.getFormat(format); + const identity = { + organization: org, + repository: repo, + branch, + head: { commit, format }, + }; + + try { + const result = await commitFormatDocs(contents, identity, formatter); - if (typeof result === "boolean") { - if (result) { - return res.status(200).send(); + if (typeof result === "boolean") { + if (result) { + return res.status(200).send(); + } else { + logger.error( + "Unknown error while attempting to commit branch update" + ); + return res.status(500).send(Messages.UnknownError); + } } else { - logger.error( - "Unknown error while attempting to commit branch update" - ); - return res.status(500).send(Messages.UnknownError); + return res.status(400).send(Messages.InvalidFormat); } - } else { - return res.status(400).send(Messages.InvalidFormat); + } catch (err) { + logger.error( + err ?? "Unknown error occurred while processing POST request" + ); + return res.status(500).send(Messages.UnknownError); } - } catch (err) { - logger.error( - err ?? "Unknown error occurred while processing POST request" - ); - return res.status(500).send(Messages.UnknownError); - } - }); - }); + }); + } + ); /** * Read a file from the host directory. |
