64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
||
|
|
import { prisma } from "@/lib/db";
|
||
|
|
import { pollFtp } from "@/lib/ftp-watcher";
|
||
|
|
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json().catch(() => ({}));
|
||
|
|
const action = body.action as string | undefined;
|
||
|
|
|
||
|
|
if (!action || !["enable", "disable", "scan"].includes(action)) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Invalid action. Use 'enable', 'disable', or 'scan'" },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (action === "scan") {
|
||
|
|
const result = await pollFtp();
|
||
|
|
if (result.error) {
|
||
|
|
return NextResponse.json({ error: result.error, ...result }, { status: 500 });
|
||
|
|
}
|
||
|
|
return NextResponse.json({ ok: true, ...result });
|
||
|
|
}
|
||
|
|
|
||
|
|
const settings = await prisma.appSettings.findUnique({
|
||
|
|
where: { id: "singleton" },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!settings) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Settings not configured" },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (action === "enable") {
|
||
|
|
if (!settings.ftpHost || !settings.ftpUser || !settings.ftpPass) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "FTP host, username, and password are required" },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
await prisma.appSettings.update({
|
||
|
|
where: { id: "singleton" },
|
||
|
|
data: { ftpEnabled: true },
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
await prisma.appSettings.update({
|
||
|
|
where: { id: "singleton" },
|
||
|
|
data: { ftpEnabled: false },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const updated = await prisma.appSettings.findUnique({ where: { id: "singleton" } });
|
||
|
|
return NextResponse.json({ ftpEnabled: updated?.ftpEnabled ?? false });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[ftp-watch POST]", error);
|
||
|
|
const message =
|
||
|
|
error instanceof Error ? error.message : "Failed to update FTP watch";
|
||
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|