@@ -4,8 +4,8 @@ import { fileDownloadContract } from '@/lib/api/contracts/storage-transfer'
44import { getValidationErrorMessage , parseRequest } from '@/lib/api/server'
55import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
66import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
7- import type { StorageContext } from '@/lib/uploads/config'
87import { hasCloudStorage } from '@/lib/uploads/core/storage-service'
8+ import { resolveTrustedFileContext } from '@/lib/uploads/utils/file-utils'
99import { verifyFileAccess } from '@/app/api/files/authorization'
1010import { createErrorResponse , FileNotFoundError } from '@/app/api/files/utils'
1111
@@ -58,12 +58,10 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
5858 } )
5959 }
6060
61- let storageContext : StorageContext | 'general' | undefined = context
62-
63- if ( isExecutionFile && ! context ) {
64- storageContext = 'execution'
65- logger . info ( `Using execution context for file: ${ key } ` )
66- }
61+ const storageContext = resolveTrustedFileContext (
62+ key ,
63+ isExecutionFile && ! context ? 'execution' : context
64+ )
6765
6866 const hasAccess = await verifyFileAccess (
6967 key ,
@@ -79,10 +77,9 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
7977 }
8078
8179 const { getBaseUrl } = await import ( '@/lib/core/utils/urls' )
82- const contextQuery = storageContext ? `?context=${ storageContext } ` : ''
83- const downloadUrl = `${ getBaseUrl ( ) } /api/files/serve/${ encodeURIComponent ( key ) } ${ contextQuery } `
80+ const downloadUrl = `${ getBaseUrl ( ) } /api/files/serve/${ encodeURIComponent ( key ) } ?context=${ storageContext } `
8481
85- logger . info ( `Generated download URL for ${ storageContext ?? 'inferred' } file: ${ key } ` )
82+ logger . info ( `Generated download URL for ${ storageContext } file: ${ key } ` )
8683
8784 return NextResponse . json ( {
8885 downloadUrl,
0 commit comments