improve browser cache strategy

This commit is contained in:
tdurieux
2021-08-13 00:34:09 +02:00
parent 70140bfb4b
commit 0283549901
4 changed files with 41 additions and 23 deletions

View File

@@ -29,6 +29,8 @@ router.get(
res.attachment(
anonymizedPath.substring(anonymizedPath.lastIndexOf("/") + 1)
);
// ache the file for 6 hours
res.header('Cache-Control', 'max-age=21600000');
await f.send(res);
} catch (error) {
return handleError(error, res);

View File

@@ -3,11 +3,7 @@ import { ensureAuthenticated } from "./connection";
import * as db from "../database/database";
import { getRepo, getUser, handleError } from "./route-utils";
import RepositoryModel from "../database/repositories/repositories.model";
import {
GitHubRepository,
getRepositoryFromGitHub,
} from "../source/GitHubRepository";
import { getRepositoryFromGitHub } from "../source/GitHubRepository";
import gh = require("parse-github-url");
import GitHubBase from "../source/GitHubBase";
import AnonymizedRepositoryModel from "../database/anonymizedRepositories/anonymizedRepositories.model";
@@ -17,6 +13,22 @@ import Repository from "../Repository";
const router = express.Router();
// get repository information
router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
const repo = await getRepo(req, res, { nocheck: true });
if (!repo) return;
try {
const user = await getUser(req);
if (user.username != repo.model.owner) {
return res.status(401).send({ error: "not_authorized" });
}
res.json((await db.getRepository(req.params.repoId)).toJSON());
} catch (error) {
handleError(error, res);
}
});
// user needs to be connected for all user API
router.use(ensureAuthenticated);
@@ -254,28 +266,30 @@ router.post("/", async (req: express.Request, res: express.Response) => {
owner: r.owner,
repo: r.name,
});
const repo = new AnonymizedRepositoryModel();
repo.repoId = repoUpdate.repoId;
repo.anonymizeDate = new Date();
repo.owner = user.username;
repo.source = {
type:
repoUpdate.options.mode == "download" ? "GitHubDownload" : "GitHubStream",
repoUpdate.options.mode == "download"
? "GitHubDownload"
: "GitHubStream",
accessToken: user.accessToken,
repositoryId: repository.model.id,
repositoryName: repoUpdate.fullName,
};
if (repo.source.type == "GitHubDownload") {
// details.size is in kilobytes
if (repository.size > config.MAX_REPO_SIZE) {
return res.status(500).send({ error: "invalid_mode" });
}
}
updateRepoModel(repo, repoUpdate);
await repo.save();
res.send("ok");
new Repository(repo).anonymize();

View File

@@ -2,21 +2,10 @@ import * as express from "express";
import config from "../../config";
import * as db from "../database/database";
import { getRepo, getUser, handleError } from "./route-utils";
import { getRepo, handleError } from "./route-utils";
const router = express.Router();
router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
const repo = await getRepo(req, res, { nocheck: true });
if (!repo) return;
try {
res.json((await db.getRepository(req.params.repoId)).toJSON());
} catch (error) {
handleError(error, res);
}
});
router.get(
"/:repoId/zip",
async (req: express.Request, res: express.Response) => {
@@ -27,6 +16,10 @@ router.get(
try {
res.attachment(`${repo.repoId}.zip`);
// ache the file for 6 hours
res.header('Cache-Control', 'max-age=21600000');
repo.zip().pipe(res);
} catch (error) {
handleError(error, res);
@@ -40,6 +33,9 @@ router.get(
const repo = await getRepo(req, res);
if (!repo) return;
try {
// ache the file for 6 hours
res.header('Cache-Control', 'max-age=21600000');
res.json(await repo.anonymizedFiles({ includeSha: false }));
} catch (error) {
handleError(error, res);

View File

@@ -85,7 +85,13 @@ export default async function start() {
.get("/r/:repoId/?*", indexResponse)
.get("/repository/:repoId/?*", indexResponse);
app.use(express.static(path.join(__dirname, "..", "public")));
app.use(
express.static(path.join(__dirname, "..", "public"), {
etag: true,
lastModified: true,
maxAge: 3600000, // 1h
})
);
app.get("*", indexResponse);