forked from basicinception/skipper-s3-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
298 lines (248 loc) · 8.57 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Module dependencies
*/
var path = require('path');
var Writable = require('stream').Writable;
var Transform = require('stream').Transform;
var concat = require('concat-stream');
var _ = require('lodash');
_.defaultsDeep = require('merge-defaults');
var knox = require('knox');
var S3MultipartUpload = require('knox-mpu-alt');
var S3Lister = require('s3-lister');
var mime = require('mime');
var gm = require('gm');
/**
* skipper-s3
*
* @param {Object} globalOpts
* @return {Object}
*/
module.exports = function SkipperS3 (globalOpts) {
globalOpts = globalOpts || {};
// console.log('S3 adapter was instantiated...');
var adapter = {
read: function (fd, cb) {
var prefix = fd;
var client = knox.createClient({
key: globalOpts.key,
secret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region||undefined,
endpoint: globalOpts.endpoint||undefined,
token: globalOpts.token||undefined
});
// Build a noop transform stream that will pump the S3 output through
var __transform__ = new Transform();
__transform__._transform = function (chunk, encoding, callback) {
return callback(null, chunk);
};
client.get(prefix).on('response', function(s3res){
// Handle explicit s3res errors
s3res.once('error', function (err) {
__transform__.emit('error', err);
});
// check whether we got an actual file stream:
if (s3res.statusCode < 300) {
s3res.pipe(__transform__);
}
// or an error:
else {
// Wait for the body of the error message to stream in:
var body = '';
s3res.setEncoding('utf8');
s3res.on('readable', function (){
var chunk = s3res.read();
if (typeof chunk === 'string') body += chunk;
});
// Then build the error and emit it
s3res.once('end', function () {
var err = new Error();
err.status = s3res.statusCode;
err.headers = s3res.headers;
err.message = 'Non-200 status code returned from S3 for requested file.';
if (body) err.message += ('\n'+body);
__transform__.emit('error', err);
});
}
})
.end();
if (cb) {
var firedCb;
__transform__.once('error', function (err) {
if (firedCb) return;
firedCb = true;
cb(err);
});
__transform__.pipe(concat(function (data) {
if (firedCb) return;
firedCb = true;
cb(null, data);
}));
}
return __transform__;
},
rm: function (fd, cb) {
knox.createClient({
key: globalOpts.key,
secret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region||undefined,
endpoint: globalOpts.endpoint||undefined
})
.del(fd)
.on('response', function (res) {
if (res.statusCode === 204) {
cb();
} else {
cb({
statusCode: res.statusCode,
message: res.body
});
}
})
.end();
},
ls: function (dirname, cb) {
var client = knox.createClient({
key: globalOpts.key,
secret: globalOpts.secret,
bucket: globalOpts.bucket,
region: globalOpts.region,
endpoint: globalOpts.endpoint,
token: globalOpts.token||undefined
});
// TODO: take a look at maxKeys
// https://www.npmjs.org/package/s3-lister
// Allow empty dirname (defaults to `/`)
if (!dirname) {
prefix='/';
}
else prefix = dirname;
// Strip leading slash from dirname to form prefix
var prefix = dirname.replace(/^\//, '');
var lister = new S3Lister(client, {
prefix : prefix
});
if (!cb) {
return lister;
}
else {
var firedCb;
lister.once('error', function (err) {
if(firedCb)return;
firedCb=true;
cb(err);
});
lister.pipe(concat(function (data) {
if(firedCb)return;
firedCb=true;
// Pluck just the "Key" (i.e. file path)
// and return only the filename (i.e. snip
// off the path prefix)
data = _.pluck(data, 'Key');
data = _.map(data, function snipPathPrefixes (thisPath) {
thisPath = thisPath.replace(/^.*[\/]([^\/]*)$/, '$1');
// Join the dirname with the filename
thisPath = path.join(dirname, path.basename(thisPath));
return thisPath;
});
// console.log('______ files _______\n', data);
cb(null, data);
}));
// TODO: marshal each matched file in the stream
// (using a Transform- take a look at all the
// "plucking" and stuff I have going on above ^)
return lister;
}
},
receive: S3Receiver
};
return adapter;
/**
* A simple receiver for Skipper that writes Upstreams to
* S3 to the configured bucket at the configured path.
*
* Includes a garbage-collection mechanism for failed
* uploads.
*
* @param {Object} options
* @return {Stream.Writable}
*/
function S3Receiver (options) {
// console.log('`.receive()` was called...');
options = options || {};
options = _.defaults(options, globalOpts);
var receiver__ = Writable({
objectMode: true
});
receiver__.once('error', function (err) {
console.log('ERROR ON RECEIVER__ ::',err);
});
// This `_write` method is invoked each time a new file is received
// from the Readable stream (Upstream) which is pumping filestreams
// into this receiver. (filename === `__newFile.filename`).
receiver__._write = function onFile(__newFile, encoding, next) {
var startedAt = new Date();
var width = options.resize.width || 200;
var height = options.resize.height || 200;
var gmOptions = options.resize.options || '';
// Allow `tmpdir` for knox-mpu to be passed in, or default
// to `.tmp/s3-upload-part-queue`
gm(__newFile).resize(width, height, gmOptions).stream(function (err, stdout, stderr) {
if (err) return next(err);
options.tmpdir = options.tmpdir || path.resolve(process.cwd(), '.tmp/s3-upload-part-queue');
var headers = options.headers || {};
// Lookup content type with mime if not set
if ('undefined' === typeof headers['content-type']) {
headers['content-type'] = mime.lookup(__newFile.fd);
}
var mpu = new S3MultipartUpload({
objectName: __newFile.fd,
stream: stdout,
maxUploadSize: options.maxBytes,
tmpDir: options.tmpdir,
headers: headers,
client: knox.createClient({
key: options.key,
secret: options.secret,
bucket: options.bucket,
region: globalOpts.region||undefined,
endpoint: globalOpts.endpoint||undefined,
token: globalOpts.token||undefined
})
}, function (err, body) {
if (err) {
// console.log(('Receiver: Error writing `' + __newFile.filename + '`:: ' + require('util').inspect(err) + ' :: Cancelling upload and cleaning up already-written bytes...').red);
receiver__.emit('error', err);
return;
}
// Package extra metadata about the S3 response on each file stream
// in case we decide we want to use it for something later
__newFile.extra = body;
// console.log(('Receiver: Finished writing `' + __newFile.filename + '`').grey);
// console.timeEnd('fileupload:'+__newFile.filename);
var endedAt = new Date();
var duration = ((endedAt - startedAt) / 1000);
// console.log('**** S3 upload took '+duration+' seconds...');
next();
});
mpu.on('progress', function(data) {
var snapshot = new Date();
var secondsElapsed = ((snapshot - startedAt) / 1000);
var estUploadRate = (data.written/1000) / secondsElapsed;
// console.log('Uploading at %dkB/s', estUploadRate);
// console.log('Elapsed:',secondsElapsed+'s');
// console.log('Uploading (%s)..',__newFile.filename, data);
receiver__.emit('progress', {
name: __newFile.filename,
written: data.written,
total: data.total,
percent: data.percent
});
});
});
};
return receiver__;
}
};