Skip to content

Commit

Permalink
feat: 支持 JSON 返回随机文件
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Jan 12, 2025
1 parent b30433b commit c7b6d29
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/routes/ApiOther.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
}
});
}
}

0 comments on commit c7b6d29

Please sign in to comment.