echos-ocr/src/app/api/ftp-watch/test/route.ts
Randall Stillwell 028840a887 Add SFTP support: replace FTP-only TLS toggle with protocol selector
Dreamhost (and most modern hosts) use SFTP (port 22), not FTP (port 21).
The previous implementation only supported FTP/FTPS via basic-ftp, causing
timeouts when connecting to SFTP servers.

Changes:
- Add ssh2-sftp-client for SFTP connections
- Replace ftpTls boolean with ftpProtocol ("sftp" | "ftp" | "ftps") in schema
- Rewrite ftp-watcher.ts to support both SFTP and FTP/FTPS protocols
- Update UI with protocol dropdown that auto-switches the default port
- Add ssh2/ssh2-sftp-client to serverExternalPackages in next.config
- Default to SFTP on port 22

Made-with: Cursor
2026-04-17 15:12:54 -05:00

34 lines
1,006 B
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { testFtpConnection } from "@/lib/ftp-watcher";
import { requireApiAuth, handleApiError } from "@/lib/api-auth";
import { prisma } from "@/lib/db";
const MASKED = "••••••••";
export async function POST(request: NextRequest) {
try {
await requireApiAuth();
const body = await request.json();
let pass = body.pass || "";
if (!pass || pass === MASKED) {
const settings = await prisma.appSettings.findUnique({ where: { id: "singleton" } });
pass = settings?.ftpPass || "";
}
const protocol = body.protocol || "sftp";
const defaultPort = protocol === "sftp" ? 22 : 21;
const result = await testFtpConnection({
host: body.host,
port: body.port || defaultPort,
user: body.user,
pass,
protocol,
incomingDir: body.incomingDir || "/incoming",
});
return NextResponse.json(result);
} catch (error) {
return handleApiError(error);
}
}