diff --git a/next.config.mjs b/next.config.mjs index 036ed39e..196635c4 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -19,7 +19,6 @@ const routes = { GENERAL: { CONTENT_PREVIEW: "/content/preview/:path*", CONTENT_PLUGINS: "/content-plugins/:path*", - ASSET_PUBLIC: "/assets/public/:path*", GENERIC_EDITOR: "/generic-editor/:path*", CONTENT_EDITOR: "/editor/content/:path*", ASSET_IMAGE: "/assets/images/:path*", @@ -48,15 +47,15 @@ const nextConfig = { return [ { source: "/action/asset/v1/upload/:identifier*", // Match asset upload routes - destination: `${process.env.WORKSPACE_BASE_URL}/api/fileUpload`, // Forward asset uploads to fileUpload.js + destination: '/api/fileUpload' // Forward asset uploads to fileUpload proxy }, { source: "/action/content/v3/upload/url/:identifier*", // Match content upload with 'url' in the path destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/action/content/v3/upload/url/:identifier*`, // Forward to proxy route with path as query param }, { - source: "/action/content/v3/upload/:identifier*", // Match content upload routes - destination: `${process.env.WORKSPACE_BASE_URL}/api/fileUpload`, // Forward content uploads to fileUpload.js + source: '/action/content/v3/upload/:identifier*', // Match content upload routes + destination: '/api/fileUpload', // Forward asset uploads to fileUpload proxy }, { source: "/action/asset/:path*", // Match other /action/asset routes @@ -74,6 +73,10 @@ const nextConfig = { source: "/api/:path*", // Match /api/ routes destination: `${process.env.WORKSPACE_BASE_URL}/api/proxy?path=/api/:path*`, // Forward them to proxy.js }, + { + source: '/assets/public/:path*', // Match any URL starting with /assets/public/ + destination: `${process.env.WORKSPACE_BASE_URL}/assets/public/:path*`, // Forward to workspace proxy + }, { source: routes.API.GENERAL.CONTENT_PREVIEW, destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.CONTENT_PREVIEW}`, // Proxy to portal @@ -82,10 +85,6 @@ const nextConfig = { source: routes.API.GENERAL.CONTENT_PLUGINS, destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.CONTENT_PLUGINS}`, // Proxy to portal }, - { - source: routes.API.GENERAL.ASSET_PUBLIC, - destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.ASSET_PUBLIC}`, // Proxy to portal - }, { source: routes.API.GENERAL.GENERIC_EDITOR, destination: `${PORTAL_BASE_URL}${routes.API.GENERAL.GENERIC_EDITOR}`, // Proxy to portal @@ -100,7 +99,7 @@ const nextConfig = { }, { source: "/app/telemetry", // Match telemetry route - destination: "/api/telemetry", // Redirect to telemetry proxy + destination: `${process.env.WORKSPACE_BASE_URL}/api/telemetry`, // Redirect to telemetry proxy }, ]; }, diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index c5f5fea9..5779813f 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -7,10 +7,10 @@ export default function Document() { diff --git a/src/pages/api/fileUpload.ts b/src/pages/api/fileUpload.ts new file mode 100644 index 00000000..635dc738 --- /dev/null +++ b/src/pages/api/fileUpload.ts @@ -0,0 +1,56 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +export const config = { + api: { + bodyParser: false, // Disable body parsing so we can handle it manually + }, +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + console.log('req.url ====>', req.url) + if (req.method === 'POST') { + try { + // Create a new Headers object to ensure valid header types + const headers = new Headers(); + + // Forward the incoming request headers, filtering out invalid headers + for (const [key, value] of Object.entries(req.headers)) { + // Exclude 'content-length' as it can cause issues + if (key.toLowerCase() !== 'content-length') { + headers.append(key, value as string); + } + } + + // Read the request body as a stream + const body = await new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + req.on('data', (chunk) => { + chunks.push(Buffer.from(chunk)); + }); + req.on('end', () => { + resolve(Buffer.concat(chunks)); + }); + req.on('error', (err) => { + reject(err); + }); + }); + + // Forward the request to the middleware API + const response = await fetch(`${process.env.WORKSPACE_BASE_URL}` + `${req.url}`, { + method: 'POST', + headers: headers, // Use the new Headers object + body, // Pass the request body as a Buffer + }); + + // Forward the middleware response back to the client + const data = await response.json(); + res.status(response.status).json(data); + } catch (error) { + console.error('Error proxying request:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } + } else { + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +}