From 951c9a781c2be907873e357bf3acf5a23ed13f69 Mon Sep 17 00:00:00 2001 From: Wyatt Verchere Date: Tue, 2 Jan 2024 16:29:16 -0800 Subject: [PATCH] adds-check --- src/index.ts | 31 ++++++++++++++++++++++++++++++- wrangler.toml | 6 +++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index c14aaa9..d4195a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ function makeError(code: number, error: String, description: String): Response { }, }); } +const CUSTOM_DATA = 'custom_files'; interface UrlData { projectId: string; @@ -68,10 +69,29 @@ async function countDownload(request: Request, env: Env, urlData: UrlData) { }), ); - console.log(`Finished counting download. Status code: ${res.status}`); + console.log(`Finished checking download. Status code: ${res.status}`); console.log(`Response body: ${await res.text()}`); } +async function checkUrl(request: Request, env: Env) { + console.log(`Attempting to check url `); + const url = `${env.LABRINTH_URL}minecraft/check_token?url=${encodeURIComponent(request.url)}`; + console.log(`url: ${url}`); + let res = await fetch( + new Request(url, { + method: 'GET', + headers: { + // Pass 'authorization' header through + authorization: request.headers.get('authorization') ?? '', + }, + }), + ); + + console.log(`Finished checking URL-token validity. Status code: ${res.status}`); + console.log(`Response body: ${await res.text()}`); + return res; +} + export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { if (allowedMethods.indexOf(request.method) === -1) { @@ -108,6 +128,15 @@ export default { return makeError(404, 'not_found', 'the requested resource does not exist'); } + if (key.startsWith(CUSTOM_DATA)) { + let res = await checkUrl(request, env); + if (res.status !== 200) { + return makeError(404, 'not_found', 'the requested resource does not exist'); + } + } + + console.log(`Getting response for object with key ${key}`); + const response = new Response(request.method === 'HEAD' ? null : (object as R2ObjectBody).body, { status: 200, headers: { diff --git a/wrangler.toml b/wrangler.toml index 88ba90e..5aecc17 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -4,13 +4,13 @@ compatibility_date = "2023-10-30" account_id = "9ddae624c98677d68d93df6e524a6061" [vars] -LABRINTH_URL = "http://127.0.0.1:8000/v2/" +LABRINTH_URL = "http://127.0.0.1:8000/v3/" [env.staging.vars] -LABRINTH_URL = "https://staging-api.modrinth.com/v2/" +LABRINTH_URL = "https://staging-api.modrinth.com/v3/" [env.prod.vars] -LABRINTH_URL = "https://api.modrinth.com/v2/" +LABRINTH_URL = "https://api.modrinth.com/v3/" [[r2_buckets]] binding = "MODRINTH_CDN"