Permalink
Browse files

get initial file copying to work

  • Loading branch information...
maxogden committed Dec 4, 2015
1 parent 0ce2279 commit f278c4fe64c3c91d8ebf4c628b296d6a7e901973
Showing with 90 additions and 8 deletions.
  1. +2 −1 .gitignore
  2. +7 −5 cli.js
  3. +80 −2 index.js
  4. +1 −0 package.json
View
@@ -2,4 +2,5 @@ node_modules
.DS_Store
tmp
.idea
data
data
data2
View
12 cli.js
@@ -8,15 +8,17 @@ var cmd = args._[0]
run()
function run () {
var loc = args._[1] || process.cwd()
if (cmd === 'share') {
// share
var loc = args._[1] || process.cwd()
var db = dat(loc)
db.share()
var db1 = dat(loc, {datPath: './.dat1'})
db1.share()
} else if (cmd) {
// download
var hash = args._[1]
if (!hash) return usage()
var hash = args._[0]
if (!hash) return usage('root.txt')
var db2 = dat(loc, {datPath: './.dat2'})
db2.download(hash)
} else {
return usage('root.txt')
}
View
@@ -1,3 +1,4 @@
var net = require('net')
var fs = require('fs')
var path = require('path')
var walker = require('folder-walker')
@@ -8,6 +9,8 @@ var xtend = require('xtend')
var mkdirp = require('mkdirp')
var through = require('through2')
var pump = require('pump')
var discoveryChannel = require('discovery-channel')
var series = require('run-series')
module.exports = Dat
@@ -19,14 +22,16 @@ function Dat (dir, opts) {
createIfMissing: true
}
opts = xtend(opts, defaults)
var datPath = path.join(homedir(), '.dat/db')
var datPath = opts.datPath || path.join(homedir(), '.dat/db')
if (opts.createIfMissing) mkdirp.sync(datPath)
var hyperdriveOpts = {name: 'dat'}
var drive = hyperdrive(level(datPath, opts), hyperdriveOpts)
this.drive = drive
this.discovery = discoveryChannel({dht: false})
}
Dat.prototype.share = function () {
var self = this
var stream = walker(this.dir, {filter: function (filename) {
var basename = path.basename(filename)
if (basename[0] === '.') return false // ignore hidden files and folders
@@ -47,7 +52,80 @@ Dat.prototype.share = function () {
pump(stream, adder, function (err) {
if (err) throw err
pack.finalize(function () {
console.log('added stuff')
var link = pack.id.toString('hex')
console.log(link, '<-- this is your hyperdrive link')
self.serve(link, function (err, port, close) {
if (err) throw err
console.log('Sharing on', port)
})
})
})
}
Dat.prototype.serve = function (link, cb) {
var self = this
var server = net.createServer(function (socket) {
socket.pipe(self.drive.createPeerStream()).pipe(socket)
})
server.listen(0, function (err) {
if (err) return cb(err)
var port = server.address().port
function ann () {
// discovery-channel currently only works with 20 bytes hashes
self.discovery.announce(link.slice(0, 20), port)
}
ann()
var interval = setInterval(ann, 10000)
function close (cb) {
clearInterval(interval)
server.close(cb)
}
cb(null, port, close)
})
}
Dat.prototype.download = function (link) {
var self = this
var lookup = self.discovery.lookup(link.slice(0, 20))
lookup.on('peer', function (ip, port) {
console.log('found peer')
var socket = net.connect(port, ip)
socket.pipe(self.drive.createPeerStream()).pipe(socket)
})
var feed = self.drive.get(link) // the link identifies/verifies the content
var next = 0
downloadNext()
function downloadNext () {
feed.get(next, function (err, entry) {
if (err) throw err
console.log('entry', entry)
var content = self.drive.get(entry.link)
var gets = []
var writeStream = fs.createWriteStream(entry.value.name, {mode: entry.value.mode})
for (var i = 0; i < entry.link.blocks; i++) {
var download = (function (piece) {
return function (cb) {
content.get(piece, function (err, data) {
writeStream.write(data)
cb(err)
})
}
})(i)
gets.push(download)
}
series(gets, function (err) {
if (err) throw err
writeStream.end()
next++
downloadNext()
})
})
}
}
View
@@ -26,6 +26,7 @@
"author": "max ogden",
"license": "BSD-3-Clause",
"dependencies": {
"discovery-channel": "^1.2.0",
"folder-walker": "^1.1.2",
"level": "^1.3.0",
"minimist": "^1.2.0",

0 comments on commit f278c4f

Please sign in to comment.