1
1
import { NextResponse } from "next/server" ;
2
2
import nodemailer from "nodemailer" ;
3
+ import path from "path" ;
3
4
4
5
type MailOptions = {
5
6
from : string ;
@@ -15,8 +16,14 @@ type MailOptions = {
15
16
} ;
16
17
17
18
// Allowed MIME types for PDF and image files
18
- const ALLOWED_MIME_TYPES = [ "application/pdf" , "image/jpeg" , "image/png" , "image/gif" ] ;
19
+ const ALLOWED_MIME_TYPES = [
20
+ "application/pdf" ,
21
+ "image/jpeg" ,
22
+ "image/png" ,
23
+ "image/gif" ,
24
+ ] ;
19
25
const MAX_FILE_SIZE_MB = 5 ; // Limit file size to 5 MB
26
+ const ALLOWED_EXTENSIONS = [ ".pdf" , ".jpg" , ".jpeg" , ".png" , ".gif" ] ; // Allowed file extensions
20
27
21
28
export async function POST ( request : Request ) {
22
29
try {
@@ -66,9 +73,11 @@ export async function POST(request: Request) {
66
73
for ( const file of files ) {
67
74
if ( file instanceof Blob ) {
68
75
const fileType = file . type ;
76
+ const fileName = ( file as any ) . name ;
77
+ const fileExtension = path . extname ( fileName ) . toLowerCase ( ) ;
69
78
const fileSizeMB = file . size / ( 1024 * 1024 ) ; // Convert size to MB
70
79
71
- if ( ! ALLOWED_MIME_TYPES . includes ( fileType ) ) {
80
+ if ( ! ALLOWED_MIME_TYPES . includes ( fileType ) || ! ALLOWED_EXTENSIONS . includes ( fileExtension ) ) {
72
81
return NextResponse . json (
73
82
{ message : `File type not allowed: ${ fileType } ` } ,
74
83
{ status : 400 }
@@ -77,14 +86,14 @@ export async function POST(request: Request) {
77
86
78
87
if ( fileSizeMB > MAX_FILE_SIZE_MB ) {
79
88
return NextResponse . json (
80
- { message : `File ${ file . name } exceeds the 5MB size limit` } ,
89
+ { message : `File ${ fileName } exceeds the 5MB size limit` } ,
81
90
{ status : 400 }
82
91
) ;
83
92
}
84
93
85
94
const buffer = await file . arrayBuffer ( ) ;
86
95
attachments . push ( {
87
- filename : ( file as any ) . name ,
96
+ filename : fileName ,
88
97
content : Buffer . from ( buffer ) ,
89
98
} ) ;
90
99
}
0 commit comments