-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudinaryReceiver.js
45 lines (32 loc) · 988 Bytes
/
CloudinaryReceiver.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
const Writable = require('stream').Writable;
module.exports = function CloudinaryReceiver(cloudinary, options) {
const receiver = Writable({
objectMode: true
});
options.rowHandler = options.rowHandler || function () {
};
receiver._files = [];
receiver._write = function onFile(file, encoding, done) {
const headers = options.headers || {};
const stream = cloudinary.uploader.upload_stream(function (result) {
if (result.error) {
return receiver.emit('error', new Error(result.error.message));
}
file.extra = result;
file.byteCount = result.bytes;
file.size = result.bytes;
done();
}, options.uploadOptions);
stream.on('error', function (error) {
done(error);
});
stream.on('readable', function () {
let record;
while (record = stream.read()) {
options.rowHandler(record, file.fd, file);
}
});
file.pipe(stream);
};
return receiver;
};