From c7b6d29e9feef9f59ebff227884f35ea4184a81d Mon Sep 17 00:00:00 2001 From: SALTWOOD <105980161+SALTWOOD@users.noreply.github.com> Date: Sun, 12 Jan 2025 22:41:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20JSON=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E9=9A=8F=E6=9C=BA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/ApiOther.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/routes/ApiOther.ts b/src/routes/ApiOther.ts index 563e10b..548dea8 100644 --- a/src/routes/ApiOther.ts +++ b/src/routes/ApiOther.ts @@ -4,7 +4,23 @@ import { ApiFactory } from "./ApiFactory.js"; export class ApiOther { public static register(inst: ApiFactory) { inst.app.get("/api/other/random_file", (req, res) => { - res.redirect(encodeURI(Utilities.getRandomElement(inst.files)?.path || '')); + const type = req.query.type ?? "redirect"; + switch (type) { + case "302": + case "redirect": + res.redirect(encodeURI(Utilities.getRandomElement(inst.files)?.path || '')); + break; + case "json": + const url = Utilities.getRandomElement(inst.files)?.path || ''; + res.json({ + url: url, + encodedUrl: encodeURI(url) + }); + default: + res.status(400).json({ + error: "Bad request" + }); + } }); } } \ No newline at end of file