panel 2 courier api restore
This commit is contained in:
parent
74f0a40139
commit
b1b4633763
120
app/api/courrier/[id]/route.ts
Normal file
120
app/api/courrier/[id]/route.ts
Normal file
@ -0,0 +1,120 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { ImapFlow } from 'imapflow';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
// Simple in-memory cache for email content
|
||||
const emailContentCache = new Map<string, any>();
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = params.id;
|
||||
|
||||
// Authentication check
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
const cacheKey = `email:${session.user.id}:${id}`;
|
||||
if (emailContentCache.has(cacheKey)) {
|
||||
return NextResponse.json(emailContentCache.get(cacheKey));
|
||||
}
|
||||
|
||||
// Get credentials from database
|
||||
const credentials = await prisma.mailCredentials.findUnique({
|
||||
where: {
|
||||
userId: session.user.id
|
||||
}
|
||||
});
|
||||
|
||||
if (!credentials) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No mail credentials found. Please configure your email account.' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create IMAP client
|
||||
const client = new ImapFlow({
|
||||
host: credentials.host,
|
||||
port: credentials.port,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: credentials.email,
|
||||
pass: credentials.password,
|
||||
},
|
||||
logger: false,
|
||||
emitLogs: false,
|
||||
tls: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
|
||||
// Open INBOX
|
||||
await client.mailboxOpen('INBOX');
|
||||
|
||||
// Fetch the email with UID search
|
||||
const message = await client.fetchOne(id, {
|
||||
uid: true,
|
||||
source: true,
|
||||
envelope: true,
|
||||
bodyStructure: true,
|
||||
flags: true
|
||||
});
|
||||
|
||||
if (!message) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email not found' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Parse the email content
|
||||
const emailContent = {
|
||||
id: message.uid.toString(),
|
||||
from: message.envelope.from?.[0]?.address || '',
|
||||
fromName: message.envelope.from?.[0]?.name ||
|
||||
message.envelope.from?.[0]?.address?.split('@')[0] || '',
|
||||
to: message.envelope.to?.map((addr: any) => addr.address).join(', ') || '',
|
||||
subject: message.envelope.subject || '(No subject)',
|
||||
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
||||
content: message.source?.toString() || '',
|
||||
read: message.flags.has('\\Seen'),
|
||||
starred: message.flags.has('\\Flagged'),
|
||||
flags: Array.from(message.flags),
|
||||
hasAttachments: message.bodyStructure?.type === 'multipart'
|
||||
};
|
||||
|
||||
// Cache the email content (with a 15-minute expiry)
|
||||
emailContentCache.set(cacheKey, emailContent);
|
||||
setTimeout(() => emailContentCache.delete(cacheKey), 15 * 60 * 1000);
|
||||
|
||||
// Return the email content
|
||||
return NextResponse.json(emailContent);
|
||||
} finally {
|
||||
try {
|
||||
await client.logout();
|
||||
} catch (e) {
|
||||
console.error('Error during IMAP logout:', e);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching email:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch email content' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user