diff --git a/nginx.conf b/nginx.conf index 386d862..940b882 100644 --- a/nginx.conf +++ b/nginx.conf @@ -5,6 +5,12 @@ server { # 같은 도메인의 /api 요청을 NAS 백엔드(18080)로 전달 → 브라우저는 CORS 없이 호출 location /api/ { + # 자료실 파일 업로드: nginx 기본 1MB 제한 → 백엔드(multipart 5GB)와 맞춤 + # 버퍼링 끔: 큰 파일을 nginx 디스크에 다 받은 뒤 넘기지 않고 바로 백엔드로 흘려보냄 + client_max_body_size 5g; + proxy_request_buffering off; + proxy_read_timeout 600s; + proxy_send_timeout 600s; proxy_pass http://192.168.0.215:18080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/src/features/bbs/api.ts b/src/features/bbs/api.ts index ad861fb..0663c93 100644 --- a/src/features/bbs/api.ts +++ b/src/features/bbs/api.ts @@ -67,6 +67,36 @@ export const createMediaPost = (token: string, url: string, title: string, categ body: JSON.stringify({ url, title, category }), }); +/** + * 내 PC 파일 업로드 → 바로 완료된 글로 등록. onProgress: 보낸 비율(0~100) + * fetch는 업로드 진행률을 알려주지 않아서 XMLHttpRequest 사용 + */ +export const uploadMediaFiles = (token: string, files: File[], title: string, category: string, onProgress: (percent: number) => void) => + new Promise((resolve, reject) => { + const body = new FormData(); + files.forEach((f) => body.append("files", f)); + body.append("title", title); + body.append("category", category); + const xhr = new XMLHttpRequest(); + xhr.open("POST", `${API_URL}/api/media-posts/upload`); + xhr.setRequestHeader("Authorization", `Bearer ${token}`); + xhr.upload.onprogress = (e) => e.lengthComputable && onProgress(Math.floor((e.loaded / e.total) * 100)); + xhr.onerror = () => reject(new Error("※ 업로드 중 연결이 끊어졌습니다.")); + xhr.onload = () => { + if (xhr.status === 401 || xhr.status === 403) return reject(new AuthError("※ 로그인이 만료되었습니다. 다시 로그인하세요.")); + if (xhr.status === 413) return reject(new Error("※ 파일이 너무 큽니다.")); + if (xhr.status < 200 || xhr.status >= 300) { + let reason: string | undefined; + try { + reason = JSON.parse(xhr.responseText)?.error; + } catch {} + return reject(new Error(reason ? `※ ${reason}` : `※ 서버 오류 (${xhr.status})`)); + } + resolve(); + }; + xhr.send(body); + }); + const PAGE_SIZE = 30; /** diff --git a/src/features/bbs/components/MediaDetail.tsx b/src/features/bbs/components/MediaDetail.tsx index 0f8009d..f836cd4 100644 --- a/src/features/bbs/components/MediaDetail.tsx +++ b/src/features/bbs/components/MediaDetail.tsx @@ -168,10 +168,16 @@ export default function MediaDetail({ id, token, categories, onError, onDeleted, )}
- - 원본 보기 ↗ - {" "} - · L=목록 · M=메인 ·{" "} + {/* 업로드한 글은 원본 링크가 없음 (sourceUrl = "") */} + {post.sourceUrl && ( + <> + + 원본 보기 ↗ + {" "} + ·{" "} + + )} + L=목록 · M=메인 ·{" "} {confirming ? ( <> 파일까지 지워집니다. 정말 삭제할까요?{" "} diff --git a/src/features/bbs/components/MediaList.tsx b/src/features/bbs/components/MediaList.tsx index 43cefe3..a63d833 100644 --- a/src/features/bbs/components/MediaList.tsx +++ b/src/features/bbs/components/MediaList.tsx @@ -1,5 +1,5 @@ import { useState, type FormEvent } from "react"; -import { createMediaPost } from "../api"; +import { createMediaPost, uploadMediaFiles } from "../api"; import type { MediaPost, MediaQuery } from "../types"; import DatePicker from "./DatePicker"; import { MoreSentinel } from "./PostList"; @@ -33,6 +33,7 @@ const EMPTY_SEARCH = { q: "", from: "", to: "" }; export default function MediaList(props: Props) { const { posts, total, last, onMore, categories, query, token, onSelect, onQuery, onCreated, onError } = props; const [busy, setBusy] = useState(false); + const [uploaded, setUploaded] = useState(null); // 업로드 중이면 보낸 비율(%) const tabs = [{ key: null, name: "전체" }, ...categories.map((c) => ({ key: c, name: c })), { key: "", name: "미분류" }]; const searching = query.q || query.from || query.to; @@ -41,20 +42,27 @@ export default function MediaList(props: Props) { if (!token) return; const form = e.currentTarget; const data = new FormData(form); + const url = String(data.get("url")).trim(); + const title = String(data.get("title")).trim(); + const category = String(data.get("category")).trim(); + // 빈 파일 칸도 FormData에는 이름 없는 File로 들어옴 → 이름 있는 것만 + const files = data.getAll("files").filter((f): f is File => f instanceof File && f.name !== ""); + if (!url && files.length === 0) { + onError(new Error("※ 링크를 넣거나 파일을 고르세요.")); + return; + } setBusy(true); try { - await createMediaPost( - token, - String(data.get("url")).trim(), - String(data.get("title")).trim(), - String(data.get("category")).trim(), - ); + // 파일을 골랐으면 업로드, 아니면 링크 다운로드 + if (files.length > 0) await uploadMediaFiles(token, files, title, category, setUploaded); + else await createMediaPost(token, url, title, category); form.reset(); onCreated(); } catch (err) { onError(err); } finally { setBusy(false); + setUploaded(null); } }; @@ -85,7 +93,12 @@ export default function MediaList(props: Props) {
+