Skip to content

Commit 00d7069

Browse files
committed
Fix app import.
1 parent 8b46aba commit 00d7069

File tree

3 files changed

+79
-138
lines changed

3 files changed

+79
-138
lines changed

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

Lines changed: 76 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var mustache = require('mustache');
2222
var util = require('util');
2323
var fs = require('fs');
2424
var spawn = require('child_process').spawn;
25+
var tmp = require('tmp');
26+
var async = require('async');
2527

2628
//hack to make fs.existsSync work between different node versions
2729
if ( !fs.existsSync ) {
@@ -38,7 +40,7 @@ exports.get_routes = [
3840
exports.post_routes = [
3941
{ path: '/api/app/create', handler:'api_app_create_handler' },
4042
{ path: /^\/api\/app\/remove\/(\w+)$/, handler:'api_app_remove_handler' },
41-
{ path: /^\/api\/app\/import$/, handler:'api_app_import_handler' }
43+
{ path: "/api/app/import", handler:'api_app_import_handler' }
4244
];
4345

4446
exports.on_destroy = function() {
@@ -184,7 +186,7 @@ exports.export_download_handler = function( app, req, res, pathmatches ) {
184186
};
185187

186188

187-
exports.api_app_import_handler = function( app, req, res, pathmatches ) {
189+
exports.api_app_import_handler = function( app, req, res ) {
188190

189191
if ( !req.files || !req.files['import_file'] ) {
190192
res.json({
@@ -202,122 +204,82 @@ exports.api_app_import_handler = function( app, req, res, pathmatches ) {
202204
return;
203205
}
204206

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 });
286209

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+
});
309278
});
310-
} else {
311-
completeImport();
312-
}
313-
});
314-
279+
}
315280
});
316281
});
317-
318-
319-
320-
};
282+
}
321283

322284
exports.api_app_remove_handler = function( app, req, res, pathmatches ) {
323285
var apptoremove = "";

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,12 @@ var handleFileImport = function( ev ) {
125125
if ( files && files.length > 0 ) {
126126
var importfile = files[0];
127127

128-
//console.log( importfile );
129-
if (!importfile.type.match('application/zip')) {
130-
alert('This doesn\'t appear to be a Coder project zip file');
131-
return false;
132-
}
133128
thefile = importfile;
134129

135130
var fdata = new FormData();
136131
fdata.append( 'import_file', thefile );
137132
fdata.append( 'test', 'foo' );
138-
133+
139134
$.ajax({
140135
url: '/app/coder/api/app/import',
141136
type: 'POST',
@@ -154,23 +149,6 @@ var handleFileImport = function( ev ) {
154149
}
155150
}
156151
});
157-
158-
159-
/*
160-
var reader = new FileReader();
161-
// Closure to capture the file information.
162-
reader.onload = (function(theFile) {
163-
return function(e) {
164-
// Render thumbnail.
165-
var span = document.createElement('span');
166-
span.innerHTML = ['<img class="thumb" src="', e.target.result,
167-
'" title="', escape(theFile.name), '"/>'].join('');
168-
document.getElementById('list').insertBefore(span, null);
169-
};
170-
})(f);
171-
172-
reader.readAsDataURL(f);
173-
*/
174152
}
175153
};
176154

coder-base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"connect": "*",
1515
"cookie": "*",
1616
"async": "0.2.*",
17-
"ncp": "0.5.*"
17+
"ncp": "0.5.*",
18+
"tmp": "0.0.23"
1819
}
1920
}

0 commit comments

Comments
 (0)