260825_02_ENH: 파일 접근 next.js를 통해서 호출 되도록 수정

This commit is contained in:
2026-08-25 15:16:26 +09:00
parent 75385bd452
commit c08bc5a808
6 changed files with 39 additions and 7 deletions

View File

@@ -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

View File

@@ -1,4 +1,3 @@
NEXT_PUBLIC_ENV=local
ENV=local
API_SERVER=http://localhost:6107/api/v1
NEXT_PUBLIC_FILE_ORIGIN=http://localhost:6107

View File

@@ -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

View File

@@ -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")! }
: {}),
},
});
}

View File

@@ -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),
});

View File

@@ -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 등은 새 탭에 그냥 열려버린다.