feat: allow download small repositories for free

This commit is contained in:
tdurieux
2022-10-04 13:53:01 +02:00
parent f7a994b6dc
commit c90710fb6f
3 changed files with 31 additions and 2 deletions

View File

@@ -9,6 +9,8 @@ interface Config {
DEFAULT_QUOTA: number;
MAX_FILE_SIZE: number;
MAX_REPO_SIZE: number;
AUTO_DOWNLOAD_REPO_SIZE: number;
FREE_DOWNLOAD_REPO_SIZE: number;
AUTH_CALLBACK: string;
/**
* Allow to download repository and files
@@ -38,6 +40,8 @@ const config: Config = {
DEFAULT_QUOTA: 2 * 1024 * 1024 * 1024 * 8,
MAX_FILE_SIZE: 100 * 1024 * 1024, // in b, 10MB
MAX_REPO_SIZE: 60000, // in kb, 60MB
AUTO_DOWNLOAD_REPO_SIZE: 150, // in kb, 150kb
FREE_DOWNLOAD_REPO_SIZE: 150, // in kb, 150kb
ENABLE_DOWNLOAD: true,
AUTH_CALLBACK: "http://localhost:5000/github/auth",
ANONYMIZATION_MASK: "XXXX",

View File

@@ -214,7 +214,7 @@ router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
}
});
function validateNewRepo(repoUpdate) {
function validateNewRepo(repoUpdate): void {
const validCharacters = /^[0-9a-zA-Z\-\_]+$/;
if (
!repoUpdate.repoId.match(validCharacters) ||
@@ -403,6 +403,9 @@ router.post("/", async (req: express.Request, res: express.Response) => {
});
}
}
if (repository.size < config.AUTO_DOWNLOAD_REPO_SIZE) {
repo.source.type = "GitHubDownload";
}
repo.conference = repoUpdate.conference;
await repo.save();

View File

@@ -24,7 +24,22 @@ router.get(
const repo = await getRepo(req, res);
if (!repo) return;
if (repo.source.type != "GitHubDownload") {
let download = false;
const conference = await repo.conference();
if (conference) {
download =
conference.quota.size > -1 &&
!!config.ENABLE_DOWNLOAD &&
repo.source.type == "GitHubDownload";
}
if (
repo.size.storage < config.FREE_DOWNLOAD_REPO_SIZE * 1024 &&
repo.source.type == "GitHubDownload"
) {
download = true;
}
if (!download) {
throw new AnonymousError("download_not_enabled", {
httpStatus: 403,
object: req.params.repoId,
@@ -124,6 +139,13 @@ router.get(
!!config.ENABLE_DOWNLOAD &&
repo.source.type == "GitHubDownload";
}
console.log(repo.size.storage, repo.source.type)
if (
repo.size.storage < config.FREE_DOWNLOAD_REPO_SIZE * 1024 &&
repo.source.type == "GitHubDownload"
) {
download = true;
}
res.header("Cache-Control", "no-cache");
res.json({