Files
webmailserver/app/api/mail/messages/[uid]/attachments/route.ts
2026-05-14 01:57:52 +03:00

45 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/auth";
import { getMailSession } from "@/lib/mail-session";
import { getAttachment } from "@/lib/imap";
// GET /api/mail/messages/[uid]/attachments?folder=INBOX&filename=doc.pdf
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ uid: string }> }
) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const creds = await getMailSession();
if (!creds) return NextResponse.json({ error: "Mail oturumu yok" }, { status: 401 });
const { uid } = await params;
const folder = req.nextUrl.searchParams.get("folder") ?? "INBOX";
const filename = req.nextUrl.searchParams.get("filename");
if (!filename) {
return NextResponse.json({ error: "Dosya adı gerekli" }, { status: 400 });
}
try {
const att = await getAttachment(creds, folder, parseInt(uid), filename);
if (!att) {
return NextResponse.json({ error: "Ek bulunamadı" }, { status: 404 });
}
return new NextResponse(att.content, {
headers: {
"Content-Type": att.contentType,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
"Content-Length": String(att.content.length),
},
});
} catch (err: any) {
return NextResponse.json(
{ error: "Ek indirilemedi: " + (err?.message ?? "") },
{ status: 502 }
);
}
}