import { defineEventHandler, getHeader, getRequestURL, getRouterParam, proxyRequest } from 'h3' import { useRuntimeConfig } from '#imports' import { getTenantFromContext } from '../../utils/tenant' function joinURL(base: string, path: string) { if (!path) return base return base.replace(/\/+$/, '') + '/' + path.replace(/^\/+/, '') } /** * 文件代理 * /api/file/[...path] → SaaS 后端文件服务器 * 隐藏真实文件服务器地址 */ export default defineEventHandler((event) => { const config = useRuntimeConfig() const ctx = getTenantFromContext(event, config) const fileServerBase = config.public.fileServerBase as string const path = getRouterParam(event, 'path') || '' const search = getRequestURL(event).search const target = joinURL(fileServerBase, path) + search const authorization = getHeader(event, 'authorization') return proxyRequest(event, target, { headers: { TenantId: ctx.tenantId, ...(authorization ? { Authorization: authorization } : {}) } }) })