@@ -32,14 +32,13 @@ if ( !fs.existsSync ) {
32
32
33
33
exports . get_routes = [
34
34
{ path :'/' , handler :'index_handler' } ,
35
- { path : / ^ \/ e x p o r t \/ d o w n l o a d \/ ( \w + \. z i p ) $ / , handler :'export_download_handler' }
35
+ { path : / ^ \/ e x p o r t \/ d o w n l o a d \/ ( \w + ) \. z i p $ / , handler :'export_download_handler' }
36
36
] ;
37
37
38
38
39
39
exports . post_routes = [
40
40
{ path : '/api/app/create' , handler :'api_app_create_handler' } ,
41
41
{ path : / ^ \/ a p i \/ a p p \/ r e m o v e \/ ( \w + ) $ / , handler :'api_app_remove_handler' } ,
42
- { path : / ^ \/ a p i \/ a p p \/ e x p o r t \/ ( \w + ) $ / , handler :'api_app_export_handler' } ,
43
42
{ path : / ^ \/ a p i \/ a p p \/ i m p o r t $ / , handler :'api_app_import_handler' }
44
43
] ;
45
44
@@ -180,10 +179,9 @@ exports.api_app_create_handler = function( app, req, res ) {
180
179
} ;
181
180
182
181
exports . export_download_handler = function ( app , req , res , pathmatches ) {
183
-
184
- var exportname = "export.zip" ;
182
+ var appname ;
185
183
if ( pathmatches && pathmatches [ 1 ] !== "" ) {
186
- exportname = pathmatches [ 1 ] ;
184
+ appname = pathmatches [ 1 ] ;
187
185
} else {
188
186
res . json ( {
189
187
status : 'error' ,
@@ -192,19 +190,36 @@ exports.export_download_handler = function( app, req, res, pathmatches ) {
192
190
return ;
193
191
}
194
192
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 ) ;
201
210
} ) ;
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
+ } ) ;
208
223
} ;
209
224
210
225
@@ -341,94 +356,6 @@ exports.api_app_import_handler = function( app, req, res, pathmatches ) {
341
356
342
357
343
358
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
-
432
359
} ;
433
360
434
361
exports . api_app_remove_handler = function ( app , req , res , pathmatches ) {
0 commit comments