@@ -22,6 +22,8 @@ var mustache = require('mustache');
22
22
var util = require ( 'util' ) ;
23
23
var fs = require ( 'fs' ) ;
24
24
var spawn = require ( 'child_process' ) . spawn ;
25
+ var tmp = require ( 'tmp' ) ;
26
+ var async = require ( 'async' ) ;
25
27
26
28
//hack to make fs.existsSync work between different node versions
27
29
if ( ! fs . existsSync ) {
@@ -38,7 +40,7 @@ exports.get_routes = [
38
40
exports . post_routes = [
39
41
{ path : '/api/app/create' , handler :'api_app_create_handler' } ,
40
42
{ path : / ^ \/ a p i \/ a p p \/ r e m o v e \/ ( \w + ) $ / , handler :'api_app_remove_handler' } ,
41
- { path : / ^ \/ a p i \ /a p p \ /i m p o r t $ / , handler :'api_app_import_handler' }
43
+ { path : "/ api/app/import" , handler :'api_app_import_handler' }
42
44
] ;
43
45
44
46
exports . on_destroy = function ( ) {
@@ -184,7 +186,7 @@ exports.export_download_handler = function( app, req, res, pathmatches ) {
184
186
} ;
185
187
186
188
187
- exports . api_app_import_handler = function ( app , req , res , pathmatches ) {
189
+ exports . api_app_import_handler = function ( app , req , res ) {
188
190
189
191
if ( ! req . files || ! req . files [ 'import_file' ] ) {
190
192
res . json ( {
@@ -202,122 +204,82 @@ exports.api_app_import_handler = function( app, req, res, pathmatches ) {
202
204
return ;
203
205
}
204
206
205
- var path = process . cwd ( ) ;
206
- var success = true ;
207
- var importkey = 'appimport' ; //TODO: maybe this should be random and auto-cleaned
208
- var tmpfolder = path + '/tmp/' + importkey ;
209
- try { forceRemoveDir ( tmpfolder ) ; } catch ( e ) { }
210
- try { fs . mkdirSync ( tmpfolder ) ; } catch ( e ) { success = false ; }
211
-
212
-
213
- var completeImport = function ( ) {
214
-
215
- if ( ! fs . existsSync ( tmpfolder + '/app/meta.json' )
216
- || ! fs . existsSync ( tmpfolder + '/app/app.js' )
217
- || ! fs . existsSync ( tmpfolder + '/views/index.html' )
218
- || ! fs . existsSync ( tmpfolder + '/static/css/index.css' )
219
- || ! fs . existsSync ( tmpfolder + '/static/js/index.js' ) ) {
220
-
221
- res . json ( {
222
- status : "error" ,
223
- error : "Invalid application bundle"
224
- } ) ;
225
- return ;
226
- }
227
-
228
-
229
- var importfile = fs . readFileSync ( tmpfolder + '/app/meta.json' , 'utf-8' ) ;
230
- var importinfo = JSON . parse ( importfile ) ;
231
-
232
- if ( ! importinfo || ! importinfo . color
233
- || typeof ( importinfo . author ) === 'undefined' || ! importinfo . name
234
- || ! importinfo . created || ! importinfo . modified ) {
235
-
236
- res . json ( {
237
- status : "error" ,
238
- error : "invalid project file"
239
- } ) ;
240
- return ;
241
- }
242
-
243
-
244
- var metainfo = {
245
- created : importinfo . created ,
246
- modified : importinfo . modified ,
247
- color : importinfo . color ,
248
- author : importinfo . author ,
249
- name : importinfo . name ,
250
- hidden : false ,
251
- } ;
252
-
253
- var newappid = getAppIDFromTitle ( metainfo . name ) ;
254
- if ( newappid === "" ) {
255
- res . json ( {
256
- status : 'error' ,
257
- error : 'invalid app id'
258
- } ) ;
259
- }
260
- newappid = getAvailableNewAppID ( newappid ) ;
261
- buildFolderStructure ( newappid ) ;
262
-
263
- //app meta.json file
264
- var metapath = process . cwd ( ) + '/apps/' + newappid + '/meta.json' ;
265
- fs . writeFileSync ( metapath , JSON . stringify ( metainfo , null , 4 ) , 'utf8' ) ;
266
-
267
- //app node.js file
268
- copyFile ( tmpfolder + '/app/app.js' , path + '/apps/' + newappid + '/app.js' ) ;
269
- //html view
270
- copyFile ( tmpfolder + '/views/index.html' , path + '/views/apps/' + newappid + '/index.html' ) ;
271
- //css data
272
- copyFile ( tmpfolder + '/static/css/index.css' , path + '/static/apps/' + newappid + '/css/index.css' ) ;
273
- //index.js file
274
- copyFile ( tmpfolder + '/static/js/index.js' , path + '/static/apps/' + newappid + '/js/index.js' ) ;
275
-
276
- var mediadir = tmpfolder + '/static/media/' ;
277
- var mediafiles = fs . readdirSync ( mediadir ) ;
278
- for ( var x in mediafiles ) {
279
- var filename = mediafiles [ x ] ;
280
- var info = fs . statSync ( mediadir + filename ) ;
281
- if ( typeof info !== 'undefined' && info && info . isFile ( ) ) {
282
- copyFile ( mediadir + filename , path + "/static/apps/" + newappid + "/media/" + filename ) ;
283
- }
284
- }
285
-
207
+ tmp . dir ( { mode : 0755 , unsafeCleanup : true } , function ( err , tmpPath ) {
208
+ var unzip = spawn ( 'unzip' , [ req . files [ 'import_file' ] . path ] , { cwd : tmpPath } ) ;
286
209
287
- res . json ( {
288
- status : "success" ,
289
- name : metainfo . name ,
290
- appname : newappid
291
- } ) ;
292
- } ;
293
-
294
-
295
- var uploadPath = tmpfolder + '/appimport.zip' ;
296
- fs . readFile ( req . files [ 'import_file' ] . path , function ( err , data ) {
297
- fs . writeFile ( uploadPath , data , function ( err ) {
298
-
299
- var unzip = spawn ( 'unzip' , [ 'appimport.zip' ] , { cwd : tmpfolder } ) ;
300
- unzip . stdout . on ( 'data' , function ( data ) {
301
- } ) ;
302
- unzip . stderr . on ( 'data' , function ( data ) {
303
- } ) ;
304
- unzip . on ( 'exit' , function ( code ) {
305
- if ( code !== 0 ) {
306
- res . json ( {
307
- status : "error" ,
308
- error : "unzip error: " + code
210
+ unzip . on ( 'exit' , function ( code ) {
211
+ if ( code !== 0 ) {
212
+ res . json ( {
213
+ status : "error" ,
214
+ error : "unzip error: " + code
215
+ } ) ;
216
+ } else {
217
+ console . log ( tmpPath ) ;
218
+ async . every ( [
219
+ tmpPath + '/app/meta.json' ,
220
+ tmpPath + '/app/app.js' ,
221
+ tmpPath + '/views/index.html' ,
222
+ tmpPath + '/static/css/index.css' ,
223
+ tmpPath + '/static/js/index.js' ] ,
224
+ fs . exists ,
225
+ function ( result ) {
226
+ if ( ! result ) {
227
+ res . json ( {
228
+ status : "error" ,
229
+ error : "Invalid application bundle"
230
+ } ) ;
231
+ return ;
232
+ }
233
+
234
+ fs . readFile ( tmpPath + '/app/meta.json' , 'utf-8' , function ( err , data ) {
235
+ if ( err ) {
236
+ res . json ( {
237
+ status : "error" ,
238
+ error : "cannot open project file"
239
+ } ) ;
240
+ return ;
241
+ }
242
+
243
+ var metadata = JSON . parse ( data ) ;
244
+ if ( ! metadata || ! metadata . name ) {
245
+ res . json ( {
246
+ status : "error" ,
247
+ error : "invalid project file"
248
+ } ) ;
249
+ return ;
250
+ }
251
+
252
+ var newappid = getAppIDFromTitle ( metadata . name ) ;
253
+ if ( newappid === "" ) {
254
+ res . json ( {
255
+ status : 'error' ,
256
+ error : 'invalid app id'
257
+ } ) ;
258
+ return ;
259
+ }
260
+
261
+ newappid = getAvailableNewAppID ( newappid ) ;
262
+
263
+ coderlib . createApp ( tmpPath , newappid , function ( err , app ) {
264
+ if ( err ) {
265
+ res . json ( {
266
+ status : 'error' ,
267
+ error : err
268
+ } ) ;
269
+ } else {
270
+ res . json ( {
271
+ status : "success" ,
272
+ name : app . metadata . name ,
273
+ appname : app . name
274
+ } ) ;
275
+ }
276
+ } ) ;
277
+ } ) ;
309
278
} ) ;
310
- } else {
311
- completeImport ( ) ;
312
- }
313
- } ) ;
314
-
279
+ }
315
280
} ) ;
316
281
} ) ;
317
-
318
-
319
-
320
- } ;
282
+ }
321
283
322
284
exports . api_app_remove_handler = function ( app , req , res , pathmatches ) {
323
285
var apptoremove = "" ;
0 commit comments