lx-scan
Version:
License eXtension to find 5-tuples of all installed packages: name, version, project home page, license (e. g. Apache v2, BSD) and required notice. It includes a GUI to edit information for each package and to enter information if necessary.
71 lines (60 loc) • 2.22 kB
JavaScript
var fs = require("fs");
var lxu = require("lxutils");
var http = require("http");
var pa = require("path");
module.exports.get = function(req,res) {
var verbose = (req.query.verbose === "true");
var type = req.query.type||"";
var jfile = req.query.jfile||"";
var path = req.query.path||"";
var newpath = "";
path = pa.resolve(path);
if(path === "" || jfile === "") {
// A malformed URL was requested, send user back to base
res.redirect("http://localhost:8888?jfile="+jfile+"&err=Error: The given URL was missing parameters.");
return;
}
// Identify a path that doesn't exist
if(!fs.existsSync(pa.dirname(path))) {
res.redirect("http://localhost:8888?jfile="+jfile+"&err=Error: File path does not exist.");
return;
}
if(fs.existsSync(path)) {
if(!fs.statSync(path).isFile()) {
res.redirect("http://localhost:8888?jfile="+jfile+"&err=Error: Cannot overwrite folders.");
return;
}
}
if(type === "json") {
// In this case just copy the local JSON file to whatever destination;
// no care taken with read/write so there's a race condition here if a second
// user is trying to modify the same JSON file at the same time;
// SUPER ROBUST CODE TEAM GO!
lxu.parseJSON(req.app.locals.jfiles[jfile],function(err,jfile_data) {
jfile_data = lxu.updateJMods(jfile_data);
var fname_split = path.split(".");
if(fname_split[fname_split.length - 1] !== "json") {
path += ".json"
}
newpath = ("&path="+path);
lxu.writeJSON(req.app.locals.jfiles[jfile],jfile_data,function(err) {
res.redirect("http://localhost:8888?jfile="+jfile+newpath);
});
lxu.writeJSON(path,jfile_data);
});
} else if(type === "html") {
// Download an HTML page rendered by a separate view
var file = fs.createWriteStream(path);
var url = "http://localhost:8888/save/report?jfile="+jfile+"&verbose="+verbose+"&path="+path;
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish',function() {
file.close();
});
}).on('error',function(err) {
fs.unlink(path);
throw err;
});
res.redirect("http://localhost:8888?jfile="+jfile+newpath+"&path="+req.query.oldpath);
}
};