Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added upload and download helpers

  • Loading branch information...
commit 3bfdeaff64f62b1795446c33d2daa589ac9ef75a 1 parent 056f19c
Damon Oehlman authored
34 README.md
Source Rendered
@@ -2,7 +2,7 @@
2 2
3 3 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.
4 4
5   -## Reading
  5 +## Downloading From Couch to FileSystem
6 6
7 7 ### Example: Download Attachments to the Filesystem
8 8
@@ -18,7 +18,22 @@ var attachmate = require('attachmate'),
18 18 r.pipe(w);
19 19 ```
20 20
21   -## Writing
  21 +### Example: Download Attachments (using download helper)
  22 +
  23 +```js
  24 +var attachmate = require('attachmate'),
  25 + path = require('path');
  26 +
  27 +attachmate.download(
  28 + 'http://localhost:5984/testdb/doc_with_attachments',
  29 + path.resolve(__dirname, 'output'),
  30 + function(err) {
  31 + console.log('done, error = ', err);
  32 + }
  33 +);
  34 +```
  35 +
  36 +## Uploading from Filesystem to Couch
22 37
23 38 ### Options
24 39
@@ -41,3 +56,18 @@ var attachmate = require('attachment'),
41 56 // upload the contents of the input directory as attachments
42 57 r.pipe(w);
43 58 ```
  59 +
  60 +### Example: Upload Attachments (using upload helper)
  61 +
  62 +```js
  63 +var attachmate = require('attachmate'),
  64 + path = require('path');
  65 +
  66 +attachmate.upload(
  67 + 'http://localhost:5984/testdb/test',
  68 + path.resolve(__dirname, 'input'),
  69 + function(err) {
  70 + console.log('done, error = ', err);
  71 + }
  72 +);
  73 +```
10 examples/download-short.js
... ... @@ -0,0 +1,10 @@
  1 +var attachmate = require('../'),
  2 + path = require('path');
  3 +
  4 +attachmate.download(
  5 + 'http://10.211.55.4:5984/steelmesh/test',
  6 + path.resolve(__dirname, 'output'),
  7 + function(err) {
  8 + console.log('done, error = ', err);
  9 + }
  10 +);
10 examples/upload-short.js
... ... @@ -0,0 +1,10 @@
  1 +var attachmate = require('../'),
  2 + path = require("path");
  3 +
  4 +attachmate.upload(
  5 + 'http://10.211.55.4:5984/steelmesh/test',
  6 + path.resolve(__dirname, 'input'),
  7 + function(err) {
  8 + console.log('done, error = ', err);
  9 + }
  10 +);
57 index.js
... ... @@ -1,2 +1,55 @@
1   -exports.Reader = require('./lib/reader');
2   -exports.Writer = require('./lib/writer');
  1 +var fstream = require('fstream'),
  2 + Reader = exports.Reader = require('./lib/reader'),
  3 + Writer = exports.Writer = require('./lib/writer');
  4 +
  5 +exports.download = function(srcDoc, targetPath, opts, callback) {
  6 + var src, dst;
  7 +
  8 + // if the options is the callback, then remap
  9 + if (typeof opts == 'function') {
  10 + callback = opts;
  11 + opts = {};
  12 + }
  13 +
  14 + // ensure we have a callback
  15 + callback = callback || function() {};
  16 +
  17 + // create the source and dest readers / writers
  18 + src = new Reader({ path: srcDoc });
  19 + dst = fstream.Writer({ path: targetPath, type: 'Directory'});
  20 +
  21 + // wire callbacks
  22 + dst.on('end', callback);
  23 + dst.on('error', callback);
  24 +
  25 + // pipe from the couch doc to the target dir
  26 + src.pipe(dst);
  27 +};
  28 +
  29 +exports.upload = function(targetDoc, sourcePath, opts, callback) {
  30 + var src, dst;
  31 +
  32 + // if the options is the callback, then remap
  33 + if (typeof opts == 'function') {
  34 + callback = opts;
  35 + opts = {};
  36 + }
  37 +
  38 + // ensure we have a callback
  39 + callback = callback || function() {};
  40 +
  41 + // add the target doc to the opts
  42 + opts.path = targetDoc;
  43 +
  44 + // create the source and destination readers and writers
  45 + src = fstream.Reader({ path: sourcePath, type: 'Directory' });
  46 + dst = new Writer(opts);
  47 +
  48 + // wire callbacks
  49 + dst.on('end', callback);
  50 + dst.on('error', callback);
  51 +
  52 + // pipe the attachments to the directory
  53 + src.pipe(dst);
  54 +};
  55 +
14 lib/reader.js
... ... @@ -1,4 +1,5 @@
1   -var request = require('request'),
  1 +var debug = require('debug')('attachmate'),
  2 + request = require('request'),
2 3 util = require('util'),
3 4 Reader = require('fstream').Reader,
4 5 HttpRequestEntry = require('./requestentry'),
@@ -8,7 +9,6 @@ function AttachmentReader(props) {
8 9 // TODO: check the path property is a url
9 10 this.path = props.path;
10 11
11   -
12 12 this._entries = null;
13 13 this._index = -1;
14 14 this._ended = false;
@@ -59,10 +59,10 @@ AttachmentReader.prototype._read = function() {
59 59 this.emit('close');
60 60 }
61 61
62   - return;
  62 + return undefined;
63 63 }
64   -
65   - console.log(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
  64 +
  65 + debug('found: ' + this.path.replace(reTrailingSlash, '') + this._entries[this._index]);
66 66 entry = new HttpRequestEntry(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
67 67 this.emit('entry', entry);
68 68
@@ -71,8 +71,10 @@ AttachmentReader.prototype._read = function() {
71 71 });
72 72
73 73 entry.on('error', function(err) {
74   - console.log(err);
  74 + reader.emit(err);
75 75 });
  76 +
  77 + return undefined;
76 78 };
77 79
78 80 module.exports = AttachmentReader;
1  lib/writer.js
@@ -70,6 +70,7 @@ AttachmentWriter.prototype.end = function() {
70 70 // if the current document does not have an id, then give it one
71 71 request({ url: writer.path, method: 'PUT', json: currentDoc }, function(err, res, body) {
72 72 debug('uploaded doc ' + (typeof body == 'object' ? body.id + '/' + body.rev : ''));
  73 + writer.emit('end');
73 74 });
74 75 }
75 76 });

0 comments on commit 3bfdeaf

Please sign in to comment.
Something went wrong with that request. Please try again.