-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Download Projects, Deploy to Vercel + Confetti Celebrations #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ import { WorkspaceService } from './services/workspace'; | |
| import { ClineService } from './services/cline'; | ||
| import fs from 'fs'; | ||
| import mongoose from "mongoose"; | ||
| import archiver from "archiver"; | ||
|
|
||
|
|
||
| dotenv.config(); | ||
|
|
||
|
|
@@ -157,6 +159,47 @@ app.get("/api/projects/:projectId/files", async (req, res) => { | |
| } | ||
| }); | ||
|
|
||
| // Download Route | ||
| app.get("/api/projects/:projectId/download", async (req, res) => { | ||
| try { | ||
| const { projectId } = req.params; | ||
| // Verify project exists | ||
| const project = await Project.findById(projectId); | ||
| if (!project) { | ||
| return res.status(404).json({ error: "Project not found" }); | ||
| } | ||
|
|
||
| const repoPath = WorkspaceService.getRepoPath(projectId); | ||
| if (!fs.existsSync(repoPath)) { | ||
| return res.status(404).json({ error: "No code generated yet" }); | ||
| } | ||
|
|
||
| res.setHeader('Content-Type', 'application/zip'); | ||
| res.setHeader('Content-Disposition', `attachment; filename="${project.name.replace(/[^a-z0-9]/gi, '_').toLowerCase() || 'project'}.zip"`); | ||
|
|
||
| const archive = archiver('zip', { | ||
| zlib: { level: 9 } // Sets the compression level. | ||
| }); | ||
|
|
||
| archive.on('error', function (err) { | ||
| console.error("Archiver error:", err); | ||
| res.status(500).send({ error: err.message }); | ||
| }); | ||
|
|
||
| // pipe archive data to the file | ||
| archive.pipe(res); | ||
|
|
||
| // append files from a sub-directory, putting its contents at the root of archive | ||
| archive.directory(repoPath, false); | ||
|
|
||
| archive.finalize(); | ||
|
|
||
| } catch (error) { | ||
| console.error("Download failed:", error); | ||
| res.status(500).json({ error: "Download failed" }); | ||
| } | ||
| }); | ||
|
Comment on lines
+162
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Streaming error handling is unsafe after - archive.on('error', function (err) {
- console.error("Archiver error:", err);
- res.status(500).send({ error: err.message });
- });
+ archive.on('error', (err) => {
+ console.error("Archiver error:", err);
+ // response is a stream at this point; don't try to write JSON/status reliably
+ res.destroy(err);
+ });
+
+ req.on('close', () => {
+ if (!res.writableEnded) archive.abort();
+ });
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @M-DEV-1, thanks for the acknowledgment! Let me know if you'd like any clarification on the suggested approach for handling stream errors more safely. |
||
|
|
||
| // Preview Route (Static File Serving) | ||
| app.get("/preview/:projectId/*", (req, res) => { | ||
| const { projectId } = req.params; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.