diff --git a/.env.development b/.env.development index 2a94de4..863cf89 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,3 @@ NEXT_PUBLIC_ENV=development ENV=development API_SERVER=http://127.0.0.1:6107/api/v1 -NEXT_PUBLIC_FILE_ORIGIN=http://127.0.0.1:6107 diff --git a/.env.local b/.env.local index 7ac7c80..23141e3 100644 --- a/.env.local +++ b/.env.local @@ -1,4 +1,3 @@ NEXT_PUBLIC_ENV=local ENV=local API_SERVER=http://localhost:6107/api/v1 -NEXT_PUBLIC_FILE_ORIGIN=http://localhost:6107 diff --git a/.env.production b/.env.production index 82a8f44..b67ccee 100644 --- a/.env.production +++ b/.env.production @@ -1,4 +1,3 @@ NEXT_PUBLIC_ENV=production ENV=production API_SERVER=http://127.0.0.1:6107/api/v1 -NEXT_PUBLIC_FILE_ORIGIN=http://127.0.0.1:6107 diff --git a/src/app/api/files/[...slug]/route.ts b/src/app/api/files/[...slug]/route.ts new file mode 100644 index 0000000..60e7bfc --- /dev/null +++ b/src/app/api/files/[...slug]/route.ts @@ -0,0 +1,36 @@ +import { cookies } from "next/headers"; +import { NextRequest, NextResponse } from "next/server"; +import { serverEnv } from "@/lib/env.server"; +import { CookieKeys } from "@/store/cookie/cookie-keys"; +import type { RouteContext } from "@/types/common"; + +// 파일 서버(백엔드와 동일 호스트)가 테스트/운영 환경에서 내부망 전용이라 브라우저가 직접 접근할 수 없다. +// 내부망에 접근 가능한 건 이 Next.js 서버뿐이므로, 여기서 대신 받아 그대로 스트리밍해서 전달한다. +export async function GET(request: NextRequest, context: RouteContext) { + const { slug = [] } = await context.params; + if (!slug.length) return new NextResponse(null, { status: 404 }); + + const fileOrigin = new URL(serverEnv.API_SERVER).origin; + const targetUrl = `${fileOrigin}/files/${slug.map(encodeURIComponent).join("/")}${request.nextUrl.search}`; + + const cookieStore = await cookies(); + const accessToken = cookieStore.get(CookieKeys.AuthToken)?.value; + + const backendResponse = await fetch(targetUrl, { + headers: accessToken ? { authorization: `Bearer ${accessToken}` } : undefined, + }); + + if (!backendResponse.ok || !backendResponse.body) { + return new NextResponse(null, { status: backendResponse.status }); + } + + return new NextResponse(backendResponse.body, { + status: backendResponse.status, + headers: { + "content-type": backendResponse.headers.get("content-type") ?? "application/octet-stream", + ...(backendResponse.headers.get("content-length") + ? { "content-length": backendResponse.headers.get("content-length")! } + : {}), + }, + }); +} diff --git a/src/lib/env.client.ts b/src/lib/env.client.ts index a532436..4e8b9c8 100644 --- a/src/lib/env.client.ts +++ b/src/lib/env.client.ts @@ -5,5 +5,4 @@ export const clientEnv = createEnv({ API_BASE: env.required("NEXT_PUBLIC_API_BASE", process.env.NEXT_PUBLIC_API_BASE), STORE_KEY: env.required("NEXT_PUBLIC_STORE_KEY", process.env.NEXT_PUBLIC_STORE_KEY), STORE_IV: env.required("NEXT_PUBLIC_STORE_IV", process.env.NEXT_PUBLIC_STORE_IV), - FILE_ORIGIN: env.required("NEXT_PUBLIC_FILE_ORIGIN", process.env.NEXT_PUBLIC_FILE_ORIGIN), }); diff --git a/src/lib/file-download.ts b/src/lib/file-download.ts index ecbaf66..213ee6f 100644 --- a/src/lib/file-download.ts +++ b/src/lib/file-download.ts @@ -1,7 +1,7 @@ -import { clientEnv } from "@/lib/env.client"; - +// 백엔드(파일 서버)가 테스트/운영 환경에서 내부망 전용 주소라 브라우저가 직접 접근할 수 없다. +// 프론트 자기 origin의 BFF 프록시(src/app/api/files/[...slug]/route.ts)를 거쳐 내려받는다. export function toFileDownloadUrl(downloadUrl: string): string { - return `${clientEnv.FILE_ORIGIN}${downloadUrl}`; + return `/api${downloadUrl}`; } // 백엔드가 Content-Disposition: attachment를 내려주지 않아 PDF 등은 새 탭에 그냥 열려버린다.