From 4f38447459e4d5fe2f5e63982e58509517cbfaab Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Sat, 23 Jan 2021 22:59:02 +0800 Subject: [PATCH] server: add /token endpoint to get retrieval token of content --- server/routes/api/torrents.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/server/routes/api/torrents.ts b/server/routes/api/torrents.ts index c54f7617..a20921bf 100644 --- a/server/routes/api/torrents.ts +++ b/server/routes/api/torrents.ts @@ -663,6 +663,36 @@ router.patch<{hash: string}, unknown, SetTorrentContentsPropertiesOptions>('/:ha }); }); +/** + * GET /api/torrents/{hash}/contents/{indices}/token + * @summary Gets retrieval token of contents of a torrent. + * @tags Torrent + * @security User + * @param {string} hash.path + * @param {string} indices.path - 'all' or indices of selected contents separated by ',' + * @return {string} 200 - token - text/plain + */ +router.get<{hash: string; indices: string}, unknown, unknown, {token: string}>( + '/:hash/contents/:indices/token', + // This operation performs authentication operations. + rateLimit({ + windowMs: 5 * 60 * 1000, + max: 200, + }), + (req, res) => { + if (req.user) { + const {hash, indices} = req.params; + res.status(200).send( + getToken({ + username: req.user.username, + hash, + indices, + }), + ); + } + }, +); + /** * GET /api/torrents/{hash}/contents/{indices}/data * @summary Gets downloaded data of contents of a torrent.