Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #387 from aleitner/master
Browse files Browse the repository at this point in the history
3.1.1
  • Loading branch information
bookchin committed Sep 6, 2016
2 parents af28691 + f710e2a commit 4244101
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 83 deletions.
162 changes: 114 additions & 48 deletions bin/actions/files.js
Expand Up @@ -30,6 +30,33 @@ module.exports.list = function(bucketid) {
});
};

module.exports.getInfo = function(bucketid, fileid, callback) {
var client = this._storj.PrivateClient();

client.listFilesInBucket(bucketid, function(err, files) {
if (err) {
log('error', err.message);
return callback(null);
}

if (!files.length) {
log('warn', 'There are no files in this bucket.');
return callback(null);
}

files.forEach(function(file) {
if (fileid === file.id) {
log(
'info',
'Name: %s, Type: %s, Size: %s bytes, ID: %s',
[file.filename, file.mimetype, file.size, file.id]
);
return callback(file);
}
});
});
};

module.exports.remove = function(id, fileId, env) {
var client = this._storj.PrivateClient();
var keypass = this._storj.getKeyPass();
Expand Down Expand Up @@ -61,8 +88,19 @@ module.exports.remove = function(id, fileId, env) {
// TODO: refactor this to shorter statements
module.exports.upload = function(bucket, filepath, env) {
var self = this;

var concurrency = env.concurrency ? parseInt(env.concurrency) : 6;

if (parseInt(env.redundancy) > 12 || parseInt(env.redundancy) < 1) {
return log('error', '%s is an invalid Redundancy value.', env.redundancy);
}

if (concurrency < 1) {
return log('error', 'Concurrency cannot be less than 1');
}

var client = this._storj.PrivateClient({
concurrency: env.concurrency ? parseInt(env.concurrency) : 6
concurrency: concurrency
});
var keypass = this._storj.getKeyPass();

Expand All @@ -74,10 +112,13 @@ module.exports.upload = function(bucket, filepath, env) {
async.eachOfSeries(filepaths, function(origFilepath, index, callback) {
// In *NIX the wildcard is already parsed so this will cover other OS's
var parsedFileArray = globule.find(origFilepath);
if (fs.statSync(parsedFileArray[0]).isFile() === true) {
expandedFilepaths = expandedFilepaths.concat(parsedFileArray);
if (storj.utils.existsSync(parsedFileArray[0])) {
if (fs.statSync(parsedFileArray[0]).isFile() === true) {
expandedFilepaths = expandedFilepaths.concat(parsedFileArray);
}
} else {
return log('error', '%s could not be found', origFilepath);
}

callback();
}, function(err) {
if (err) {
Expand Down Expand Up @@ -237,6 +278,10 @@ module.exports.upload = function(bucket, filepath, env) {
module.exports.mirror = function(bucket, file, env) {
var client = this._storj.PrivateClient();

if (parseInt(env.redundancy) > 12 || parseInt(env.redundancy) < 1) {
return log('error', '%s is an invalid Redundancy value.', env.redundancy);
}

log(
'info',
'Establishing %s mirrors per shard for redundancy',
Expand Down Expand Up @@ -269,63 +314,84 @@ module.exports.download = function(bucket, id, filepath, env) {
var client = this._storj.PrivateClient();
var keypass = this._storj.getKeyPass();

if (storj.utils.existsSync(filepath)) {

if (storj.utils.existsSync(filepath) && fs.statSync(filepath).isFile()) {
return log('error', 'Refusing to overwrite file at %s', filepath);
}

utils.getKeyRing(keypass, function(keyring) {
var target = fs.createWriteStream(filepath);
var secret = keyring.get(id);
if (!storj.utils.existsSync(path.dirname(filepath))) {
return log('error', '%s is not an existing folder', path.dirname(filepath));
}

if (!secret) {
return log('error', 'No decryption key found in key ring!');
}
module.exports.getInfo.call(self, bucket, id, function(file) {
var target;

var decrypter = new storj.DecryptStream(secret);
var received = 0;
var exclude = env.exclude.split(',');
if (fs.statSync(filepath).isDirectory() === true && file !== null) {

target.on('finish', function() {
log('info', 'File downloaded and written to %s.', [filepath]);
}).on('error', function(err) {
log('error', err.message);
});
var fullpath = path.join(filepath,file.filename);

client.createFileStream(bucket, id, {
exclude: exclude
},function(err, stream) {
if (err) {
return log('error', err.message);
if (storj.utils.existsSync(fullpath)) {
return log('error', 'Refusing to overwrite file at %s', fullpath);
}
target = fs.createWriteStream(fullpath);
} else {
target = fs.createWriteStream(filepath);
}

stream.on('error', function(err) {
log('warn', 'Failed to download shard, reason: %s', [err.message]);
fs.unlink(filepath, function(unlinkFailed) {
if (unlinkFailed) {
return log('error', 'Failed to unlink partial file.');
}
utils.getKeyRing(keypass, function(keyring) {
var secret = keyring.get(id);

if (!err.pointer) {
return;
}
if (!secret) {
return log('error', 'No decryption key found in key ring!');
}

log('info', 'Retrying download from other mirrors...');
exclude.push(err.pointer.farmer.nodeID);
module.exports.download.call(
self,
bucket,
id,
filepath,
{ exclude: env.exclude.join(',')}
);
});
}).pipe(through(function(chunk) {
received += chunk.length;
log('info', 'Received %s of %s bytes', [received, stream._length]);
this.queue(chunk);
})).pipe(decrypter).pipe(target);
var decrypter = new storj.DecryptStream(secret);
var received = 0;
var exclude = env.exclude.split(',');

target.on('finish', function() {
log('info', 'File downloaded and written to %s.', [filepath]);
}).on('error', function(err) {
log('error', err.message);
});

client.createFileStream(bucket, id, {
exclude: exclude
},function(err, stream) {
if (err) {
return log('error', err.message);
}

stream.on('error', function(err) {
log('warn', 'Failed to download shard, reason: %s', [err.message]);
fs.unlink(filepath, function(unlinkFailed) {
if (unlinkFailed) {
return log('error', 'Failed to unlink partial file.');
}

if (!err.pointer) {
return;
}

log('info', 'Retrying download from other mirrors...');
exclude.push(err.pointer.farmer.nodeID);
module.exports.download.call(
self,
bucket,
id,
filepath,
{ exclude: env.exclude.join(',')}
);
});
}).pipe(through(function(chunk) {
received += chunk.length;
log('info', 'Received %s of %s bytes', [received, stream._length]);
this.queue(chunk);
})).pipe(decrypter).pipe(target);
});
});
});

};

module.exports.stream = function(bucket, id, env) {
Expand Down
11 changes: 8 additions & 3 deletions bin/storj.js
Expand Up @@ -236,9 +236,14 @@ program
.action(utils.provefile.bind(program));

program
.command('reset-keyring')
.description('reset the keyring password')
.action(utils.resetkeyring.bind(program));
.command('change-keyring')
.description('change the keyring password')
.action(utils.changekeyring.bind(program));

program
.command('reset-keyring')
.description('delete the current keyring and start a new one')
.action(utils.resetkeyring.bind(program));

program
.command('sign-message <privatekey> <message>')
Expand Down
37 changes: 36 additions & 1 deletion bin/utils.js
Expand Up @@ -8,6 +8,7 @@ var prompt = require('prompt');
var os = require('os');
var tmp = require('tmp');
var assert = require('assert');
var rimraf = require('rimraf');

var HOME = platform !== 'win32' ? process.env.HOME : process.env.USERPROFILE;
var DATADIR = path.join(HOME, '.storjcli');
Expand Down Expand Up @@ -192,7 +193,7 @@ module.exports.exportkeyring = function(directory) {
});
};

module.exports.resetkeyring = function() {
module.exports.changekeyring = function() {
var keypass = this._storj.getKeyPass();

module.exports.getKeyRing(keypass, function(keyring) {
Expand Down Expand Up @@ -222,6 +223,40 @@ module.exports.resetkeyring = function() {
});
};

module.exports.resetkeyring = function() {

log('info', 'You may lose access to all your stored data if you do this.');
log('info', 'I recommend you run `storj keyring-export` before deletion.');

function destroyKeyRing() {
rimraf.sync(path.join(DATADIR, 'key.ring/'));
module.exports.getNewPassword(
'Enter a password for your new keyring',
function(err, result) {
try {
storj.KeyRing(DATADIR, result.password);
} catch (err) {
return log('error', 'Could not create keyring, bad password?');
}
log('info', 'Successfully created a new key ring.');
}
);
}

function confirm() {
module.exports.getConfirmation(
'Are REALLY you sure you want to destroy your keyring?',
destroyKeyRing
);
}

module.exports.getConfirmation(
'Are you sure you want to destroy your keyring?',
confirm
);

};

module.exports.generatekey = function(env) {
var keypair = storj.KeyPair();

Expand Down

0 comments on commit 4244101

Please sign in to comment.