Permalink
Browse files

Added upload and download helpers

  • Loading branch information...
1 parent 056f19c commit 3bfdeaff64f62b1795446c33d2daa589ac9ef75a @DamonOehlman committed Dec 2, 2011
Showing with 116 additions and 10 deletions.
  1. +32 −2 README.md
  2. +10 −0 examples/download-short.js
  3. +10 −0 examples/upload-short.js
  4. +55 −2 index.js
  5. +8 −6 lib/reader.js
  6. +1 −0 lib/writer.js
View
@@ -2,7 +2,7 @@
An experiment in using @isaacs useful [fstream](https://github.com/isaacs/fstream) project to stream files in and out of [CouchDB](http://couchdb.apache.org/) as attachments to documents.
-## Reading
+## Downloading From Couch to FileSystem
### Example: Download Attachments to the Filesystem
@@ -18,7 +18,22 @@ var attachmate = require('attachmate'),
r.pipe(w);
```
-## Writing
+### Example: Download Attachments (using download helper)
+
+```js
+var attachmate = require('attachmate'),
+ path = require('path');
+
+attachmate.download(
+ 'http://localhost:5984/testdb/doc_with_attachments',
+ path.resolve(__dirname, 'output'),
+ function(err) {
+ console.log('done, error = ', err);
+ }
+);
+```
+
+## Uploading from Filesystem to Couch
### Options
@@ -41,3 +56,18 @@ var attachmate = require('attachment'),
// upload the contents of the input directory as attachments
r.pipe(w);
```
+
+### Example: Upload Attachments (using upload helper)
+
+```js
+var attachmate = require('attachmate'),
+ path = require('path');
+
+attachmate.upload(
+ 'http://localhost:5984/testdb/test',
+ path.resolve(__dirname, 'input'),
+ function(err) {
+ console.log('done, error = ', err);
+ }
+);
+```
@@ -0,0 +1,10 @@
+var attachmate = require('../'),
+ path = require('path');
+
+attachmate.download(
+ 'http://10.211.55.4:5984/steelmesh/test',
+ path.resolve(__dirname, 'output'),
+ function(err) {
+ console.log('done, error = ', err);
+ }
+);
@@ -0,0 +1,10 @@
+var attachmate = require('../'),
+ path = require("path");
+
+attachmate.upload(
+ 'http://10.211.55.4:5984/steelmesh/test',
+ path.resolve(__dirname, 'input'),
+ function(err) {
+ console.log('done, error = ', err);
+ }
+);
View
@@ -1,2 +1,55 @@
-exports.Reader = require('./lib/reader');
-exports.Writer = require('./lib/writer');
+var fstream = require('fstream'),
+ Reader = exports.Reader = require('./lib/reader'),
+ Writer = exports.Writer = require('./lib/writer');
+
+exports.download = function(srcDoc, targetPath, opts, callback) {
+ var src, dst;
+
+ // if the options is the callback, then remap
+ if (typeof opts == 'function') {
+ callback = opts;
+ opts = {};
+ }
+
+ // ensure we have a callback
+ callback = callback || function() {};
+
+ // create the source and dest readers / writers
+ src = new Reader({ path: srcDoc });
+ dst = fstream.Writer({ path: targetPath, type: 'Directory'});
+
+ // wire callbacks
+ dst.on('end', callback);
+ dst.on('error', callback);
+
+ // pipe from the couch doc to the target dir
+ src.pipe(dst);
+};
+
+exports.upload = function(targetDoc, sourcePath, opts, callback) {
+ var src, dst;
+
+ // if the options is the callback, then remap
+ if (typeof opts == 'function') {
+ callback = opts;
+ opts = {};
+ }
+
+ // ensure we have a callback
+ callback = callback || function() {};
+
+ // add the target doc to the opts
+ opts.path = targetDoc;
+
+ // create the source and destination readers and writers
+ src = fstream.Reader({ path: sourcePath, type: 'Directory' });
+ dst = new Writer(opts);
+
+ // wire callbacks
+ dst.on('end', callback);
+ dst.on('error', callback);
+
+ // pipe the attachments to the directory
+ src.pipe(dst);
+};
+
View
@@ -1,4 +1,5 @@
-var request = require('request'),
+var debug = require('debug')('attachmate'),
+ request = require('request'),
util = require('util'),
Reader = require('fstream').Reader,
HttpRequestEntry = require('./requestentry'),
@@ -8,7 +9,6 @@ function AttachmentReader(props) {
// TODO: check the path property is a url
this.path = props.path;
-
this._entries = null;
this._index = -1;
this._ended = false;
@@ -59,10 +59,10 @@ AttachmentReader.prototype._read = function() {
this.emit('close');
}
- return;
+ return undefined;
}
-
- console.log(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
+
+ debug('found: ' + this.path.replace(reTrailingSlash, '') + this._entries[this._index]);
entry = new HttpRequestEntry(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
this.emit('entry', entry);
@@ -71,8 +71,10 @@ AttachmentReader.prototype._read = function() {
});
entry.on('error', function(err) {
- console.log(err);
+ reader.emit(err);
});
+
+ return undefined;
};
module.exports = AttachmentReader;
View
@@ -70,6 +70,7 @@ AttachmentWriter.prototype.end = function() {
// if the current document does not have an id, then give it one
request({ url: writer.path, method: 'PUT', json: currentDoc }, function(err, res, body) {
debug('uploaded doc ' + (typeof body == 'object' ? body.id + '/' + body.rev : ''));
+ writer.emit('end');
});
}
});

0 comments on commit 3bfdeaf

Please sign in to comment.