@@ -4,21 +4,26 @@ import path from 'path';
44import { getMediaDir } from 'codefox-common' ;
55import { logger } from '@/app/log/logger' ;
66
7+
78export async function GET (
89 request : NextRequest ,
910 { params } : { params : { path : string [ ] } }
1011) {
1112 try {
1213 const mediaDir = getMediaDir ( ) ;
14+ logger . info ( `📁 getMediaDir = ${ mediaDir } ` ) ;
1315 const filePath = path . join ( mediaDir , ...params . path ) ;
1416 const normalizedPath = path . normalize ( filePath ) ;
17+ logger . info ( `📁 getMediaDir = ${ mediaDir } ` ) ;
18+ logger . info ( `📂 full filePath = ${ filePath } ` ) ;
19+ logger . debug ( `Requested path: ${ params . path . join ( '/' ) } ` ) ;
20+ logger . debug ( `Full resolved path: ${ filePath } ` ) ;
1521
1622 if ( ! normalizedPath . startsWith ( mediaDir ) ) {
17- logger . error ( 'Possible directory traversal attempt:', filePath ) ;
23+ logger . warn ( '⛔ Directory traversal attempt blocked :', filePath ) ;
1824 return new Response ( 'Access denied' , { status : 403 } ) ;
1925 }
2026
21- // File extension allowlist
2227 const contentTypeMap : Record < string , string > = {
2328 '.jpg' : 'image/jpeg' ,
2429 '.jpeg' : 'image/jpeg' ,
@@ -27,34 +32,37 @@ export async function GET(
2732 } ;
2833
2934 const ext = path . extname ( filePath ) . toLowerCase ( ) ;
35+ logger . debug ( `File extension: ${ ext } ` ) ;
3036 if ( ! contentTypeMap [ ext ] ) {
37+ logger . warn ( `⛔ Forbidden file type: ${ ext } ` ) ;
3138 return new Response ( 'Forbidden file type' , { status : 403 } ) ;
3239 }
3340
34- // File existence and size check
3541 let fileStat ;
3642 try {
3743 fileStat = await fs . stat ( filePath ) ;
3844 } catch ( err ) {
45+ logger . warn ( `❌ File not found at path: ${ filePath } ` ) ;
3946 return new Response ( 'File not found' , { status : 404 } ) ;
4047 }
4148
4249 if ( fileStat . size > 10 * 1024 * 1024 ) {
43- // 10MB limit
50+ logger . warn ( `📦 File too large ( ${ fileStat . size } bytes): ${ filePath } ` ) ;
4451 return new Response ( 'File too large' , { status : 413 } ) ;
4552 }
4653
47- // Read and return the file
4854 const fileBuffer = await fs . readFile ( filePath ) ;
55+ logger . info ( `✅ Serving file: ${ filePath } ` ) ;
56+
4957 return new Response ( fileBuffer , {
5058 headers : {
5159 'Content-Type' : contentTypeMap [ ext ] ,
5260 'X-Content-Type-Options' : 'nosniff' ,
5361 'Cache-Control' : 'public, max-age=31536000' ,
5462 } ,
5563 } ) ;
56- } catch ( error ) {
57- logger . error ( 'Error serving media file:' , error ) ;
64+ } catch ( error : any ) {
65+ logger . error ( '🔥 Error serving media file:' , error ) ;
5866 const errorMessage =
5967 process . env . NODE_ENV === 'development'
6068 ? `Error serving file: ${ error . message } `
0 commit comments