@@ -196,22 +196,57 @@ exports.api_app_import_handler = function( req, res, pathmatches ) {
196
196
try { forceRemoveDir ( tmpfolder ) ; } catch ( e ) { }
197
197
try { fs . mkdirSync ( tmpfolder ) ; } catch ( e ) { success = false ; }
198
198
199
+ var isValidBundle = function ( check_path ) {
200
+
201
+ // Return true if find the signature of an app bundle
202
+ // in 'check_path', otherwise return false.
203
+
204
+ if ( fs . existsSync ( check_path + '/app/meta.json' )
205
+ && fs . existsSync ( check_path + '/app/app.js' )
206
+ && fs . existsSync ( check_path + '/views/index.html' )
207
+ && fs . existsSync ( check_path + '/static/css/index.css' )
208
+ && fs . existsSync ( check_path + '/static/js/index.js' ) ) {
209
+ return true ;
210
+ } else {
211
+ return false ;
212
+ }
213
+ }
199
214
200
215
var completeImport = function ( ) {
201
216
202
- if ( ! fs . existsSync ( tmpfolder + '/app/meta.json' )
203
- || ! fs . existsSync ( tmpfolder + '/app/app.js' )
204
- || ! fs . existsSync ( tmpfolder + '/views/index.html' )
205
- || ! fs . existsSync ( tmpfolder + '/static/css/index.css' )
206
- || ! fs . existsSync ( tmpfolder + '/static/js/index.js' ) ) {
217
+ if ( ! isValidBundle ( tmpfolder ) ) {
207
218
208
- res . json ( {
209
- status : "error" ,
210
- error : "Invalid application bundle"
211
- } ) ;
212
- return ;
213
- }
219
+ // If don't find app signature in the top level of the
220
+ // unzipped file, also look for it in any subdirectories
221
+ // that are found (max one deep at present).
222
+
223
+ var found = false ;
224
+ var files = fs . readdirSync ( tmpfolder + '/' ) ;
214
225
226
+ var fullpath = '' ;
227
+
228
+ for ( var x in files ) {
229
+
230
+ fullpath = tmpfolder + '/' + files [ x ] ;
231
+
232
+ if ( fs . statSync ( fullpath ) . isDirectory ( ) ) {
233
+ if ( isValidBundle ( fullpath ) ) {
234
+ found = true ;
235
+ break ;
236
+ }
237
+ }
238
+ }
239
+
240
+ if ( found ) {
241
+ tmpfolder = fullpath ;
242
+ } else {
243
+ res . json ( {
244
+ status : "error" ,
245
+ error : "Invalid application bundle"
246
+ } ) ;
247
+ return ;
248
+ }
249
+ }
215
250
216
251
var importfile = fs . readFileSync ( tmpfolder + '/app/meta.json' , 'utf-8' ) ;
217
252
var importinfo = JSON . parse ( importfile ) ;
0 commit comments