Skip to content

Commit 47fa325

Browse files
committed
Make export work with the new app structure.
1 parent 30fe399 commit 47fa325

File tree

2 files changed

+33
-111
lines changed

2 files changed

+33
-111
lines changed

coder-apps/common/coder/app/app.js

Lines changed: 32 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ if ( !fs.existsSync ) {
3232

3333
exports.get_routes = [
3434
{ path:'/', handler:'index_handler' },
35-
{ path: /^\/export\/download\/(\w+\.zip)$/, handler:'export_download_handler' }
35+
{ path: /^\/export\/download\/(\w+)\.zip$/, handler:'export_download_handler' }
3636
];
3737

3838

3939
exports.post_routes = [
4040
{ path: '/api/app/create', handler:'api_app_create_handler' },
4141
{ path: /^\/api\/app\/remove\/(\w+)$/, handler:'api_app_remove_handler' },
42-
{ path: /^\/api\/app\/export\/(\w+)$/, handler:'api_app_export_handler' },
4342
{ path: /^\/api\/app\/import$/, handler:'api_app_import_handler' }
4443
];
4544

@@ -180,10 +179,9 @@ exports.api_app_create_handler = function( app, req, res ) {
180179
};
181180

182181
exports.export_download_handler = function( app, req, res, pathmatches ) {
183-
184-
var exportname = "export.zip";
182+
var appname;
185183
if ( pathmatches && pathmatches[1] !== "" ) {
186-
exportname = pathmatches[1];
184+
appname = pathmatches[1];
187185
} else {
188186
res.json({
189187
status: 'error',
@@ -192,19 +190,36 @@ exports.export_download_handler = function( app, req, res, pathmatches ) {
192190
return;
193191
}
194192

195-
var exportfile = 'appexport.zip';
196-
var path = process.cwd();
197-
if ( !fs.existsSync( path + '/tmp/' + exportfile ) ) {
198-
res.json({
199-
status: "error",
200-
error: "Export file doesn't exist"
193+
res.contentType('zip');
194+
res.setHeader('Content-disposition', 'attachment; filename='+ appname + '.zip');
195+
196+
var path = process.cwd() + "/apps/" + appname;
197+
198+
fs.exists(path, function(exists) {
199+
if (!exists) {
200+
res.send(404);
201+
return;
202+
}
203+
204+
// Options -r recursive - redirect to stdout
205+
var zip = spawn('zip', ['-r', '-', '.'], {cwd: path});
206+
207+
// Keep writing stdout to res
208+
zip.stdout.on('data', function (data) {
209+
res.write(data);
201210
});
202-
return;
203-
}
204-
205-
res.download( path + '/tmp/' + exportfile, exportname );
206-
207-
211+
212+
// End the response on zip exit
213+
zip.on('exit', function (code) {
214+
if(code !== 0) {
215+
res.statusCode = 500;
216+
console.log('zip process exited with code ' + code);
217+
res.end();
218+
} else {
219+
res.end();
220+
}
221+
});
222+
});
208223
};
209224

210225

@@ -341,94 +356,6 @@ exports.api_app_import_handler = function( app, req, res, pathmatches ) {
341356

342357

343358

344-
};
345-
346-
exports.api_app_export_handler = function( app, req, res, pathmatches ) {
347-
348-
var apptoexport = "";
349-
if ( pathmatches && pathmatches[1] !== "" ) {
350-
apptoexport = pathmatches[1];
351-
} else {
352-
res.json({
353-
status: 'error',
354-
error: 'invalid parameters'
355-
});
356-
return;
357-
}
358-
359-
var path = process.cwd();
360-
if ( !fs.existsSync( path + '/apps/' + apptoexport + '/app.js' ) ) {
361-
res.json({
362-
status: "error",
363-
error: "Application doesn't exist"
364-
});
365-
return;
366-
}
367-
368-
var success = true;
369-
var exportkey = 'appexport'; //TODO: maybe this should be random and auto-cleaned
370-
var tmpfolder = path + '/tmp/' + exportkey;
371-
try { forceRemoveDir( tmpfolder ); } catch (e) {}
372-
try { fs.unlinkSync( path + '/tmp/' + exportkey + '.zip' ); } catch (e) {}
373-
try { fs.mkdirSync( tmpfolder ); } catch (e) { success = false; }
374-
try { fs.mkdirSync( tmpfolder + '/app' ); } catch (e) { success = false; }
375-
try { fs.mkdirSync( tmpfolder + '/static' ); } catch (e) { success = false; }
376-
try { fs.mkdirSync( tmpfolder + '/static/css' ); } catch (e) { success = false; }
377-
try { fs.mkdirSync( tmpfolder + '/static/js' ); } catch (e) { success = false; }
378-
try { fs.mkdirSync( tmpfolder + '/static/media' ); } catch (e) { success = false; }
379-
try { fs.mkdirSync( tmpfolder + '/views' ); } catch (e) { success = false; }
380-
381-
if ( !success ) {
382-
res.json({
383-
status: "error",
384-
error: "Cannot create export directory."
385-
});
386-
return;
387-
}
388-
389-
390-
//app node.js file
391-
copyFile( path + '/apps/' + apptoexport + '/app.js', tmpfolder + '/app/app.js' );
392-
//app meta.json file
393-
copyFile( path + '/apps/' + apptoexport + '/meta.json', tmpfolder + '/app/meta.json' );
394-
//html view
395-
copyFile( path + '/views/apps/' + apptoexport + '/index.html', tmpfolder + '/views/index.html' );
396-
//css data
397-
copyFile( path + '/static/apps/' + apptoexport + '/css/index.css', tmpfolder + '/static/css/index.css' );
398-
//index.js file
399-
copyFile( path + '/static/apps/' + apptoexport + '/js/index.js', tmpfolder + '/static/js/index.js' );
400-
401-
var mediadir = path + "/static/apps/" + apptoexport + "/media/";
402-
var mediafiles = fs.readdirSync( mediadir );
403-
for ( var x in mediafiles ) {
404-
var filename = mediafiles[x];
405-
var info = fs.statSync( mediadir + filename );
406-
if ( typeof info !== 'undefined' && info && info.isFile() ) {
407-
copyFile( mediadir + filename, tmpfolder + '/static/media/' + filename );
408-
}
409-
}
410-
411-
var zip = spawn('zip', ['-r', '../appexport.zip', '.', '-i', '*'], { cwd: tmpfolder });
412-
zip.stdout.on('data', function (data) {
413-
//console.log('coder::api_app_export_handler zip: ' + data );
414-
});
415-
zip.stderr.on('data', function (data) {
416-
//console.log('coder::api_app_export_handler zip error: ' + data );
417-
});
418-
zip.on('exit', function (code) {
419-
if(code !== 0) {
420-
res.json({
421-
status: "error",
422-
error: "zip error: " + code
423-
});
424-
} else {
425-
res.json({
426-
status: "success",
427-
file: apptoexport + ".zip"
428-
});
429-
}
430-
});
431-
432359
};
433360

434361
exports.api_app_remove_handler = function( app, req, res, pathmatches ) {

coder-apps/common/editor/static/js/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,7 @@ var downloadURL = function downloadURL(url) {
149149

150150

151151
var exportAppClicked = function() {
152-
153-
$.post( '/app/coder/api/app/export/' + edit_appname, function( d ) {
154-
if ( d.status == 'success' ) {
155-
downloadURL( '/app/coder/export/download/' + d.file );
156-
}
157-
});
152+
downloadURL( '/app/coder/export/download/' + edit_appname + '.zip' );
158153
};
159154

160155

0 commit comments

Comments
 (0)