7.5 추천/신고, 조회수 카운트 o
This commit is contained in:
15
src/app/api/posts/[id]/view/route.ts
Normal file
15
src/app/api/posts/[id]/view/route.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { getUserIdFromRequest } from "@/lib/auth";
|
||||
|
||||
export async function POST(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await context.params;
|
||||
const userId = getUserIdFromRequest(req);
|
||||
const ip = req.headers.get("x-forwarded-for") || undefined;
|
||||
const userAgent = req.headers.get("user-agent") || undefined;
|
||||
await prisma.postViewLog.create({ data: { postId: id, userId: userId ?? null, ip, userAgent } });
|
||||
await prisma.postStat.upsert({ where: { postId: id }, update: { views: { increment: 1 } }, create: { postId: id, views: 1 } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
|
||||
24
src/app/api/uploads/route.ts
Normal file
24
src/app/api/uploads/route.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const form = await req.formData();
|
||||
const file = form.get("file") as File | null;
|
||||
if (!file) return NextResponse.json({ error: "file required" }, { status: 400 });
|
||||
|
||||
const bytes = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(bytes);
|
||||
const ext = path.extname(file.name || "") || ".bin";
|
||||
const uploadsDir = path.join(process.cwd(), "public", "uploads");
|
||||
await fs.mkdir(uploadsDir, { recursive: true });
|
||||
const filename = `${Date.now()}-${Math.random().toString(36).slice(2)}${ext}`;
|
||||
const filepath = path.join(uploadsDir, filename);
|
||||
await fs.writeFile(filepath, buffer);
|
||||
const url = `/uploads/${filename}`;
|
||||
return NextResponse.json({ url });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user