7.10 제휴업소 일반(사진) 카테고리 전용 이미지 첨부/미리보기 규칙 o

This commit is contained in:
koreacomp5
2025-10-09 17:16:27 +09:00
parent b1f368cdbd
commit 9ed65edf65
5 changed files with 25 additions and 12 deletions

View File

@@ -19,6 +19,15 @@ export async function POST(req: Request) {
const { boardId, authorId, title, content, isAnonymous } = parsed.data;
const board = await prisma.board.findUnique({ where: { id: boardId } });
const requiresApproval = board?.requiresApproval ?? false;
// 사진형 보드 필수 이미지 검증: content 내 이미지 링크 최소 1개
const isImageOnly = (board?.requiredFields as any)?.imageOnly;
const minImages = (board?.requiredFields as any)?.minImages ?? 0;
if (isImageOnly || minImages > 0) {
const imageLinks = (content.match(/!\[[^\]]*\]\([^\)]+\)/g) ?? []).length;
if (imageLinks < (minImages || 1)) {
return NextResponse.json({ error: { message: `이미지 최소 ${minImages || 1}개 필요` } }, { status: 400 });
}
}
const post = await prisma.post.create({
data: {
boardId,

View File

@@ -2,20 +2,22 @@
import { useState } from "react";
import { useToast } from "@/app/components/ui/ToastProvider";
export function UploadButton({ onUploaded }: { onUploaded: (url: string) => void }) {
export function UploadButton({ onUploaded, multiple = false }: { onUploaded: (url: string) => void; multiple?: boolean }) {
const { show } = useToast();
const [loading, setLoading] = useState(false);
async function onChange(e: React.ChangeEvent<HTMLInputElement>) {
const f = e.target.files?.[0];
if (!f) return;
const fd = new FormData();
fd.append("file", f);
const files = Array.from(e.target.files ?? []);
if (files.length === 0) return;
try {
setLoading(true);
const r = await fetch("/api/uploads", { method: "POST", body: fd });
const data = await r.json();
if (!r.ok) throw new Error(JSON.stringify(data));
onUploaded(data.url);
for (const f of files) {
const fd = new FormData();
fd.append("file", f);
const r = await fetch("/api/uploads", { method: "POST", body: fd });
const data = await r.json();
if (!r.ok) throw new Error(JSON.stringify(data));
onUploaded(data.url);
}
show("업로드 완료");
} catch (e) {
show("업로드 실패");
@@ -26,7 +28,7 @@ export function UploadButton({ onUploaded }: { onUploaded: (url: string) => void
}
return <label style={{ display: "inline-block" }}>
<span style={{ padding: "6px 10px", border: "1px solid #ddd", borderRadius: 6, cursor: "pointer" }}>{loading ? "업로드 중..." : "이미지 업로드"}</span>
<input type="file" accept="image/*" onChange={onChange} style={{ display: "none" }} />
<input type="file" accept="image/*" onChange={onChange} style={{ display: "none" }} multiple={multiple} />
</label>;
}

View File

@@ -33,7 +33,7 @@ export default function NewPostPage({ searchParams }: { searchParams?: { boardId
<input placeholder="boardId" value={form.boardId} onChange={(e) => setForm({ ...form, boardId: e.target.value })} />
<input placeholder="제목" value={form.title} onChange={(e) => setForm({ ...form, title: e.target.value })} />
<textarea placeholder="내용" value={form.content} onChange={(e) => setForm({ ...form, content: e.target.value })} rows={10} />
<UploadButton onUploaded={(url) => setForm((f) => ({ ...f, content: `${f.content}\n![image](${url})` }))} />
<UploadButton multiple onUploaded={(url) => setForm((f) => ({ ...f, content: `${f.content}\n![image](${url})` }))} />
<button disabled={loading} onClick={submit}>{loading ? "저장 중..." : "등록"}</button>
</div>
);