mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 02:42:45 +00:00
39 lines
952 B
TypeScript
39 lines
952 B
TypeScript
import * as express from "express";
|
|
import AnonymizedFile from "../AnonymizedFile";
|
|
import { getRepo, handleError } from "./route-utils";
|
|
|
|
export const router = express.Router();
|
|
|
|
router.get(
|
|
"/:repoId/file/:path*",
|
|
async (req: express.Request, res: express.Response) => {
|
|
let anonymizedPath = req.params.path;
|
|
if (req.params[0]) {
|
|
anonymizedPath += req.params[0];
|
|
}
|
|
anonymizedPath = anonymizedPath;
|
|
|
|
const repo = await getRepo(req, res);
|
|
if (!repo) return;
|
|
|
|
await repo.countView();
|
|
|
|
try {
|
|
const f = new AnonymizedFile(repo, {
|
|
anonymizedPath,
|
|
});
|
|
if (!(await f.isFileSupported())) {
|
|
return res.status(500).send({ error: "file_not_supported" });
|
|
}
|
|
res.attachment(
|
|
anonymizedPath.substring(anonymizedPath.lastIndexOf("/") + 1)
|
|
);
|
|
await f.send(res);
|
|
} catch (error) {
|
|
return handleError(error, res);
|
|
}
|
|
}
|
|
);
|
|
|
|
export default router;
|