-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
179 lines (144 loc) · 5.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const WritableStream = require('stream').Writable;
const Transform = require('stream').Transform;
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) => {
const opts = Object.assign({ useNewUrlParser: true }, mongoOptions);
mongodb.MongoClient.connect(uri, opts, fn);
}
const bucket = (db, bucketOptions) => {
const opts = Object.assign({}, bucketOptions);
return new mongodb.GridFSBucket(db, opts);
}
module.exports = function SkipperGridFS(globalOptions) {
const options = globalOptions || {};
_.defaults(options, {
uri: 'mongodb://localhost:27017/mydatabase'
});
const adapter = {};
adapter.rm = (fd, cb) => {
const errorHandler = (err, client) => {
if (client) client.close();
if (cb) cb(err);
}
client(options.uri, options.mongoOptions, (err, client) => {
if (err) {
errorHandler(err, client);
}
bucket(client.db(), options.bucketOptions).delete(fd, (err) => errorHandler(err, client));
if (cb) cb();
});
}
adapter.ls = (dirpath, cb) => {
const errorHandler = (err, client) => {
if (client) client.close();
if (cb) cb(err);
}
const __transform__ = Transform({ objectMode: true });
__transform__._transform = (chunk, encoding, callback) => {
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 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) {
__transform__.pipe(concat((data) => {
return cb(null, Array.isArray(data) ? data : [data]);
}));
} else {
return __transform__;
}
}
adapter.read = (fd, cb) => {
const __transform__ = Transform();
__transform__._transform = (chunk, encoding, callback) => {
return callback(null, chunk);
};
__transform__.once('error', (error, client) => {
if (client) client.close();
if (cb) cb(error);
});
__transform__.once('done', (client) => {
if (client) client.close();
});
client(options.uri, options.mongoOptions, (err, client) => {
if (err) {
__transform__.emit('error', error, client);
}
const downloadStream = bucket(client.db(), options.bucketOptions).openDownloadStream(fd);
downloadStream.once('end', () => {
__transform__.emit('done', client);
});
downloadStream.once('error', (error) => {
__transform__.emit('error', error, client);
});
downloadStream.pipe(__transform__);
});
if (cb) {
__transform__.pipe(concat((data) => {
return cb(null, data);
}));
} else {
return __transform__;
}
}
adapter.receive = (opts) => {
const receiver__ = WritableStream({ objectMode: true });
receiver__.once('done', (client, done) => {
if (client) client.close();
if (done) done();
});
receiver__.once('error', (error, client, done) => {
if (client) client.close();
if (done) done(error);
});
receiver__.write = (__newFile, encoding, done) => {
client(options.uri, options.mongoOptions, (error, client) => {
if (error) {
receiver__.emit('error', error, client, done);
}
const fd = __newFile.fd;
const filename = __newFile.filename;
const outs__ = bucket(client.db(), options.bucketOptions).openUploadStreamWithId(fd, filename, {
metadata: {
filename: filename,
fd: fd,
dirname: __newFile.dirname || path.dirname(fd)
},
contentType: mime.getType(fd)
});
__newFile.once('close', () => {
receiver__.emit('done', client, done);
});
__newFile.once('error', (error) => {
receiver__.emit('error', error, client, done);
});
outs__.once('finish', () => {
receiver__.emit('done', client, done);
});
outs__.once('error', (error) => {
receiver__.emit('error', error, client, done);
});
__newFile.pipe(outs__);
});
}
return receiver__;
}
return adapter;
};