forked from nitaigao/gulp-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (51 loc) · 1.98 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
'use strict';
var es = require('event-stream');
var knox = require('knox');
var gutil = require('gulp-util');
var mime = require('mime');
mime.default_type = 'text/plain';
module.exports = function (aws, options) {
options = options || {};
if (!options.delay) { options.delay = 0; }
var client = knox.createClient(aws);
var waitTime = 0;
var regexGzip = /\.([a-z]{2,})\.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;
client.putBuffer(file.contents, uploadPath, headers, function(err, res) {
if (err || res.statusCode !== 200) {
gutil.log(gutil.colors.red('[FAILED]', file.path + " -> [BUCKET] " + aws.bucket + " -> " + uploadPath));
} else {
gutil.log(gutil.colors.green('[SUCCESS]', file.path + " -> [BUCKET] " + aws.bucket + " -> " + uploadPath));
res.resume();
}
});
return file;
});
};