Advertisement
Guest User

Untitled

a guest
Apr 1st, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var
  2.         https = require('https'),
  3.         AdmZip = require('adm-zip'),
  4.         app = require('express')();
  5.  
  6. app.get("/*", function(req, res) {
  7.  
  8.     var
  9.             parts = req.params[0].split('/'),
  10.             owner = parts[0],
  11.             repo = parts[1],
  12.             branch = parts[2],
  13.             top_dir = repo + '-' + branch + '/',
  14.             zip_address = 'https://nodeload.github.com/' + owner + '/' + repo + '/zip/' + branch;
  15.  
  16.     https.get(zip_address, function(response) {
  17.         var data = [], dataLen = 0;
  18.         response.on('data', function(chunk) {
  19.  
  20.             data.push(chunk);
  21.             dataLen += chunk.length;
  22.  
  23.         }).on('end', function() {
  24.             var buf = new Buffer(dataLen);
  25.             for (var i = 0, len = data.length, pos = 0; i < len; i++) {
  26.                 data[i].copy(buf, pos);
  27.                 pos += data[i].length;
  28.             }
  29.             var zip = new AdmZip(buf);
  30.             var new_zip = new AdmZip();
  31.             var zipEntries = zip.getEntries();
  32.  
  33.             zipEntries.forEach(function(entry) {
  34.                 if (!entry.isDirectory) {
  35.                     console.log(entry.entryName.replace(top_dir, ''));
  36.                     console.log(zip.readAsText(entry));
  37.                     new_zip.addFile(entry.entryName.replace(top_dir, ''), zip.readAsText(entry), "");
  38.                 }
  39.             });
  40.  
  41.             var out = new_zip.toBuffer();
  42.             res.header('Content-Type', 'application/zip');
  43.             res.header('Content-Disposition', 'attachment; filename="' + repo + '.zip"');
  44.             res.send(out);
  45.         });
  46.  
  47.     });
  48.  
  49. });
  50.  
  51. app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement