Migrate to Vercel Sandbox using OIDC
This commit is contained in:
+36
-37
@@ -15,41 +15,37 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
console.log('[create-zip] Creating project zip...');
|
||||
|
||||
// Create zip file in sandbox
|
||||
const result = await global.activeSandbox.runCode(`
|
||||
import zipfile
|
||||
import os
|
||||
import json
|
||||
|
||||
os.chdir('/home/user/app')
|
||||
|
||||
# Create zip file
|
||||
with zipfile.ZipFile('/tmp/project.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for root, dirs, files in os.walk('.'):
|
||||
# Skip node_modules and .git
|
||||
dirs[:] = [d for d in dirs if d not in ['node_modules', '.git', '.next', 'dist']]
|
||||
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
arcname = os.path.relpath(file_path, '.')
|
||||
zipf.write(file_path, arcname)
|
||||
|
||||
# Get file size
|
||||
file_size = os.path.getsize('/tmp/project.zip')
|
||||
print(f" Created project.zip ({file_size} bytes)")
|
||||
`);
|
||||
// Create zip file in sandbox using standard commands
|
||||
const zipResult = await global.activeSandbox.runCommand({
|
||||
cmd: 'bash',
|
||||
args: ['-c', `zip -r /tmp/project.zip . -x "node_modules/*" ".git/*" ".next/*" "dist/*" "build/*" "*.log"`]
|
||||
});
|
||||
|
||||
if (zipResult.exitCode !== 0) {
|
||||
const error = await zipResult.stderr();
|
||||
throw new Error(`Failed to create zip: ${error}`);
|
||||
}
|
||||
|
||||
const sizeResult = await global.activeSandbox.runCommand({
|
||||
cmd: 'bash',
|
||||
args: ['-c', `ls -la /tmp/project.zip | awk '{print $5}'`]
|
||||
});
|
||||
|
||||
const fileSize = await sizeResult.stdout();
|
||||
console.log(`[create-zip] Created project.zip (${fileSize.trim()} bytes)`);
|
||||
|
||||
// Read the zip file and convert to base64
|
||||
const readResult = await global.activeSandbox.runCode(`
|
||||
import base64
|
||||
|
||||
with open('/tmp/project.zip', 'rb') as f:
|
||||
content = f.read()
|
||||
encoded = base64.b64encode(content).decode('utf-8')
|
||||
print(encoded)
|
||||
`);
|
||||
const readResult = await global.activeSandbox.runCommand({
|
||||
cmd: 'base64',
|
||||
args: ['/tmp/project.zip']
|
||||
});
|
||||
|
||||
const base64Content = readResult.logs.stdout.join('').trim();
|
||||
if (readResult.exitCode !== 0) {
|
||||
const error = await readResult.stderr();
|
||||
throw new Error(`Failed to read zip file: ${error}`);
|
||||
}
|
||||
|
||||
const base64Content = (await readResult.stdout()).trim();
|
||||
|
||||
// Create a data URL for download
|
||||
const dataUrl = `data:application/zip;base64,${base64Content}`;
|
||||
@@ -57,15 +53,18 @@ with open('/tmp/project.zip', 'rb') as f:
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
dataUrl,
|
||||
fileName: 'e2b-project.zip',
|
||||
fileName: 'vercel-sandbox-project.zip',
|
||||
message: 'Zip file created successfully'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('[create-zip] Error:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: (error as Error).message
|
||||
}, { status: 500 });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: (error as Error).message
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user