1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const knox = require('knox');
|
6 | const mime = require('mime');
|
7 | const async = require('async');
|
8 |
|
9 |
|
10 | mime.define({
|
11 | 'application/x-font-opentype': ['otf','eot','ttf'],
|
12 | 'image/svg+xml': ['svg'],
|
13 | 'application/font-woff': ['woff']
|
14 | });
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function S3(bucket, key, secret) {
|
24 | const config = {
|
25 | key: key,
|
26 | secret: secret,
|
27 | bucket: bucket
|
28 | };
|
29 | this.bucket = bucket;
|
30 |
|
31 | if (bucket && key && secret) {
|
32 | this.client = knox.createClient(config);
|
33 | }
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | S3.prototype.setCloudfrontMapping = function(cloudfrontMapping){
|
42 | this.cloudfrontMapping = cloudfrontMapping;
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | S3.prototype.getBucket = function() {
|
50 | return this.bucket;
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | S3.prototype.putBuffer = function(contents, path, makePrivateOrHeaders, callback) {
|
61 |
|
62 | if (typeof makePrivateOrHeaders === 'function') {
|
63 | callback = makePrivateOrHeaders;
|
64 | makePrivateOrHeaders = false;
|
65 | }
|
66 |
|
67 | path = _cleanPath(path);
|
68 |
|
69 | const self = this;
|
70 | const buffer = contents instanceof Buffer ? contents : new Buffer(contents);
|
71 |
|
72 | const maxTries = 3;
|
73 | let numTries = 0;
|
74 | let fileUpload = null;
|
75 |
|
76 | async.until(
|
77 | () => { return fileUpload || numTries > maxTries },
|
78 | untilNext => {
|
79 | self.client.putBuffer(buffer, path, _createHeaders(makePrivateOrHeaders), function (err, result) {
|
80 |
|
81 | fileUpload = result && self.urlWithBucket(path);
|
82 | let delayTimeoutMs = 0;
|
83 |
|
84 | if (++numTries <= maxTries && err) {
|
85 | delayTimeoutMs = Math.pow(2, 10+numTries);
|
86 | console.log('S3 Upload error, will retry', {err, numTries, delayTimeoutMs});
|
87 | err = null;
|
88 | }
|
89 |
|
90 | setTimeout(() => {
|
91 | untilNext(err);
|
92 | }, delayTimeoutMs);
|
93 | });
|
94 | },
|
95 | err => {
|
96 | callback(err, fileUpload);
|
97 | }
|
98 | );
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | S3.prototype.putFile = function(sourcePath, targetPath, makePrivateOrHeaders, callback) {
|
110 |
|
111 | targetPath = _cleanPath(targetPath);
|
112 |
|
113 | const self = this;
|
114 | const stream = fs.createReadStream(sourcePath);
|
115 |
|
116 | const headers = _createHeaders(makePrivateOrHeaders, targetPath);
|
117 | headers['Content-Length'] = fs.statSync(sourcePath).size;
|
118 |
|
119 | const maxTries = 3;
|
120 | let numTries = 0;
|
121 | let fileUpload = null;
|
122 |
|
123 | async.until(
|
124 | () => { return fileUpload || numTries > maxTries },
|
125 | untilNext => {
|
126 | self.client.putStream(stream, targetPath, headers, function(err, result){
|
127 |
|
128 | fileUpload = result && self.urlWithBucket(targetPath);
|
129 | let delayTimeoutMs = 0;
|
130 |
|
131 | if (++numTries <= maxTries && err) {
|
132 | delayTimeoutMs = Math.pow(2, 10+numTries);
|
133 | console.warn('S3 Upload error, will retry', {err, numTries, delayTimeoutMs, fileUpload});
|
134 | err = null;
|
135 | }
|
136 |
|
137 | setTimeout(() => {
|
138 | untilNext(err);
|
139 | }, delayTimeoutMs);
|
140 | });
|
141 | },
|
142 | err => {
|
143 | callback(err, fileUpload);
|
144 | }
|
145 | );
|
146 | };
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | S3.prototype.fileExists = function(path, callback) {
|
154 |
|
155 | const self = this;
|
156 |
|
157 | path = _cleanPath(path);
|
158 |
|
159 |
|
160 | self.client.headFile(path, function(err, response) {
|
161 | callback(err, response && response.statusCode === 200);
|
162 | });
|
163 |
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | S3.prototype.urlWithBucket = function(path) {
|
172 | path = path || '';
|
173 |
|
174 | path = path.replace(/\\/g, '/');
|
175 | if (path[0] === '/') {
|
176 | path = path.substr(1);
|
177 | }
|
178 | return this.cloudfrontMapping
|
179 | ? 'https://'+this.cloudfrontMapping+'.cloudfront.net/'+path
|
180 | : 'https://s3.amazonaws.com/'+this.bucket.toLowerCase()+'/'+path;
|
181 | };
|
182 |
|
183 | function _createHeaders(makePrivateOrHeaders, targetPath) {
|
184 |
|
185 | let headers = {};
|
186 | const contentType = targetPath && mime.lookup(targetPath);
|
187 | if (typeof makePrivateOrHeaders === 'object') {
|
188 | headers = makePrivateOrHeaders;
|
189 | } else if (!makePrivateOrHeaders) {
|
190 | headers['x-amz-acl'] = 'public-read';
|
191 | }
|
192 |
|
193 |
|
194 | if (!headers['Content-Type'] && contentType) {
|
195 | headers['Content-Type'] = contentType;
|
196 | }
|
197 |
|
198 | return headers;
|
199 | }
|
200 |
|
201 | function _cleanPath(path) {
|
202 |
|
203 | return path && path.replace(/\\/g,'/');
|
204 | }
|
205 |
|
206 | module.exports = S3; |
\ | No newline at end of file |