Skip to content

Commit

Permalink
fixed with help from ipfs team ipfs/js-ipfs#1024
Browse files Browse the repository at this point in the history
  • Loading branch information
davidweisss committed Sep 27, 2017
1 parent b2f6f6a commit 1ed5374
Showing 1 changed file with 60 additions and 93 deletions.
153 changes: 60 additions & 93 deletions storageServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,108 +21,75 @@ var HOST = '0.0.0.0';

// Nodejs encryption of buffers
var privateKey = new Buffer('my secret');
var aes = crypto.createCipher('aes-256-cbc', privateKey);

var ipfsNode = new IPFS()

https.createServer(https_options, function (req, res) {
if (req.url == '/fileupload' && req.method == 'POST') {
var form = new formidable.IncomingForm();
ipfsNode.on('ready', (err) => {
if (err) {
console.error(err)
process.exit(1)
}


var server = https.createServer(https_options, function (req, res) {
if (req.url == '/fileupload' && req.method == 'POST') {
var form = new formidable.IncomingForm();

// form parsing
form.parse(req, function (err, fields, files) {
if(typeof(ipfsNode)==='undefined'){console.log('IPFS is undefined.')}else{console.log('IPFS is defined.')}
var ipfsNode = new IPFS()
// form parsing
form.parse(req, function (err, fields, files) {

console.log('New IPFS node spawned!')
console.log('\nNode info on property isOnline:', ipfsNode.isOnline() )
// Move to upload directory
var oldpath = files.filetoupload.path;
var newpath = '/home/davidweisss/DNA-IDstorage/uploads/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
});
// create read stream to encrypt and send to ipfs
var rstream = fs
.createReadStream(files.filetoupload.path)
.pipe(crypto.createCipher('aes-256-cbc', privateKey))

// create read stream to encrypt and send to ipfs
var rstream = fs.createReadStream(newpath);

// Upload to IPFS
// Create the File to add, a file consists of a path + content. More details on
// https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
// asynchronous server startup and upload
let fileMultihash

series(
[
// 1. load ipfs server
(cb) => ipfsNode.on('ready', (cb)),
// 2. After loading ipfs
(cb) => ipfsNode.files.add({
path: 'private_data_if_no_permission_destroy.encr',
content: rstream.pipe(aes)
},
// When file loaded
(err, result) => {
if (err) { return cb(err) }
console.log('\nAdded file:', result[0].path, result[0].hash)
fileMultihash = result[0].hash
var b =
'<h1> Your information is now encrypted in the interplanetary file system</h1>'
+'<h2>Your data is immortalized, and encrypted <a href="https://ipfs.io/ipfs/'+ fileMultihash +'">here</a>. This is the Multi hash of your file:'+fileMultihash+'</h2>'
+'<h2> Only you have its password. If shared with this location, it gives access to your data. Share wisely.</h2>';

res.write(b)
// IPFS Core exposed components
// // - for booting up a node
//console.log('\nNode info on property init:', ipfsNode.init() )
//console.log('\nNode info on property preStart:', ipfsNode.preStart() )
//console.log('\nNode info on property start:', ipfsNode.start() )
//console.log('\nNode info on property stop:', ipfsNode.stop() )
console.log('Node info on property isOnline:', ipfsNode.isOnline() )
// - interface-ipfs-core defined API
console.log('\nNode info on property version:', ipfsNode.version() )
console.log('\nNode info on property id:', ipfsNode.id() )
// console.log('\nNode info on property repo:', ipfsNode.repo )
// console.log('\nNode info on property bootstrap:', ipfsNode.bootstrap)
// console.log('\nNode info on property config:', ipfsNode.config )
// console.log('\nNode info on property block:', ipfsNode.block )
// console.log('\nNode info on property object:', ipfsNode.object )
// console.log('\nNode info on property dag:', ipfsNode.dag )
// console.log('\nNode info on property libp2p:', ipfsNode.libp2p )
// console.log('\nNode info on property swarm:', ipfsNode.swarm )
// console.log('\nNode info on property files:', ipfsNode.files )
// console.log('\nNode info on property bitswap:', ipfsNode.bitswap )
// console.log('\nNode info on property ping:', ipfsNode.ping)
// console.log('\nNode info on property pubsub:', ipfsNode.pubsub )
// console.log('\nNode info on property dht:', ipfsNode.dht )
// Upload to IPFS
// Create the File to add, a file consists of a path + content. More details on
// https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
// asynchronous server startup and upload
let fileMultihash

cb()
}
)
]
,
() => {
if(typeof(ipfsNode)==='undefined'){console.log('In callback: IPFS is undefined.')}else{console.log('In callback: IPFS is defined.')}
return res.end()}
)
ipfsNode.files.add({
path: 'private_data_if_no_permission_destroy.encr',
content: rstream
},
// When file loaded
(err, result) => {
if (err) { return cb(err) }
console.log('\nAdded file:', result[0].path, result[0].hash)
fileMultihash = result[0].hash
res.write(
'<h1> Your information is now encrypted in the interplanetary file system</h1>' +
'<h2>Your data is immortalized, and encrypted <a href="https://ipfs.io/ipfs/' + result[0].hash + '">here</a>. This is the Multi hash of your file:' + fileMultihash + '</h2>' +
'<h2> Only you have its password. If shared with this location, it gives access to your data. Share wisely.</h2>',
res.end.bind(res))
}
)
})
}else {
// Upload form
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1> Store and send encrypted content to anyone</h1>');
res.write('<h2>Encrypt and store in always-available, non-erasable peer-to-peer storage <br> Access and share with keys on www.</h2>');
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
res.write('<p>secure https server encrypting your info then uploading it to permanent peer to peer storage network (ipfs). <br> </br> Gives you a web adress (ipfs) and a password to decrypt your data</p>');
res.write('<p>Only you, and whomever you share the key with, has access</p>');
res.write('<p>* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control. <p>');
return res.end();
}
}).on('listening', (err) => {
if (err) {
console.error(err)
process.exit(1)
}
)
}
else {
// Upload form
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1> Store and send encrypted content to anyone</h1>');
res.write('<h2>Encrypt and store in always-available, non-erasable peer-to-peer storage <br> Access and share with keys on www.</h2>');
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
res.write('<p>secure https server encrypting your info then uploading it to permanent peer to peer storage network (ipfs). <br> </br> Gives you a web adress (ipfs) and a password to decrypt your data</p>');
res.write('<p>Only you, and whomever you share the key with, has access</p>');
res.write('<p>* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control. <p>');
return res.end();
}
}).listen(PORT, HOST);

console.log('HTTPS Server listening on %s:%s', HOST, PORT)
}).listen(PORT, HOST);
})
// start server
//server = https.createServer(https_options, app).listen(PORT, HOST);
console.log('HTTPS Server listening on %s:%s', HOST, PORT);
Expand Down

0 comments on commit 1ed5374

Please sign in to comment.