exports.fetchFromGithub = function (url, name, version) { var source = { location: url, name: name, version: version }; utils.processGithubRepository(source); var realFile = source.filename; var appPath = module.appPath(); if (appPath === undefined) { fs.remove(realFile); throw "javascript.app-path not set, rejecting app loading"; } var path = fs.join(appPath, source.name + "-" + source.version); if (fs.exists(path)) { fs.remove(realFile); var err = new ArangoError(); err.errorNum = arangodb.errors.ERROR_APP_ALREADY_EXISTS.code; err.errorMessage = arangodb.errors.ERROR_APP_ALREADY_EXISTS.message; throw err; } fs.makeDirectoryRecursive(path); fs.unzipFile(realFile, path, false, true); var gitFilename = "/gitinfo.json"; fs.write(path+gitFilename, JSON.stringify(url)); exports.scanAppDirectory(); return "app:" + source.name + ":" + source.version; };
callback : function (req, res) { 'use strict'; var safe = /^[0-9a-zA-Z_\-\.]+$/; if (req.suffix.length !== 0) { actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER); return; } var json = actions.getJsonBody(req, res, actions.HTTP_BAD); if (json === undefined) { return; } var serverFile = json.filename; var realFile = fs.join(fs.getTempPath(), serverFile); if (! fs.isFile(realFile)) { actions.resultNotFound(req, res, arangodb.errors.ERROR_FILE_NOT_FOUND.code); return; } try { var name = json.name; var version = json.version; if (! safe.test(name)) { fs.remove(realFile); throw "rejecting unsafe name '" + name + "'"; } if (! safe.test(version)) { fs.remove(realFile); throw "rejecting unsafe version '" + version + "'"; } var appPath = module.appPath(); if (typeof appPath === "undefined") { fs.remove(realFile); throw "javascript.app-path not set, rejecting app loading"; } var path = fs.join(appPath, name + "-" + version); if (fs.exists(path)) { fs.remove(realFile); throw "destination path '" + path + "' already exists"; } fs.makeDirectoryRecursive(path); fs.unzipFile(realFile, path, false, true); foxxManager.scanAppDirectory(); var found = arangodb.db._collection("_aal").firstExample({ type: "app", path: name + "-" + version }); if (found !== null) { found = found.app; } actions.resultOk(req, res, actions.HTTP_OK, { path: path, app: found }); } catch (err) { actions.resultException(req, res, err, undefined, false); } }
function updateFishbowlFromZip (filename) { 'use strict'; var i; var tempPath = fs.getTempPath(); var toSave = [ ]; try { fs.makeDirectoryRecursive(tempPath); var root = fs.join(tempPath, "foxx-apps-master/applications"); // remove any previous files in the directory fs.listTree(root).forEach(function (file) { if (file.match(/\.json$/)) { try { fs.remove(fs.join(root, file)); } catch (err3) { } } }); fs.unzipFile(filename, tempPath, false, true); if (! fs.exists(root)) { throw new Error("'applications' directory is missing in foxx-apps-master, giving up"); } var m = fs.listTree(root); var reSub = /(.*)\.json$/; for (i = 0; i < m.length; ++i) { var f = m[i]; var match = reSub.exec(f); if (match === null) { continue; } var app = fs.join(root, f); var desc; try { desc = JSON.parse(fs.read(app)); } catch (err1) { arangodb.printf("Cannot parse description for app '" + f + "': %s\n", String(err1)); continue; } desc._key = match[1]; if (! desc.hasOwnProperty("name")) { desc.name = match[1]; } toSave.push(desc); } if (toSave.length > 0) { var fishbowl = getFishbowlStorage(); db._executeTransaction({ collections: { write: fishbowl.name() }, action: function (params) { var c = require("internal").db._collection(params.collection); c.truncate(); params.apps.forEach(function(app) { c.save(app); }); }, params: { apps: toSave, collection: fishbowl.name() } }); arangodb.printf("Updated local repository information with %d application(s)\n", toSave.length); } } catch (err) { if (tempPath !== undefined && tempPath !== "") { try { fs.removeDirectoryRecursive(tempPath); } catch (err2) { } } throw err; } }
function repackZipFile (source) { 'use strict'; var i; var filename = source.filename; 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! var tf; for (i = 0; i < tree.length && found === undefined; ++i) { tf = tree[i]; if (re.test(tf) || tf === mf) { found = tf; } } if (found === undefined) { throwFileNotFound("Cannot find manifest file in zip file '" + filename + "'"); } var mp; if (found === mf) { mp = "."; } else { mp = found.substr(0, found.length - mf.length - 1); } // ............................................................................. // throw away source file if necessary // ............................................................................. if (source.removeFile && source.filename !== '') { try { fs.remove(source.filename); } catch (err1) { arangodb.printf("Cannot remove temporary file '%s'\n", source.filename); } } delete source.filename; delete source.removeFile; // ............................................................................. // repack the zip file // ............................................................................. var newSource = { location: fs.join(path, mp) }; processDirectory(newSource); source.name = newSource.name; source.version = newSource.version; source.filename = newSource.filename; source.removeFile = newSource.removeFile; // ............................................................................. // cleanup temporary paths // ............................................................................. try { fs.removeDirectoryRecursive(path); } catch (err2) { arangodb.printf("Cannot remove temporary directory '%s'\n", path); } }
this.inspectUploadedFile = function (filename) { var console = require("console"); if (! fs.isFile(filename)) { throw "Unable to find zip file"; } var i; var path; try { path = fs.getTempFile("zip", false); } catch (err1) { console.error("cannot get temp file: %s", String(err1)); throw err1; } try { fs.unzipFile(filename, path, false, true); } catch (err2) { console.error("cannot unzip file '%s' into directory '%s': %s", filename, path, String(err2)); throw err2; } // ............................................................................. // 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; };