11import { readFile } from 'fs/promises'
22import type { NextRequest } from 'next/server'
33import { NextResponse } from 'next/server'
4+ import { checkHybridAuth } from '@/lib/auth/hybrid'
45import { createLogger } from '@/lib/logs/console/logger'
56import { downloadFile , getStorageProvider , isUsingCloudStorage } from '@/lib/uploads'
67import { S3_KB_CONFIG } from '@/lib/uploads/setup'
78import '@/lib/uploads/setup.server'
8- import { getSession } from '@/lib/auth'
99import {
1010 createErrorResponse ,
1111 createFileResponse ,
@@ -29,23 +29,19 @@ export async function GET(
2929
3030 logger . info ( 'File serve request:' , { path } )
3131
32- const session = await getSession ( )
33- if ( ! session ?. user ?. id ) {
34- logger . warn ( 'Unauthorized file access attempt' , { path } )
32+ const authResult = await checkHybridAuth ( request , { requireWorkflowId : false } )
33+
34+ if ( ! authResult . success ) {
35+ logger . warn ( 'Unauthorized file access attempt' , { path, error : authResult . error } )
3536 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
3637 }
3738
38- const userId = session . user . id
39+ const userId = authResult . userId
3940 const fullPath = path . join ( '/' )
4041 const isS3Path = path [ 0 ] === 's3'
4142 const isBlobPath = path [ 0 ] === 'blob'
4243 const isCloudPath = isS3Path || isBlobPath
4344 const cloudKey = isCloudPath ? path . slice ( 1 ) . join ( '/' ) : fullPath
44- const isExecutionFile = cloudKey . split ( '/' ) . length >= 3 && ! cloudKey . startsWith ( 'kb/' )
45-
46- if ( ! isExecutionFile ) {
47- logger . info ( 'Authenticated file access granted' , { userId, path : cloudKey } )
48- }
4945
5046 if ( isUsingCloudStorage ( ) || isCloudPath ) {
5147 const bucketType = request . nextUrl . searchParams . get ( 'bucket' )
@@ -64,7 +60,7 @@ export async function GET(
6460 }
6561}
6662
67- async function handleLocalFile ( filename : string , userId : string ) : Promise < NextResponse > {
63+ async function handleLocalFile ( filename : string , userId ? : string ) : Promise < NextResponse > {
6864 try {
6965 const filePath = findLocalFile ( filename )
7066
0 commit comments