From 14a5fb09478f92c6ab2705f857e1217de143f1eb Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Thu, 13 Nov 2025 04:08:19 +0000 Subject: [PATCH] Create course download endpoint cgen-ebe12d6531284dc08ffd8515ce9f5cd6 --- api/courses/download.ts | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 api/courses/download.ts diff --git a/api/courses/download.ts b/api/courses/download.ts new file mode 100644 index 00000000..f13b7cba --- /dev/null +++ b/api/courses/download.ts @@ -0,0 +1,68 @@ +import fs from 'fs'; +import path from 'path'; + +export default async function handler(req: Request) { + const url = new URL(req.url); + const courseSlug = url.searchParams.get('course'); + const format = url.searchParams.get('format') || 'markdown'; // markdown, pdf, code + + if (!courseSlug) { + return new Response(JSON.stringify({ error: 'Missing course slug' }), { + status: 400, + headers: { 'Content-Type': 'application/json' }, + }); + } + + try { + const filePath = path.join( + process.cwd(), + 'public', + 'courses', + `${courseSlug}.${format === 'markdown' ? 'md' : format}` + ); + + // Check if file exists + if (!fs.existsSync(filePath)) { + return new Response( + JSON.stringify({ error: 'Course material not found' }), + { + status: 404, + headers: { 'Content-Type': 'application/json' }, + } + ); + } + + // Read file + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const fileSize = Buffer.byteLength(fileContent); + + // Determine content type + const contentTypes: Record = { + markdown: 'text/markdown; charset=utf-8', + pdf: 'application/pdf', + code: 'text/plain; charset=utf-8', + }; + + return new Response(fileContent, { + status: 200, + headers: { + 'Content-Type': contentTypes[format] || 'application/octet-stream', + 'Content-Length': fileSize.toString(), + 'Content-Disposition': `attachment; filename="${courseSlug}-guide.${format === 'markdown' ? 'md' : format}"`, + 'Cache-Control': 'public, max-age=3600', + }, + }); + } catch (error: any) { + console.error('Download error:', error); + return new Response( + JSON.stringify({ + error: 'Failed to download course material', + message: error.message, + }), + { + status: 500, + headers: { 'Content-Type': 'application/json' }, + } + ); + } +}