f2a0c20f07
Users can rate a recipe with review text and an optional photo. Adds ratings.photo_key column, a reviews list endpoint, and a review-purpose presign path (reviewer isn't the recipe owner, so the upload authorization differs from cover-photo uploads). Also fixes CSP connect-src/img-src to allow the storage origin — direct-to-S3/MinIO presigned uploads and stored images were silently blocked by Content-Security-Policy in the browser. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
47 lines
1.9 KiB
TypeScript
47 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
import { createPresignedUploadUrl } from "@/lib/storage";
|
|
import { db, recipes, eq, and } from "@epicure/db";
|
|
|
|
const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp", "image/avif"] as const;
|
|
type AllowedType = (typeof ALLOWED_TYPES)[number];
|
|
|
|
const Schema = z.object({
|
|
contentType: z.string().refine((t): t is AllowedType => (ALLOWED_TYPES as readonly string[]).includes(t), {
|
|
message: "Content type must be jpeg, png, webp, or avif",
|
|
}),
|
|
recipeId: z.string().uuid(),
|
|
purpose: z.enum(["recipe", "review"]).default("recipe"),
|
|
});
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = Schema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "Validation error", issues: parsed.error.issues }, { status: 400 });
|
|
}
|
|
|
|
const { recipeId, contentType, purpose } = parsed.data;
|
|
const recipe = await db.query.recipes.findFirst({
|
|
where: purpose === "recipe"
|
|
? and(eq(recipes.id, recipeId), eq(recipes.authorId, session!.user.id))
|
|
: eq(recipes.id, recipeId),
|
|
columns: { id: true, visibility: true, authorId: true },
|
|
});
|
|
if (!recipe) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
if (purpose === "review" && recipe.visibility === "private" && recipe.authorId !== session!.user.id) {
|
|
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
}
|
|
|
|
const ext = contentType.split("/")[1] ?? "jpg";
|
|
const folder = purpose === "review" ? "reviews" : "photos";
|
|
const key = `recipes/${recipeId}/${folder}/${session!.user.id}-${crypto.randomUUID()}.${ext}`;
|
|
const url = await createPresignedUploadUrl(key, contentType);
|
|
|
|
return NextResponse.json({ url, key });
|
|
}
|