92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { db } from '@/app/db/drizzle';
|
|
import { emails } from '@/app/db/schema';
|
|
import { authenticate, getBody } from '@/app/lib/utils';
|
|
import { eq } from 'drizzle-orm';
|
|
import { CopyObjectCommand, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
|
import { getS3Client } from '@/app/lib/utils';
|
|
import nodemailer from 'nodemailer';
|
|
import { Readable } from 'stream';
|
|
|
|
export async function GET(req: NextRequest) {
|
|
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
const bucket = searchParams.get('bucket');
|
|
const key = searchParams.get('key');
|
|
if (!bucket || !key) return NextResponse.json({ error: 'Missing params' }, { status: 400 });
|
|
|
|
const [email] = await db.select().from(emails).where(eq(emails.s3Key, key));
|
|
if (!email) return NextResponse.json({ error: 'Email not found' }, { status: 404 });
|
|
|
|
return NextResponse.json({
|
|
subject: email.subject,
|
|
from: email.from,
|
|
to: email.to?.join(', '),
|
|
html: email.html,
|
|
raw: email.raw,
|
|
processed: email.processed ? 'true' : 'false',
|
|
});
|
|
}
|
|
|
|
// PUT: Update processed in S3 and DB
|
|
export async function PUT(req: NextRequest) {
|
|
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
|
|
const { bucket, key, processed } = await req.json();
|
|
if (!bucket || !key) return NextResponse.json({ error: 'Missing params' }, { status: 400 });
|
|
|
|
const s3 = getS3Client();
|
|
const head = await s3.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
|
|
const newMeta = { ...head.Metadata, [process.env.PROCESSED_META_KEY!]: processed };
|
|
await s3.send(new CopyObjectCommand({
|
|
Bucket: bucket,
|
|
Key: key,
|
|
CopySource: `${bucket}/${key}`,
|
|
Metadata: newMeta,
|
|
MetadataDirective: 'REPLACE'
|
|
}));
|
|
|
|
await db.update(emails).set({ processed: processed === 'true' }).where(eq(emails.s3Key, key));
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|
|
|
|
// POST: Resend, update in S3 and DB
|
|
export async function POST(req: NextRequest) {
|
|
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
|
|
const { bucket, key } = await req.json();
|
|
if (!bucket || !key) return NextResponse.json({ error: 'Missing params' }, { status: 400 });
|
|
|
|
const s3 = getS3Client();
|
|
const { Body } = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
|
|
const raw = await getBody(Body as Readable);
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST,
|
|
port: Number(process.env.SMTP_PORT),
|
|
secure: false,
|
|
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
|
|
tls: { rejectUnauthorized: false }
|
|
});
|
|
|
|
try {
|
|
await transporter.sendMail({ raw });
|
|
// Update S3 Metadata
|
|
const head = await s3.send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
|
|
const newMeta = { ...head.Metadata, [process.env.PROCESSED_META_KEY!]: process.env.PROCESSED_META_VALUE! };
|
|
await s3.send(new CopyObjectCommand({
|
|
Bucket: bucket,
|
|
Key: key,
|
|
CopySource: `${bucket}/${key}`,
|
|
Metadata: newMeta,
|
|
MetadataDirective: 'REPLACE'
|
|
}));
|
|
// Update DB
|
|
await db.update(emails).set({ processed: true }).where(eq(emails.s3Key, key));
|
|
return NextResponse.json({ message: 'Resent successfully' });
|
|
} catch (error) {
|
|
return NextResponse.json({ error: (error as Error).message }, { status: 500 });
|
|
}
|
|
} |