Skip to content

Commit

Permalink
adding basic download handler and rendering for local assets page
Browse files Browse the repository at this point in the history
  • Loading branch information
thelamer committed Dec 9, 2019
1 parent 72e4fb6 commit 0ee3438
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 28 deletions.
68 changes: 57 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var fs = require('fs');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var isBinaryFile = require("isbinaryfile").isBinaryFile;
var path = require('path');
var readdirp = require('readdirp');
var request = require('request');
var si = require('systeminformation');
var { version } = require('./package.json');
Expand Down Expand Up @@ -37,7 +39,6 @@ io.on('connection', function(socket){
socket.on('getdash', function(){
var tftpcmd = '/usr/sbin/in.tftpd --version';
var nginxcmd = '/usr/sbin/nginx -v';
// Hard code until we have endpoints to ingest
var dashinfo = {};
dashinfo['webversion'] = version;
dashinfo['menuversion'] = fs.readFileSync('/config/menuversion.txt', 'utf8');
Expand Down Expand Up @@ -108,11 +109,24 @@ io.on('connection', function(socket){
// When the endpoints content is requested send it to the client
socket.on('getlocal', function(filename){
var remotemenuversion = fs.readFileSync('/config/menuversion.txt', 'utf8');
request.get('https://raw.githubusercontent.com/netbootxyz/netboot.xyz/' + remotemenuversion + '/endpoints.yml', function (error, response, body) {
if (!error && response.statusCode == 200) {
var endpoints = yaml.safeLoad(body);
io.sockets.in(socket.id).emit('renderlocal',endpoints,remotemenuversion);
request.get('https://raw.githubusercontent.com/netbootxyz/netboot.xyz/' + remotemenuversion + '/endpoints.yml', async function (error, response, body) {
if (!error && response.statusCode == 200) {
var localfiles = await readdirp.promise('/assets/.');
var assets = [];
if (localfiles.length != 0){
for (var i in localfiles){
assets.push('/' + localfiles[i].path);
}
}
var endpoints = yaml.safeLoad(body);
io.sockets.in(socket.id).emit('renderlocal',endpoints,assets,remotemenuversion);
}
});
});
// When remote downloads are requested make folders and download
socket.on('dlremote', function(dlfiles){
dlremote(dlfiles, function(response){
io.sockets.in(socket.id).emit('renderlocalhook');
});
});
});
Expand Down Expand Up @@ -146,11 +160,13 @@ async function upgrademenu(version, callback){
// Download files
var download_files = ['menus.tar.gz', 'netboot.xyz-undionly.kpxe', 'netboot.xyz.efi', 'netboot.xyz.kpxe'];
var download_endpoint = 'https://github.com/netbootxyz/netboot.xyz/releases/download/' + version + '/';
var downloads = [];
for (var i in download_files){
var file = download_files[i];
var url = download_endpoint + file;
await downloader(url,remote_folder);
var url = download_endpoint + file;
downloads.push({'url':url,'path':remote_folder});
}
await downloader(downloads);
var untarcmd = 'tar xf ' + remote_folder + 'menus.tar.gz -C ' + remote_folder;
exec(untarcmd, function (err, stdout) {
fs.unlinkSync(remote_folder + 'menus.tar.gz');
Expand All @@ -161,10 +177,40 @@ async function upgrademenu(version, callback){
});
}

async function downloader(url, path, callback){
const dl = new DownloaderHelper(url, path);
dl.on('end', () => console.log('Downloaded ' + url + ' to ' + path));
await dl.start();
// Grab remote files
async function dlremote(dlfiles, callback){
var dlarray = [];
for (var i in dlfiles){
var dlfile = dlfiles[i];
var dlpath = '/assets' + path.dirname(dlfile);
// Make destination directory
fs.mkdirSync(dlpath, { recursive: true });
// Construct array for use in download function
var url = 'https://github.com/netbootxyz' + dlfile;
dlarray.push({'url':url,'path':dlpath});
}
await downloader(dlarray);
callback(null, 'done');
}

// downloader loop
async function downloader(downloads){
var total = downloads.length;
for (var i in downloads){
var value = downloads[i];
var url = value.url;
var path = value.path;
var dloptions = {override:true,retry:{maxRetries:2,delay:5000}};
var dl = new DownloaderHelper(url, path, dloptions);
dl.on('end', function(){
console.log('Downloaded ' + url + ' to ' + path);
});
dl.on('progress', function(stats){
io.emit('dldata', url, [+i + 1,total], stats);
});
await dl.start();
}
io.emit('purgestatus');
}

// Spin up application on port 3000
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"isbinaryfile": "^4.0.2",
"js-yaml": "^3.13.1",
"node-downloader-helper": "^1.0.11",
"readdirp": "^3.3.0",
"request": "^2.88.0",
"socket.io": "^2.3.0",
"systeminformation": "^4.16.0"
Expand Down
5 changes: 3 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
<div class="nav-link" style="cursor:pointer;" onclick="renderlocal()" >Local Assets</div>
</li>
</ul>
<form class="form-inline my-2 my-md-0" onsubmit="return false;" id="headerform">
</form>
<ul class="navbar-nav ml-auto">
<div id="statusbar" style="width:500px" ></div>
</ul>
</div>
</nav>
<!-- Main page content -->
Expand Down
73 changes: 58 additions & 15 deletions public/js/netbootxyz-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ socket.on('editrenderfile', function(response,filename,metadata){
var buttons = '';
}
else if (metadata == false){
var buttons = '<button style="cursor:pointer;" onclick="saveconfig(\'' + filename + '\')" class="btn btn-success m-3 float-right" type="submit">Save Config</button>';
var buttons = '<button onclick="saveconfig(\'' + filename + '\')" class="btn btn-success m-3 float-right" type="submit">Save Config</button>';
}
else if (metadata == true){
var buttons = '<button style="cursor:pointer;" onclick="saveconfig(\'' + filename + '\')" class="btn btn-success m-3 float-right" type="submit">Save Config</button>\
<button style="cursor:pointer;" onclick="revertconfig(\'' + filename + '\')" class="btn btn-primary m-3 float-right" type="submit">Revert to Stock</button>';
var buttons = '<button onclick="saveconfig(\'' + filename + '\')" class="btn btn-success m-3 float-right" type="submit">Save Config</button>\
<button onclick="revertconfig(\'' + filename + '\')" class="btn btn-primary m-3 float-right" type="submit">Revert to Stock</button>';
}
$('#configcontent').empty();
$('#configcontent').append('\
Expand Down Expand Up @@ -219,24 +219,22 @@ function renderlocal(){
$('#pagecontent').append('<center><div class="spinner-grow" style="width: 3rem; height: 3rem;" role="status"><span class="sr-only">Loading...</span></div><br><h2>Getting Remote file list</h2></center>');
socket.emit('getlocal');
}
socket.on('renderlocal', function(endpoints,remotemenuversion){
console.log(remotemenuversion);
socket.on('renderlocal', function(endpoints,localfiles,remotemenuversion){
$('#pagecontent').empty();
$('#pagecontent').append('\
<div class="card-group">\
<div class="card">\
<div class="card-header">\
Remote Functions\
</div>\
<div class="card-body">\
<table class="table table-sm" id="">\
</table>\
<div class="row">\
<div class="col-8">\
</div>\
<div class="col-4">\
<button onclick="dlremote()" class="btn btn-success btn-sm">Download Selected</button>\
</div>\
</div>\
</div>\
</div>\
<div class="card">\
<div class="card-header">\
Local Functions\
</div>\
<div class="card-body">\
<table class="table table-sm" id="">\
</table>\
Expand Down Expand Up @@ -267,7 +265,12 @@ socket.on('renderlocal', function(endpoints,remotemenuversion){
</div>');
$.each(endpoints.endpoints, function( index, value ) {
$.each(value.files, function( arrindex, file ) {
$('#remoteassets').append('<tr><td><input type="checkbox" class="form-check-input remotecheck" id="fill"></td><td>' + index + '</td><td><a href="https://github.com/netbootxyz' + value.path + file + '" target="_blank">' + value.path.split('download/')[1] + file + '</a></td></tr>');
if (localfiles.includes(value.path + file)){
$('#localassets').append('<tr><td><input type="checkbox" class="form-check-input localcheck" value="' + value.path + file + '"></td><td>' + index + '</td><td><a href="https://github.com/netbootxyz' + value.path + file + '" target="_blank">' + value.path.split('download/')[1] + file + '</a></td></tr>');
}
else{
$('#remoteassets').append('<tr><td><input type="checkbox" class="form-check-input remotecheck" value="' + value.path + file + '"></td><td>' + index + '</td><td><a href="https://github.com/netbootxyz' + value.path + file + '" target="_blank">' + value.path.split('download/')[1] + file + '</a></td></tr>');
}
});
});
});
Expand All @@ -290,4 +293,44 @@ function localclear(){
$('.localcheck').each(function() {
this.checked = false;
});
};
};
// Download remote files
function dlremote(){
var allfiles = $('.remotecheck');
var dlfiles = [];
$.each(allfiles, function( index, value ) {
if($(this).is(":checked")){
dlfiles.push($(this).val());
}
}).promise().done(function(){
if(dlfiles.length != 0){
socket.emit('dlremote',dlfiles);
}
});
}
// Re-render local hook
socket.on('renderlocalhook', function(){
renderlocal();
});

//// Download Status Bars ////
socket.on('dldata', function(url, count, stats){
$('#statusbar').empty();
$('#statusbar').append('\
<div class="row">\
<div class="col-4">\
' + url.split('download/')[1] + '\
</div>\
<div class="col-2">\
' + count[0] + ' of ' + count[1] + '\
</div>\
<div class="col-6">\
<div class="progress">\
<div class="progress-bar" role="progressbar" style="width: ' + stats.progress + '%;" aria-valuenow="' + stats.progress + '" aria-valuemin="0" aria-valuemax="100"></div>\
</div>\
</div>\
</dev>');
});
socket.on('purgestatus', function(){
$('#statusbar').empty();
});

0 comments on commit 0ee3438

Please sign in to comment.