Skip to content

Commit

Permalink
adding 2 part file downloads and deletion for local files
Browse files Browse the repository at this point in the history
  • Loading branch information
thelamer committed Dec 9, 2019
1 parent 0ee3438 commit daa7462
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
39 changes: 37 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var path = require('path');
var readdirp = require('readdirp');
var request = require('request');
var si = require('systeminformation');
const util = require('util');
var { version } = require('./package.json');
var yaml = require('js-yaml');

Expand Down Expand Up @@ -129,6 +130,15 @@ io.on('connection', function(socket){
io.sockets.in(socket.id).emit('renderlocalhook');
});
});
// When Local deletes are requested purge items
socket.on('deletelocal', function(dlfiles){
for (var i in dlfiles){
var file = dlfiles[i];
fs.unlinkSync('/assets' + file);
console.log('Deleted /assets' + file);
}
io.sockets.in(socket.id).emit('renderlocalhook');
});
});

//// Functions ////
Expand Down Expand Up @@ -195,6 +205,7 @@ async function dlremote(dlfiles, callback){

// downloader loop
async function downloader(downloads){
var startTime = new Date();
var total = downloads.length;
for (var i in downloads){
var value = downloads[i];
Expand All @@ -205,10 +216,34 @@ async function downloader(downloads){
dl.on('end', function(){
console.log('Downloaded ' + url + ' to ' + path);
});
dl.on('progress', function(stats){
io.emit('dldata', url, [+i + 1,total], stats);
dl.on('progress', function(stats){
var currentTime = new Date();
var elaspsedTime = currentTime - startTime;
if (elaspsedTime > 500) {
startTime = currentTime;
io.emit('dldata', url, [+i + 1,total], stats);
}
});
await dl.start();
// Part 2 if exists repeat
var requestPromise = util.promisify(request);
var response = await requestPromise(url + '.part2', {method: 'HEAD'});
var s3test = response.headers.server;
if (s3test == 'AmazonS3'){
var dl2 = new DownloaderHelper(url + '.part2', path, dloptions);
dl2.on('end', function(){
console.log('Downloaded ' + url + '.part2' + ' to ' + path);
});
dl2.on('progress', function(stats){
var currentTime = new Date();
var elaspsedTime = currentTime - startTime;
if (elaspsedTime > 500) {
startTime = currentTime;
io.emit('dldata', url, [+i + 1,total], stats);
}
});
await dl2.start();
}
}
io.emit('purgestatus');
}
Expand Down
47 changes: 25 additions & 22 deletions public/js/netbootxyz-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,30 +222,15 @@ function renderlocal(){
socket.on('renderlocal', function(endpoints,localfiles,remotemenuversion){
$('#pagecontent').empty();
$('#pagecontent').append('\
<div class="card-group">\
<div class="card">\
<div class="card-body">\
<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-body">\
<table class="table table-sm" id="">\
</table>\
</div>\
</div>\
</div>\
<div class="card-group">\
<div class="card">\
<div class="card-header">\
Remote Assets at <a target="_blank" href="https://github.com/netbootxyz/netboot.xyz/releases/' + remotemenuversion + '">' + remotemenuversion + '</a>\
<span style="float:right;"><button onclick="remoteselect()" class="btn btn-primary btn-sm mr-2">Select All</button><button onclick="remoteclear()" class="btn btn-secondary btn-sm">Clear Selection</button></span>\
<span style="float:right;">\
<button onclick="remoteselect()" class="btn btn-primary btn-sm mr-2">Select All</button>\
<button onclick="remoteclear()" class="btn btn-secondary btn-sm mr-2">Clear Selection</button>\
<button onclick="dlremote()" class="btn btn-success btn-sm">Download Selected</button>\
</span>\
</div>\
<div class="card-body">\
<table class="table table-sm" id="remoteassets">\
Expand All @@ -255,7 +240,11 @@ socket.on('renderlocal', function(endpoints,localfiles,remotemenuversion){
<div class="card">\
<div class="card-header">\
Local Assets\
<span style="float:right;"><button onclick="localselect()" class="btn btn-primary btn-sm mr-2">Select All</button><button onclick="localclear()" class="btn btn-secondary btn-sm">Clear Selection</button></span>\
<span style="float:right;">\
<button onclick="localselect()" class="btn btn-primary btn-sm mr-2">Select All</button>\
<button onclick="localclear()" class="btn btn-secondary btn-sm">Clear Selection</button>\
<button onclick="deletelocal()" class="btn btn-danger btn-sm">Delete Selected</button>\
</span>\
</div>\
<div class="card-body">\
<table class="table table-sm" id="localassets">\
Expand All @@ -266,7 +255,7 @@ socket.on('renderlocal', function(endpoints,localfiles,remotemenuversion){
$.each(endpoints.endpoints, function( index, value ) {
$.each(value.files, function( arrindex, file ) {
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>');
$('#localassets').append('<tr><td><input type="checkbox" class="form-check-input localcheck" value="' + value.path + file + '"></td><td>' + index + '</td><td>' + value.path.split('download/')[1] + file + '</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 Down Expand Up @@ -312,6 +301,20 @@ function dlremote(){
socket.on('renderlocalhook', function(){
renderlocal();
});
// Delete local files
function deletelocal(){
var allfiles = $('.localcheck');
var deletefiles = [];
$.each(allfiles, function( index, value ) {
if($(this).is(":checked")){
deletefiles.push($(this).val());
}
}).promise().done(function(){
if(deletefiles.length != 0){
socket.emit('deletelocal',deletefiles);
}
});
}

//// Download Status Bars ////
socket.on('dldata', function(url, count, stats){
Expand Down

0 comments on commit daa7462

Please sign in to comment.