-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (51 loc) · 1.71 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
const request = require('request');
const Thumbor = require('thumbor');
module.exports = function skipperThumbor (options) {
options = Object.assign({
imageHost: null,
imageSecret: null,
}, options);
const thumborURL = new Thumbor(options.imageSecret, options.imageHost);
const adapter = {
read: function (fd, cb) {
return cb(new Error('TODO'));
},
rm: function (fd, cb){
return cb(new Error('TODO'));
},
ls: function (dirname, cb) {
return cb(new Error('TODO'));
},
receive: function () {
var receiver = require('stream').Writable({ objectMode: true });
receiver._write = (streamFile, encoding, done) => {
const r = request.post(options.imageHost + '/image');
r.on('response', (response) => {
const result = {};
if (response.headers.location) {
const locationParsedArray = response.headers.location.split('/');
const smartPath = locationParsedArray[2];
// const smartPath = response.headers.location.slice(6);
const thumbUrl = thumborURL.setImagePath(smartPath).resize(320, 320).buildUrl();
// result.url = url;
result.id = smartPath;
result.thumbUrl = thumbUrl;
}
receiver.emit('writefile', streamFile);
const responseJSON = response.toJSON();
result.statusCode = responseJSON.statusCode;
result.imageHost = options.imageHost;
streamFile.extra = result;
done();
});
r.on('error', (error) => {
receiver.emit('error', error);
return;
});
streamFile.pipe(r);
};
return receiver;
}
};
return adapter;
};