Skip to content

Commit

Permalink
check free space before uploads and compare against local file
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece committed Feb 2, 2015
1 parent 2fb0711 commit 3d34453
Showing 1 changed file with 64 additions and 11 deletions.
75 changes: 64 additions & 11 deletions pi_finder/src/ssh.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var SSH = require('ssh2'),
events = require('events'),
path = require('path'),
fs = require('fs'),
util = require('util');

/**** ssh is an event emitter ****/
Expand Down Expand Up @@ -43,7 +44,7 @@ proto.password = 'raspberry';
proto.host = '10.0.1.1';
proto.port = 22;
proto.type = 'shell';
proto.install_command = 'curl -SLs http://bootstrap.uniontownlabs.org/install | sudo bash';
proto.install_command = 'curl -SLs https://apt.adafruit.com/install | sudo bash';
proto.pi_config = {};

proto.handleError = function(err) {
Expand Down Expand Up @@ -105,31 +106,83 @@ proto.shutdown = function() {

proto.upload = function() {

if(! this.file_upload || ! fs.existsSync(this.file_upload)) {
return this.handleError('no file specified');
}

var size = parseInt(fs.statSync(this.file_upload).size / 1000);

this.free(function(err, available) {

if(! available) {
return this.handleError(err || 'there is not enough space available to upload the selected file');
}

if(available <= size) {
return this.handleError('there is not enough space available to upload the selected file');
}

this.send_file();

}.bind(this));

};

proto.send_file = function() {

var self = this;

this.ssh.sftp(function(err, sftp) {

if(err) {
return this.handleError(err);
return self.handleError(err);
}

var dest = '/home/' + this.username + '/' + path.basename(this.file_upload);
var dest = '/home/' + self.username + '/' + path.basename(self.file_upload);

var progress = function(uploaded, chunk, total) {
var percent = ((uploaded / total) * 100).toFixed(2);
this.handleData('\x1bc');
this.handleData(percent + '% complete\n');
}.bind(this);
self.handleData('\x1bc');
self.handleData(percent + '% complete\n');
};

sftp.fastPut(this.file_upload, dest, { step: progress }, function(err) {
sftp.fastPut(self.file_upload, dest, { step: progress }, function(err) {

if(err) {
return this.handleError(err);
return self.handleError(err);
}

this.emit('uploaded');
self.emit('uploaded');

}.bind(this));
});

}.bind(this));
});

};

proto.free = function(cb) {

this.ssh.exec('df /home | tail -1 | tr -s " " | cut -f 4 -d " "', function(err, stream) {

var data = '';

if(err) {
return cb(err);
}

stream.on('exit', function(code, signal) {

if(code) {
return cb('unable to calculate the free space available');
}

cb(null, parseInt(data) || 0);

}).on('data', function(d) {
data += d;
});

});

};

Expand Down

0 comments on commit 3d34453

Please sign in to comment.