11import { NextRequest , NextResponse } from "next/server"
22import { makeAPIErrorResponse , makeUnauthenticatedAPIErrorResponse } from "@/common"
33import { session } from "@/composition"
4+ import { env } from "@/common"
45
56export 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