-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (57 loc) · 2.12 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
'use strict';
var es = require('event-stream');
var knox = require('knox');
var gutil = require('gulp-util');
var mime = require('mime');
var yargs = require('yargs').argv;
mime.default_type = 'text/plain'; // jshint ignore:line
module.exports = function (aws, options) {
options = options || {};
if (!options.delay) { options.delay = 0; }
var dry = (yargs.dry) ? true : false;
var regexGzip = /\.gz$/i;
var regexGeneral = /\.([a-z]{2,})$/i;
return es.mapSync(function (file) {
// Verify this is a file
if (!file.isBuffer()) { return file; }
var uploadPath = file.path.replace(file.base, options.uploadPath || '');
uploadPath = uploadPath.replace(new RegExp('\\\\', 'g'), '/');
var headers = { 'x-amz-acl': 'public-read' };
if (options.headers) {
for (var key in options.headers) {
headers[key] = options.headers[key];
}
}
if (regexGzip.test(file.path)) {
// Set proper encoding for gzipped files, remove .gz suffix
headers['Content-Encoding'] = 'gzip';
uploadPath = uploadPath.substring(0, uploadPath.length - 3);
} else if (options.gzippedOnly) {
// Ignore non-gzipped files
return file;
}
// Set content type based of file extension
if (!headers['Content-Type'] && regexGeneral.test(uploadPath)) {
headers['Content-Type'] = mime.lookup(uploadPath);
if (options.encoding) {
headers['Content-Type'] += '; charset=' + options.encoding;
}
}
headers['Content-Length'] = file.stat.size;
if (dry) {
gutil.log(gutil.colors.yellow('[DRY RUN]', file.path + ' -> ' + uploadPath));
return file;
}
knox
.createClient(aws)
.putBuffer(file.contents, uploadPath, headers, function(err, res) {
if (err || res.statusCode !== 200) {
gutil.log(gutil.colors.red('[FAILED]', file.path + ' -> ' + uploadPath));
} else {
gutil.log(gutil.colors.green('[SUCCESS]', file.path + ' -> ' + uploadPath));
res.resume();
}
});
return file;
});
};