-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.js
78 lines (63 loc) · 2.33 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
'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 || {};
var client = knox.createClient(aws);
var waitTime = 0;
var regexGzip = /\.([a-z0-9]{2,})\.gz$/i;
var regexGeneral = /\.([a-z0-9]{2,})$/i;
return es.map(function (file, finished) {
if (!file.isBuffer()) { finished(null, file); return; }
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)) {
headers['Content-Encoding'] = 'gzip';
if (options.gzippedOnly) {
uploadPath = uploadPath.substring(0, uploadPath.length - 3);
}
} else if (options.gzippedOnly) {
return file;
}
// Set content type based on file extension
if (!headers['Content-Type'] && regexGeneral.test(uploadPath)) {
headers['Content-Type'] = mime.lookup(uploadPath);
if (options.encoding) {
headers['Content-Type'] += '; charset=' + options.encoding;
}
}
var contentLength = 0;
if(file.stat != null)
contentLength = file.stat.size; // In case of a stream
else
contentLength = file.contents.length; // It may be a buffer
headers['Content-Length'] = contentLength;
client.putBuffer(file.contents, uploadPath, headers, function(err, res) {
if (err || res && res.statusCode !== 200) {
gutil.log(gutil.colors.red('[FAILED]', file.path + " -> " + uploadPath));
if (err) {
gutil.log(gutil.colors.red(' AWS ERROR:', err));
throw new Error(err);
}
if (res && res.statusCode !== 200){
gutil.log(gutil.colors.red(' HTTP STATUS:', res.statusCode));
throw new Error('HTTP Status Code: ' + res.statusCode);
}
finished(err, null)
} else {
gutil.log(gutil.colors.green('[SUCCESS]') + ' ' + gutil.colors.grey(file.path) + gutil.colors.green(" -> ") + uploadPath);
res.resume();
finished(null, file)
}
});
});
};