Skip to content

Commit 1ed5374

Browse files
committed
fixed with help from ipfs team ipfs/js-ipfs#1024
1 parent b2f6f6a commit 1ed5374

File tree

1 file changed

+60
-93
lines changed

1 file changed

+60
-93
lines changed

storageServer.js

Lines changed: 60 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -21,108 +21,75 @@ var HOST = '0.0.0.0';
2121

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

25+
var ipfsNode = new IPFS()
2626

27-
https.createServer(https_options, function (req, res) {
28-
if (req.url == '/fileupload' && req.method == 'POST') {
29-
var form = new formidable.IncomingForm();
27+
ipfsNode.on('ready', (err) => {
28+
if (err) {
29+
console.error(err)
30+
process.exit(1)
31+
}
32+
33+
34+
var server = https.createServer(https_options, function (req, res) {
35+
if (req.url == '/fileupload' && req.method == 'POST') {
36+
var form = new formidable.IncomingForm();
3037

31-
// form parsing
32-
form.parse(req, function (err, fields, files) {
33-
if(typeof(ipfsNode)==='undefined'){console.log('IPFS is undefined.')}else{console.log('IPFS is defined.')}
34-
var ipfsNode = new IPFS()
38+
// form parsing
39+
form.parse(req, function (err, fields, files) {
3540

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

45-
// create read stream to encrypt and send to ipfs
46-
var rstream = fs.createReadStream(newpath);
47-
48-
// Upload to IPFS
49-
// Create the File to add, a file consists of a path + content. More details on
50-
// https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
51-
// asynchronous server startup and upload
52-
let fileMultihash
5346

54-
series(
55-
[
56-
// 1. load ipfs server
57-
(cb) => ipfsNode.on('ready', (cb)),
58-
// 2. After loading ipfs
59-
(cb) => ipfsNode.files.add({
60-
path: 'private_data_if_no_permission_destroy.encr',
61-
content: rstream.pipe(aes)
62-
},
63-
// When file loaded
64-
(err, result) => {
65-
if (err) { return cb(err) }
66-
console.log('\nAdded file:', result[0].path, result[0].hash)
67-
fileMultihash = result[0].hash
68-
var b =
69-
'<h1> Your information is now encrypted in the interplanetary file system</h1>'
70-
+'<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>'
71-
+'<h2> Only you have its password. If shared with this location, it gives access to your data. Share wisely.</h2>';
72-
73-
res.write(b)
74-
// IPFS Core exposed components
75-
// // - for booting up a node
76-
//console.log('\nNode info on property init:', ipfsNode.init() )
77-
//console.log('\nNode info on property preStart:', ipfsNode.preStart() )
78-
//console.log('\nNode info on property start:', ipfsNode.start() )
79-
//console.log('\nNode info on property stop:', ipfsNode.stop() )
80-
console.log('Node info on property isOnline:', ipfsNode.isOnline() )
81-
// - interface-ipfs-core defined API
82-
console.log('\nNode info on property version:', ipfsNode.version() )
83-
console.log('\nNode info on property id:', ipfsNode.id() )
84-
// console.log('\nNode info on property repo:', ipfsNode.repo )
85-
// console.log('\nNode info on property bootstrap:', ipfsNode.bootstrap)
86-
// console.log('\nNode info on property config:', ipfsNode.config )
87-
// console.log('\nNode info on property block:', ipfsNode.block )
88-
// console.log('\nNode info on property object:', ipfsNode.object )
89-
// console.log('\nNode info on property dag:', ipfsNode.dag )
90-
// console.log('\nNode info on property libp2p:', ipfsNode.libp2p )
91-
// console.log('\nNode info on property swarm:', ipfsNode.swarm )
92-
// console.log('\nNode info on property files:', ipfsNode.files )
93-
// console.log('\nNode info on property bitswap:', ipfsNode.bitswap )
94-
// console.log('\nNode info on property ping:', ipfsNode.ping)
95-
// console.log('\nNode info on property pubsub:', ipfsNode.pubsub )
96-
// console.log('\nNode info on property dht:', ipfsNode.dht )
47+
// Upload to IPFS
48+
// Create the File to add, a file consists of a path + content. More details on
49+
// https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
50+
// asynchronous server startup and upload
51+
let fileMultihash
9752

98-
cb()
99-
}
100-
)
101-
]
102-
,
103-
() => {
104-
if(typeof(ipfsNode)==='undefined'){console.log('In callback: IPFS is undefined.')}else{console.log('In callback: IPFS is defined.')}
105-
return res.end()}
106-
)
53+
ipfsNode.files.add({
54+
path: 'private_data_if_no_permission_destroy.encr',
55+
content: rstream
56+
},
57+
// When file loaded
58+
(err, result) => {
59+
if (err) { return cb(err) }
60+
console.log('\nAdded file:', result[0].path, result[0].hash)
61+
fileMultihash = result[0].hash
62+
res.write(
63+
'<h1> Your information is now encrypted in the interplanetary file system</h1>' +
64+
'<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>' +
65+
'<h2> Only you have its password. If shared with this location, it gives access to your data. Share wisely.</h2>',
66+
res.end.bind(res))
67+
}
68+
)
69+
})
70+
}else {
71+
// Upload form
72+
res.writeHead(200, {'Content-Type': 'text/html'});
73+
res.write('<h1> Store and send encrypted content to anyone</h1>');
74+
res.write('<h2>Encrypt and store in always-available, non-erasable peer-to-peer storage <br> Access and share with keys on www.</h2>');
75+
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
76+
res.write('<input type="file" name="filetoupload"><br>');
77+
res.write('<input type="submit">');
78+
res.write('</form>');
79+
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>');
80+
res.write('<p>Only you, and whomever you share the key with, has access</p>');
81+
res.write('<p>* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control. <p>');
82+
return res.end();
83+
}
84+
}).on('listening', (err) => {
85+
if (err) {
86+
console.error(err)
87+
process.exit(1)
10788
}
108-
)
109-
}
110-
else {
111-
// Upload form
112-
res.writeHead(200, {'Content-Type': 'text/html'});
113-
res.write('<h1> Store and send encrypted content to anyone</h1>');
114-
res.write('<h2>Encrypt and store in always-available, non-erasable peer-to-peer storage <br> Access and share with keys on www.</h2>');
115-
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
116-
res.write('<input type="file" name="filetoupload"><br>');
117-
res.write('<input type="submit">');
118-
res.write('</form>');
119-
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>');
120-
res.write('<p>Only you, and whomever you share the key with, has access</p>');
121-
res.write('<p>* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control. <p>');
122-
return res.end();
123-
}
124-
}).listen(PORT, HOST);
12589

90+
console.log('HTTPS Server listening on %s:%s', HOST, PORT)
91+
}).listen(PORT, HOST);
92+
})
12693
// start server
12794
//server = https.createServer(https_options, app).listen(PORT, HOST);
12895
console.log('HTTPS Server listening on %s:%s', HOST, PORT);

0 commit comments

Comments
 (0)