Permalink
Comparing changes
Open a pull request
- 3 commits
- 3 files changed
- 0 commit comments
- 1 contributor
Unified
Split
Showing
with
32 additions
and 16 deletions.
- +15 −0 fs.js
- +16 −16 index.js
- +1 −0 package.json
| @@ -1,4 +1,7 @@ | ||
| var fs = require('fs') | ||
| var pump = require('pump') | ||
| var mkdirp = require('mkdirp') | ||
| var through = require('through2') | ||
| var path = require('path') | ||
| var walker = require('folder-walker') | ||
| var each = require('stream-each') | ||
| @@ -28,3 +31,15 @@ module.exports.listEach = function (opts, onEach, cb) { | ||
| onEach(item, next) | ||
| }, cb) | ||
| } | ||
| module.exports.createDownloadStream = function (drive, dir) { | ||
| return through.obj(function (entry, enc, next) { | ||
| var entryPath = path.join(dir, entry.value.name) | ||
| mkdirp.sync(path.dirname(entryPath)) | ||
| var content = drive.get(entry) | ||
| var writeStream = fs.createWriteStream(entryPath, {mode: entry.value.mode}) | ||
| pump(content.createStream(), writeStream, function (err) { | ||
| next(err) | ||
| }) | ||
| }) | ||
| } | ||
| @@ -1,9 +1,7 @@ | ||
| var net = require('net') | ||
| var fs = require('fs') | ||
| var collect = require('collect-stream') | ||
| var path = require('path') | ||
| var hyperdrive = require('hyperdrive') | ||
| var mkdirp = require('mkdirp') | ||
| var through = require('through2') | ||
| var pump = require('pump') | ||
| var series = require('run-series') | ||
| var homeDir = require('home-dir') | ||
| @@ -27,6 +25,7 @@ function Dat (opts) { | ||
| } | ||
| Dat.prototype.add = function (dirs, cb) { | ||
| var self = this | ||
| if (!dirs) throw new Error('must specify directory or directories to add') | ||
| if (!cb) throw new Error('must specify callback') | ||
| @@ -37,7 +36,7 @@ Dat.prototype.add = function (dirs, cb) { | ||
| var tasks = dirs.map(function (dir) { | ||
| return function (cb) { | ||
| datFs.listEach({dir: dir}, eachItem, cb) | ||
| self.fs.listEach({dir: dir}, eachItem, cb) | ||
| } | ||
| }) | ||
| @@ -111,7 +110,18 @@ Dat.prototype.close = function (cb) { | ||
| this.discovery.close(cb) | ||
| } | ||
| // TODO remove fs specific code from this method | ||
| Dat.prototype.metadata = function (link, cb) { | ||
| var self = this | ||
| self.joinTcpSwarm(link, function (_err, link, port, close) { | ||
| var feed = self.drive.get(link) | ||
| collect(feed.createStream(), function (err, data) { | ||
| cb(err, data) | ||
| // TODO: instead of closing, return the swarm. | ||
| close() | ||
| }) | ||
| }) | ||
| } | ||
| Dat.prototype.download = function (link, dir, cb) { | ||
| var self = this | ||
| if (!cb) cb = function noop () {} | ||
| @@ -121,17 +131,7 @@ Dat.prototype.download = function (link, dir, cb) { | ||
| var feed = self.drive.get(link) // the link identifies/verifies the content | ||
| var feedStream = feed.createStream() | ||
| var download = through.obj(function (entry, enc, next) { | ||
| var entryPath = path.join(dir, entry.value.name) | ||
| mkdirp.sync(path.dirname(entryPath)) | ||
| var content = self.drive.get(entry) | ||
| var writeStream = fs.createWriteStream(entryPath, {mode: entry.value.mode}) | ||
| pump(content.createStream(), writeStream, function (err) { | ||
| next(err) | ||
| }) | ||
| }) | ||
| var download = self.fs.createDownloadStream(self.drive, dir) | ||
| pump(feedStream, download, function (err) { | ||
| cb(err, link, port, close) | ||
| }) | ||
| @@ -23,6 +23,7 @@ | ||
| "author": "max ogden", | ||
| "license": "BSD-3-Clause", | ||
| "dependencies": { | ||
| "collect-stream": "^1.1.1", | ||
| "connections": "^1.0.0", | ||
| "discovery-channel": "^1.2.1", | ||
| "folder-walker": "^1.1.2", | ||