Skip to content

Commit f196633

Browse files
author
Greg Toth
committed
modify app import to also handle app files that are nested underneath a top level directory in the import zip file
1 parent 99b70ee commit f196633

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

coder-base/apps/coder/app.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,57 @@ exports.api_app_import_handler = function( req, res, pathmatches ) {
196196
try { forceRemoveDir( tmpfolder ); } catch (e) {}
197197
try { fs.mkdirSync( tmpfolder ); } catch (e) { success = false; }
198198

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+
}
199214

200215
var completeImport = function() {
201216

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 )) {
207218

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 + '/' );
214225

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+
}
215250

216251
var importfile = fs.readFileSync( tmpfolder + '/app/meta.json', 'utf-8' );
217252
var importinfo = JSON.parse(importfile);

0 commit comments

Comments
 (0)