From c90710fb6fe3369f621e4043bd60ce6526e9ad51 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 4 Oct 2022 13:53:01 +0200 Subject: [PATCH] feat: allow download small repositories for free --- config.ts | 4 ++++ src/routes/repository-private.ts | 5 ++++- src/routes/repository-public.ts | 24 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/config.ts b/config.ts index c1a29c1..be58e52 100644 --- a/config.ts +++ b/config.ts @@ -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", diff --git a/src/routes/repository-private.ts b/src/routes/repository-private.ts index 45b6255..8ff2033 100644 --- a/src/routes/repository-private.ts +++ b/src/routes/repository-private.ts @@ -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(); diff --git a/src/routes/repository-public.ts b/src/routes/repository-public.ts index 2ae41c4..cf26c12 100644 --- a/src/routes/repository-public.ts +++ b/src/routes/repository-public.ts @@ -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({