diff --git a/js/apps/system/aardvark/aardvark.js b/js/apps/system/aardvark/aardvark.js index 858caa2caa..6c29e3020f 100644 --- a/js/apps/system/aardvark/aardvark.js +++ b/js/apps/system/aardvark/aardvark.js @@ -45,10 +45,10 @@ var docus = new (require("lib/swagger").Swagger)(); * available for mounting. */ -controller.post("/foxxes/fetch", function (req, res) { +controller.post("/foxxes/inspect", function (req, res) { var content = JSON.parse(req.requestBody), path = content.filename; - res.json(foxxes.fetch(path)); + res.json(foxxes.inspect(path)); }); // ............................................................................. diff --git a/js/apps/system/aardvark/frontend/js/views/applicationsView.js b/js/apps/system/aardvark/frontend/js/views/applicationsView.js index 21be60be56..e44b46d902 100644 --- a/js/apps/system/aardvark/frontend/js/views/applicationsView.js +++ b/js/apps/system/aardvark/frontend/js/views/applicationsView.js @@ -1,5 +1,5 @@ /*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true, vars: true, white: true, plusplus: true */ -/*global Backbone, EJS, $, window, arangoHelper, templateEngine, _ */ +/*global Backbone, EJS, $, window, arangoHelper, templateEngine, _, console */ window.ApplicationsView = Backbone.View.extend({ el: '#content', @@ -42,11 +42,30 @@ window.ApplicationsView = Backbone.View.extend({ $.ajax({ type: "POST", async: false, - url: "/_admin/aardvark/foxxes/fetch", + url: "/_admin/aardvark/foxxes/inspect", data: res.responseText, contentType: "application/json" }).done(function(res) { + // TODO: remove logs, report errors etc. console.log("Pos:", res); + + $.ajax({ + type: "POST", + async: false, + url: '/_admin/foxx/fetch', + data: JSON.stringify({ + name: res.name, + version: res.version, + filename: res.filename + }), + processData: false + }).done(function (res) { + console.log(res); + // TODO: reload UI + }).fail(function (res) { + console.log(res); + }); + }).fail(function(err) { console.log("Neg:", err); }); diff --git a/js/apps/system/aardvark/lib/foxxes.js b/js/apps/system/aardvark/lib/foxxes.js index c499378c3f..6a20fc673c 100644 --- a/js/apps/system/aardvark/lib/foxxes.js +++ b/js/apps/system/aardvark/lib/foxxes.js @@ -128,17 +128,111 @@ exports.Foxxes = function () { return success; }; - // Fetch a foxx from tmp zip file - this.fetch = function (path) { + // TODO: merge with functionality js/client/modules/org/arangodb/foxx/manager.js + this.repackZipFile = function (path) { + if (! fs.exists(path) || ! fs.isDirectory(path)) { + throw "'" + String(path) + "' is not a directory"; + } + + var tree = fs.listTree(path); + var files = []; + var i; + + for (i = 0; i < tree.length; ++i) { + var filename = fs.join(path, tree[i]); + + if (fs.isFile(filename)) { + files.push(tree[i]); + } + } + + if (files.length === 0) { + throw "Directory '" + String(path) + "' is empty"; + } + + var tempFile = fs.getTempFile("downloads", false); + + fs.zipFile(tempFile, path, files); + + return tempFile; + }; + + // TODO: merge with functionality js/client/modules/org/arangodb/foxx/manager.js + this.inspectUploadedFile = function (filename) { + if (! fs.isFile(filename)) { + throw "Unable to find zip file"; + } + + var i; + var path = fs.getTempFile("zip", false); + + fs.unzipFile(filename, path, false, true); + + // ............................................................................. + // locate the manifest file + // ............................................................................. + + var tree = fs.listTree(path).sort(function(a,b) { + return a.length - b.length; + }); + + var found; + var mf = "manifest.json"; + var re = /[\/\\\\]manifest\.json$/; // Windows! + + for (i = 0; i < tree.length && found === undefined; ++i) { + var tf = tree[i]; + + if (re.test(tf) || tf === mf) { + found = tf; + break; + } + } + + if (typeof found === "undefined") { + fs.removeDirectoryRecursive(path); + throw "Cannot find manifest file in zip file"; + } + + var mp; + + if (found === mf) { + mp = "."; + } + else { + mp = found.substr(0, found.length - mf.length - 1); + } + + var manifest = JSON.parse(fs.read(fs.join(path, found))); + + var absolutePath = this.repackZipFile(fs.join(path, mp)); + + var result = { + name : manifest.name, + version: manifest.version, + filename: absolutePath.substr(fs.getTempPath().length + 1) + }; + + fs.removeDirectoryRecursive(path); + + return result; + }; + + // Inspect a foxx in tmp zip file + this.inspect = function (path) { var fullPath = fs.join(fs.getTempPath(), path); + try { - return foxxmanager.fetch("zip", fullPath); - } catch (e) { - require("console").log(e); + var result = this.inspectUploadedFile(fullPath); + fs.remove(fullPath); + return result; + } + catch (e) { + require("console").log(e); // TODO return { error: true, status: 500, - message: "Could not import the app." + message: "Could not import the app." // TODO: add more detailed error message. TODO: return an error }; } };