Skip to content

Commit 8845001

Browse files
committed
Makes max file size and timeout configurable
1 parent 62f0912 commit 8845001

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ POSTGRESQL_DB=framna-docs
1313
REPOSITORY_NAME_SUFFIX=-openapi
1414
HIDDEN_REPOSITORIES=
1515
NEW_PROJECT_TEMPLATE_REPOSITORY=shapehq/starter-openapi
16+
PROXY_API_MAXIMUM_FILE_SIZE_IN_MEGABYTES = 10
17+
PROXY_API_TIMEOUT_IN_SECONDS = 30
1618
GITHUB_WEBHOOK_SECRET=preshared secret also put in app configuration in GitHub
1719
GITHUB_WEBHOK_REPOSITORY_ALLOWLIST=
1820
GITHUB_WEBHOK_REPOSITORY_DISALLOWLIST=

src/app/api/proxy/route.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server"
22
import { makeAPIErrorResponse, makeUnauthenticatedAPIErrorResponse } from "@/common"
33
import { session } from "@/composition"
4+
import { env } from "@/common"
45

56
export async function GET(req: NextRequest) {
67
const isAuthenticated = await session.getIsAuthenticated()
@@ -18,8 +19,10 @@ export async function GET(req: NextRequest) {
1819
return makeAPIErrorResponse(400, "Invalid \"url\" query parameter.")
1920
}
2021
try {
21-
const maxBytes = 10 * 1024 * 1024 // 10 MB
22-
const file = await downloadFile({ url, maxBytes })
22+
const maxMegabytes = Number(env.getOrThrow("PROXY_API_MAXIMUM_FILE_SIZE_IN_MEGABYTES"))
23+
const timeoutInSeconds = Number(env.getOrThrow("PROXY_API_TIMEOUT_IN_SECONDS"))
24+
const maxBytes = maxMegabytes * 1024 * 1024
25+
const file = await downloadFile({ url, maxBytes, timeoutInSeconds })
2326
return new NextResponse(file, { status: 200 })
2427
} catch (error) {
2528
if (error instanceof Error == false) {
@@ -35,10 +38,14 @@ export async function GET(req: NextRequest) {
3538
}
3639
}
3740

38-
async function downloadFile(params: { url: URL, maxBytes: number }): Promise<Blob> {
39-
const { url, maxBytes } = params
41+
async function downloadFile(params: {
42+
url: URL,
43+
maxBytes: number,
44+
timeoutInSeconds: number
45+
}): Promise<Blob> {
46+
const { url, maxBytes, timeoutInSeconds } = params
4047
const abortController = new AbortController()
41-
const timeoutSignal = AbortSignal.timeout(30 * 1000)
48+
const timeoutSignal = AbortSignal.timeout(timeoutInSeconds * 1000)
4249
const response = await fetch(url, {
4350
signal: AbortSignal.any([abortController.signal, timeoutSignal])
4451
})

0 commit comments

Comments
 (0)