Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
70 lines
3.1 KiB
TypeScript
70 lines
3.1 KiB
TypeScript
import { NextResponse, type NextRequest } from "next/server";
|
|
import { getToken } from "@/app/lib/server/session";
|
|
import { badRequest, unauthorized } from "@/app/lib/server/apiRoute";
|
|
import { codeOf, codeResponse, expiredSignal } from "@/app/lib/server/messagesProxy";
|
|
import { forwardedClientIpHeaders } from "@/app/lib/server/clientContext";
|
|
|
|
// POST multipart { file } -> mobile-api POST /api/admin/member-messages/images -> { ref }. TWO-STEP upload:
|
|
// the ref is committed by mobile-api when the message is sent (abandoned uploads are reaped, intended).
|
|
const ALLOWED = new Set(["image/jpeg", "image/png"]);
|
|
const MAX_BYTES = 5 * 1024 * 1024;
|
|
const TIMEOUT_MS = 30_000;
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const token = await getToken();
|
|
if (!token) return unauthorized();
|
|
// Refuse an oversized declared body before buffering it (formData() reads everything into memory).
|
|
if (Number(req.headers?.get("content-length") ?? 0) > MAX_BYTES + 64 * 1024) {
|
|
return badRequest("Image must be 5 MB or smaller", { code: "MESSAGE_INVALID" });
|
|
}
|
|
|
|
let file: unknown;
|
|
try {
|
|
file = (await req.formData()).get("file");
|
|
} catch {
|
|
return badRequest("Invalid request body");
|
|
}
|
|
if (!(file instanceof File)) return badRequest('Parameter "file" is required');
|
|
if (!ALLOWED.has(file.type)) return badRequest("Only JPEG or PNG images are allowed", { code: "MESSAGE_INVALID" });
|
|
if (file.size > MAX_BYTES) return badRequest("Image must be 5 MB or smaller", { code: "MESSAGE_INVALID" });
|
|
|
|
const base = process.env.MOBILE_API_BASE_URL;
|
|
const key = process.env.MOBILE_API_KEY;
|
|
if (!base || !key) return NextResponse.json({ error: "Mobile API is not configured" }, { status: 500 });
|
|
|
|
const forwardedIp = await forwardedClientIpHeaders();
|
|
|
|
const form = new FormData();
|
|
form.set("file", file, file.name);
|
|
let res: Response;
|
|
try {
|
|
res = await fetch(`${base}/api/admin/member-messages/images`, {
|
|
method: "POST",
|
|
headers: { "api-key": key, ...forwardedIp, Authorization: `Bearer ${token}` }, // no Content-Type: fetch sets the boundary
|
|
body: form,
|
|
cache: "no-store",
|
|
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
});
|
|
} catch {
|
|
return NextResponse.json({ error: "Couldn't reach the messaging service" }, { status: 502 });
|
|
}
|
|
const text = await res.text();
|
|
let data: Record<string, unknown> = {};
|
|
try {
|
|
data = text ? JSON.parse(text) : {};
|
|
} catch {
|
|
/* non-JSON upstream body: fall through to the generic message */
|
|
}
|
|
if (!res.ok) {
|
|
if (expiredSignal(res.status, text)) return unauthorized();
|
|
const code = codeOf(text); // matches the code field or a reason text that carries it
|
|
if (code) return codeResponse(code);
|
|
const message = (data.detail as string) || (data.message as string) || (data.error as string) || "Upload failed";
|
|
const status = res.status >= 400 && res.status < 500 ? res.status : 502;
|
|
return NextResponse.json({ error: message }, { status });
|
|
}
|
|
// A 200 without a ref would let the UI send a message pointing at nothing.
|
|
if (typeof data.ref !== "string") return NextResponse.json({ error: "Upload failed" }, { status: 502 });
|
|
return NextResponse.json({ ref: data.ref });
|
|
}
|