Skip to content

Commit 80b044f

Browse files
check again
1 parent fcdc04a commit 80b044f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/app/api/mail/route.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextResponse } from "next/server";
22
import nodemailer from "nodemailer";
3+
import path from "path";
34

45
type MailOptions = {
56
from: string;
@@ -15,8 +16,14 @@ type MailOptions = {
1516
};
1617

1718
// 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+
];
1925
const MAX_FILE_SIZE_MB = 5; // Limit file size to 5 MB
26+
const ALLOWED_EXTENSIONS = [".pdf", ".jpg", ".jpeg", ".png", ".gif"]; // Allowed file extensions
2027

2128
export async function POST(request: Request) {
2229
try {
@@ -66,9 +73,11 @@ export async function POST(request: Request) {
6673
for (const file of files) {
6774
if (file instanceof Blob) {
6875
const fileType = file.type;
76+
const fileName = (file as any).name;
77+
const fileExtension = path.extname(fileName).toLowerCase();
6978
const fileSizeMB = file.size / (1024 * 1024); // Convert size to MB
7079

71-
if (!ALLOWED_MIME_TYPES.includes(fileType)) {
80+
if (!ALLOWED_MIME_TYPES.includes(fileType) || !ALLOWED_EXTENSIONS.includes(fileExtension)) {
7281
return NextResponse.json(
7382
{ message: `File type not allowed: ${fileType}` },
7483
{ status: 400 }
@@ -77,14 +86,14 @@ export async function POST(request: Request) {
7786

7887
if (fileSizeMB > MAX_FILE_SIZE_MB) {
7988
return NextResponse.json(
80-
{ message: `File ${file.name} exceeds the 5MB size limit` },
89+
{ message: `File ${fileName} exceeds the 5MB size limit` },
8190
{ status: 400 }
8291
);
8392
}
8493

8594
const buffer = await file.arrayBuffer();
8695
attachments.push({
87-
filename: (file as any).name,
96+
filename: fileName,
8897
content: Buffer.from(buffer),
8998
});
9099
}

0 commit comments

Comments
 (0)