daily pastebin goal
31%
SHARE
TWEET

My code

a guest Dec 7th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. var http = require('http');
  4. var _ = require('lodash');
  5. var express = require('express');
  6. var fs = require('fs');
  7. var path = require('path');
  8. var util = require('util');
  9. var program = require('commander');
  10. var archiver = require('archiver');
  11. var flash = require('connect-flash');
  12. var cookieParser = require('cookie-parser');
  13. var session = require('express-session');
  14.  
  15.  
  16. var jsonObject = '';
  17.  
  18.  
  19.  
  20. function collect(val, memo) {
  21.   if(val && val.indexOf('.') != 0) val = "." + val;
  22.   memo.push(val);
  23.   return memo;
  24. }
  25.  
  26. program
  27. .option('-p, --port <port>', 'Port to run the file-browser. Default value is 8088')
  28. .option('-e, --exclude <exclude>', 'File extensions to exclude. To exclude multiple extension pass -e multiple times. e.g. ( -e .js -e .cs -e .swp) ', collect, [])
  29. .option('-d, --location <string>', 'Directory in which the server must run')
  30. .parse(process.argv);
  31.  
  32.  
  33. var app = express();
  34. app.use(cookieParser());
  35.  
  36.  
  37. // var dir =  process.cwd();
  38. if(!program.location) program.location = path.normalize('C:/Users/Student/Desktop/binarysearch/IOHandlerDevelop');
  39. var dir = path.normalize(program.location);
  40.  
  41. var newDir = process.dir;
  42.  
  43. app.use(express.static(dir)); //app public directory
  44. app.use(express.static(__dirname)); //module directory
  45. var server = http.createServer(app);
  46.  
  47. if(!program.port) program.port = 8088;
  48.  
  49.  
  50. server.listen(program.port);
  51. console.log("Please open the link in your browser http://127.0.0.1:" + program.port + '\nServer running in: ' + path.normalize(program.location));
  52.  
  53.  
  54.  
  55. /*
  56.   Get files in directory
  57. */
  58. app.get('/files', function(req, res) {
  59.  var currentDir =  dir;
  60.  var query = req.query.path || '';
  61.  if (query) currentDir = path.join(dir, query);
  62.  console.log("browsing ", currentDir);
  63.  newDir = currentDir;
  64.  fs.readdir(currentDir, function (err, files) {
  65.      if (err) {
  66.         throw err;
  67.       }
  68.       var data = [];
  69.       files
  70.       .filter(function (file) {
  71.           return true;
  72.       }).forEach(function (file) {
  73.         try {
  74.                 //console.log("processing ", file);
  75.                 var isDirectory = fs.statSync(path.join(currentDir,file)).isDirectory();
  76.                 if (isDirectory) {
  77.                   data.push({ Name : file, IsDirectory: true, Path : path.join(query, file)  });
  78.                 } else {
  79.                   var ext = path.extname(file);
  80.                   if(program.exclude && _.contains(program.exclude, ext)) {
  81.                     console.log("excluding file ", file);
  82.                     return;
  83.                   }      
  84.                   data.push({ Name : file, Ext : ext, IsDirectory: false, Path : path.join(query, file) });
  85.                 }
  86.  
  87.         } catch(e) {
  88.           console.log(e);
  89.         }        
  90.        
  91.       });
  92.       data = _.sortBy(data, function(f) { return f.Name });
  93.       res.json(data);
  94.   });
  95. });
  96.  
  97.  
  98.  
  99. /*
  100. download current directory
  101. */
  102. app.get('/download', function(req,res) {
  103.   console.log("Trying to download");
  104.   //get parameter and check if sent sha number is on server
  105.   var sha_id = req.param('sha')||'';
  106.   console.log(sha_id);
  107.   // res.json(sha_id);
  108.  
  109.   //find directory
  110.   var downloadPath = getFolder(sha_id);
  111.   console.log(downloadPath);
  112.  
  113.   var archive = archiver('zip');
  114.  
  115.     archive.on('error', function(err) {
  116.       res.status(500).send({error: err.message});
  117.     });
  118.  
  119.     //on stream closed we can end the request
  120.     archive.on('end', function() {
  121.       console.log('Archive wrote %d bytes', archive.pointer());
  122.     });
  123.  
  124.     //set the archive name
  125.     zipName = path.basename(newDir);
  126.  
  127.     // req.flash('success', 'Download Successful');
  128.     res.attachment(zipName + '.zip');
  129.  
  130.     //this is the streaming magic
  131.     archive.pipe(res);
  132.  
  133.     var directories = [newDir];
  134.  
  135.     for(var i in directories) {
  136.       archive.directory(directories[i], directories[i].replace(newDir, ''));
  137.     }
  138.  
  139.     archive.finalize();
  140.   });
  141.  
  142. //finds the corresponding the server folder for the sha id
  143. function getFolder(sha_id) {
  144.   var gitFolder = '';
  145.   var i = 0;
  146.   var data = [];
  147.  
  148.   fs.readdir(newDir, function(err, files) {
  149.     if (err) {
  150.       throw err;
  151.     }
  152.  
  153.     files.forEach(function (file) {
  154.       var n = file.lastIndexOf("-");
  155.       data.push(file.substr(n+1));
  156.       // console.log(file);
  157.     });
  158.  
  159.     // console.log(data);
  160.     i = data.indexOf(sha_id);
  161.     console.log(i);
  162.     if (i === -1){
  163.       console.log("No such file or directory");
  164.     }
  165.  
  166.     gitFolder = files[i].toString();
  167.     // console.log(gitFolder);
  168.  
  169.     var downloadPath = path.join(newDir,gitFolder)
  170.     console.log(downloadPath);
  171.     return downloadPath;
  172.   });
  173. };
  174.  
  175.  
  176.  
  177. /*
  178.   default page
  179. */
  180. app.get('/', function(req, res) {
  181.   res.redirect('/lib/template.html');  
  182. });
  183.  
  184.  
  185.  
  186. /*
  187.   Get git info via curl request to bitbucket server
  188. */
  189. app.get('/git', function(req,res) {
  190.   /*
  191.     pass id or sha number via /git?id='sha number here'
  192.     default wil give all commits
  193.   */
  194.   var user = "Jenkins";
  195.   var pass = "password";
  196.   var obj = '';
  197.   var sha_id = req.param('id')||'';
  198.  
  199.   var auth = new Buffer(user + ':' + pass).toString('base64');
  200.   var options = {
  201.     host: 'svrbitbucket',
  202.     port: 7990,
  203.     path: '/rest/api/1.0/projects/IOH/repos/windriver69/commits/' + sha_id,
  204.     headers: {
  205.       'Authorization': 'Basic ' + auth
  206.     }
  207.   };
  208.  
  209.   http.get(options, function(response) {
  210.     response.on('data', function (data) {
  211.       obj += data;
  212.       // console.log('BODY: ' + obj);
  213.       console.log("Getting Git info");
  214.     });
  215.     response.on('end', function(){
  216.       jsonObject = JSON.parse(obj);  
  217.       console.log("sending JSON git log");
  218.       res.json(jsonObject.values);
  219.     });
  220.   }).on('error', function(e) {
  221.     console.log("Got error: " + e.message);
  222.     });
  223. });
RAW Paste Data
Top