Skip to content

Commit

Permalink
Merge pull request #253 from rajnishdargan/main
Browse files Browse the repository at this point in the history
Issue #PS-2194 feat: Fix content-upload and asset upload in admin portal
  • Loading branch information
itsvick authored Oct 14, 2024
2 parents 2a4c1cd + 455bcea commit b68f433
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
17 changes: 8 additions & 9 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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*",
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
},
];
},
Expand Down
4 changes: 2 additions & 2 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default function Document() {
<Head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@project-sunbird/sunbird-questionset-editor-web-component/styles.css"
href="https://cdn.jsdelivr.net/npm/@tekdi/sunbird-questionset-editor-web-component@3.0.1/styles.css"
/>
<script
src="https://cdn.jsdelivr.net/npm/@project-sunbird/sunbird-questionset-editor-web-component/sunbird-questionset-editor.js"
src="https://cdn.jsdelivr.net/npm/@tekdi/sunbird-questionset-editor-web-component@3.0.1/sunbird-questionset-editor.js"
async
></script>
</Head>
Expand Down
56 changes: 56 additions & 0 deletions src/pages/api/fileUpload.ts
Original file line number Diff line number Diff line change
@@ -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<Buffer>((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`);
}
}

0 comments on commit b68f433

Please sign in to comment.