first commit
This commit is contained in:
44
app/api/mail/messages/[uid]/attachments/route.ts
Normal file
44
app/api/mail/messages/[uid]/attachments/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
32
app/api/mail/messages/[uid]/route.ts
Normal file
32
app/api/mail/messages/[uid]/route.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { getMailSession } from "@/lib/mail-session";
|
||||
import { getMessage } from "@/lib/imap";
|
||||
|
||||
// GET /api/mail/messages/[uid]?folder=INBOX
|
||||
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";
|
||||
|
||||
try {
|
||||
const message = await getMessage(creds, folder, parseInt(uid));
|
||||
if (!message) {
|
||||
return NextResponse.json({ error: "Mesaj bulunamadı" }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(message);
|
||||
} catch (err: any) {
|
||||
return NextResponse.json(
|
||||
{ error: "Mesaj alınamadı: " + (err?.message ?? "") },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
}
|
||||
59
app/api/mail/messages/route.ts
Normal file
59
app/api/mail/messages/route.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { getMailSession } from "@/lib/mail-session";
|
||||
import { listMessages, deleteMessage, moveMessage, toggleFlag } from "@/lib/imap";
|
||||
|
||||
// GET /api/mail/messages?folder=INBOX&page=1
|
||||
export async function GET(req: NextRequest) {
|
||||
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 folder = req.nextUrl.searchParams.get("folder") ?? "INBOX";
|
||||
const page = parseInt(req.nextUrl.searchParams.get("page") ?? "1");
|
||||
|
||||
try {
|
||||
const result = await listMessages(creds, folder, page, 50);
|
||||
return NextResponse.json(result);
|
||||
} catch (err: any) {
|
||||
return NextResponse.json(
|
||||
{ error: "Mesajlar alınamadı: " + (err?.message ?? "") },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/mail/messages — actions: delete, move, flag
|
||||
export async function POST(req: NextRequest) {
|
||||
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 { action, folder, uid, toFolder, flag, add } = await req.json();
|
||||
|
||||
try {
|
||||
switch (action) {
|
||||
case "delete":
|
||||
await deleteMessage(creds, folder, uid);
|
||||
break;
|
||||
case "move":
|
||||
await moveMessage(creds, folder, uid, toFolder);
|
||||
break;
|
||||
case "flag":
|
||||
await toggleFlag(creds, folder, uid, flag, add);
|
||||
break;
|
||||
default:
|
||||
return NextResponse.json({ error: "Bilinmeyen işlem" }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (err: any) {
|
||||
return NextResponse.json(
|
||||
{ error: "İşlem başarısız: " + (err?.message ?? "") },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user