4a673ebb28
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies, OAuth redirects, manifest, service worker and cron runner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
37 lines
1.6 KiB
TypeScript
37 lines
1.6 KiB
TypeScript
import { appUrl } from "@/lib/base-path";
|
|
import { NextResponse } from "next/server";
|
|
import { putBlob } from "@/lib/storage/blob";
|
|
import { requirePageAction } from "@/lib/auth/user";
|
|
import { errorResponse } from "@/lib/http";
|
|
|
|
const MAX_BYTES = 8 * 1024 * 1024; // Facebook photo limit is ~10MB; IG JPEG ≤ 8MB
|
|
const ALLOWED = ["image/jpeg", "image/png"];
|
|
|
|
/**
|
|
* Uploads a post image to disk as a public, unguessable URL (served by
|
|
* `/api/uploads/[...path]`). Meta's servers fetch it directly for FB `url=`
|
|
* photo posts and IG containers, so no signed asset route is needed.
|
|
*/
|
|
export async function POST(req: Request) {
|
|
const form = await req.formData().catch(() => null);
|
|
const pageId = form?.get("pageId");
|
|
const file = form?.get("file");
|
|
if (typeof pageId !== "string" || !(file instanceof File)) {
|
|
return NextResponse.json({ error: "ไม่พบไฟล์" }, { status: 400 });
|
|
}
|
|
const auth = await requirePageAction(pageId, "editPost");
|
|
if (!auth.ok) return auth.response;
|
|
|
|
if (!ALLOWED.includes(file.type)) return NextResponse.json({ error: "รองรับเฉพาะ JPG และ PNG" }, { status: 400 });
|
|
if (file.size > MAX_BYTES) return NextResponse.json({ error: "ไฟล์ใหญ่เกิน 8MB" }, { status: 400 });
|
|
|
|
try {
|
|
const ext = file.type === "image/png" ? "png" : "jpg";
|
|
const baseUrl = appUrl(new URL(req.url).origin);
|
|
const blob = await putBlob(`posts/${pageId}/image.${ext}`, file, { contentType: file.type }, baseUrl);
|
|
return NextResponse.json({ url: blob.url });
|
|
} catch (e) {
|
|
return errorResponse(e, "upload");
|
|
}
|
|
}
|