Skip to content

Commit

Permalink
Version 1.0.1 (#36)
Browse files Browse the repository at this point in the history
* Correct ls to return array of file path's instead of the whole document object
* Correct read and rm to use fd as a string instead of an object
* Ability to use callback function for read instead of only a stream
* Fix some readme errors
  • Loading branch information
willhuang85 authored Dec 4, 2018
1 parent 8784090 commit e195af0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

GridFS adapter for receiving [upstreams](https://github.com/balderdashy/skipper#what-are-upstreams). Particularly useful for handling streaming multipart file uploads from the [Skipper](https://github.com/balderdashy/skipper) body parser.

Currently only supports Node 6 and up


========================================

Expand All @@ -27,7 +29,7 @@ Also make sure you have skipper [installed as your body parser](http://beta.sail
req.file('avatar')
.upload({
adapter: require('skipper-gridfs'),
uri: 'mongodb://jimmy@[email protected].com:27017/myBucket'
uri: 'mongodb://username:password@myhost.com:27017/myDatabase'
}, function whenDone(err, uploadedFiles) {
if (err) return res.negotiate(err);
else return res.ok({
Expand Down
51 changes: 33 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const mongodb = require('mongodb');
const path = require('path');
const mime = require('mime');
const _ = require('lodash');
const concat = require('concat-stream');



const client = (uri, mongoOptions, fn) => {
Expand Down Expand Up @@ -35,7 +37,7 @@ module.exports = function SkipperGridFS(globalOptions) {
errorHandler(err, client);
}

bucket(client.db(), options.bucketOptions).delete(fd._id, errorHandler);
bucket(client.db(), options.bucketOptions).delete(fd, (err) => errorHandler(err, client));
if (cb) cb();
});
}
Expand All @@ -46,31 +48,38 @@ module.exports = function SkipperGridFS(globalOptions) {
if (cb) cb(err);
}

const __transform__ = Transform();
const __transform__ = Transform({ objectMode: true });
__transform__._transform = (chunk, encoding, callback) => {
return callback(null, chunk);
return callback(null, chunk._id ? chunk._id : null);
};

__transform__.once('done', (client) => {
client.close();
});


client(options.uri, options.mongoOptions, (err, client) => {
if (err) {
errorHandler(err, client);
}

const cursor = bucket(client.db(), options.bucketOptions).find({ 'metadata.dirname': dirpath })
if (cb) {
cursor.toArray((err, documents) => {
if (err) {
errorHandler(err, client);
}
client.close();
cb(null, documents);
});
} else {
cursor.pipe(__transform__);
}
const stream = bucket(client.db(), options.bucketOptions).find({ 'metadata.dirname': dirpath }).transformStream();
stream.once('error', (err) => {
errorHandler(err, client);
});

stream.once('end', () => {
__transform__.emit('done', client);
});

stream.pipe(__transform__);
});

if (!cb) {
if (cb) {
__transform__.pipe(concat((data) => {
return cb(null, Array.isArray(data) ? data : [data]);
}));
} else {
return __transform__;
}
}
Expand All @@ -95,15 +104,21 @@ module.exports = function SkipperGridFS(globalOptions) {
__transform__.emit('error', error, client);
}

const downloadStream = bucket(client.db(), options.bucketOptions).openDownloadStream(fd._id);
const downloadStream = bucket(client.db(), options.bucketOptions).openDownloadStream(fd);
downloadStream.once('end', () => {
__transform__.emit('done', client);
});

downloadStream.pipe(__transform__);
});

return __transform__;
if (cb) {
__transform__.pipe(concat((data) => {
return cb(null, data);
}));
} else {
return __transform__;
}
}

adapter.receive = (opts) => {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "skipper-gridfs",
"version": "1.0.0",
"version": "1.0.1",
"description": "A skipper adapter to allow uploading files to MongoDB's GridFS",
"main": "index.js",
"scripts": {
Expand All @@ -27,8 +27,9 @@
"skipper-adapter-tests": "github:willhuang85/skipper-adapter-tests#master"
},
"dependencies": {
"concat-stream": "^1.6.2",
"lodash": "^4.17.11",
"mime": "^2.3.1",
"mongodb": "^3.1.8"
}
}
}

0 comments on commit e195af0

Please sign in to comment.