mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 02:42:45 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import * as express from "express";
|
|
import AnonymizedFile from "../AnonymizedFile";
|
|
import AnonymousError from "../AnonymousError";
|
|
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;
|
|
|
|
try {
|
|
await repo.countView();
|
|
|
|
const f = new AnonymizedFile({
|
|
repository: repo,
|
|
anonymizedPath,
|
|
});
|
|
if (!(await f.isFileSupported())) {
|
|
throw new AnonymousError("file_not_supported", {
|
|
httpStatus: 403,
|
|
object: f,
|
|
});
|
|
}
|
|
res.attachment(
|
|
anonymizedPath.substring(anonymizedPath.lastIndexOf("/") + 1)
|
|
);
|
|
// cache the file for 5min
|
|
res.header('Cache-Control', 'max-age=300');
|
|
await f.send(res);
|
|
} catch (error) {
|
|
return handleError(error, res, req);
|
|
}
|
|
}
|
|
);
|
|
|
|
export default router;
|