-
Notifications
You must be signed in to change notification settings - Fork 32
/
s3.js
245 lines (223 loc) · 6.92 KB
/
s3.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
/* global describe, it */
const assert = require('assert');
const request = require('request');
describe('UploadFS S3', function () {
this.timeout(50000);
const uploadfs = require('../uploadfs.js')();
const fs = require('fs');
const async = require('async');
const tempPath = '../temp';
const dstPath = '/one/two/three/test.txt';
const imageSizes = [
{
name: 'small',
width: 320,
height: 320
},
{
name: 'medium',
width: 640,
height: 640
},
{
name: 'large',
width: 1140,
height: 1140
}
];
const s3Options = require('../s3TestOptions.js');
s3Options.imageSizes = imageSizes;
s3Options.tempPath = tempPath;
s3Options.params = {
Bucket: s3Options.bucket,
ACL: 'public-read'
};
it('S3 Should init s3 connection without error', function(done) {
return uploadfs.init(s3Options, function(e) {
assert(!e, 'S3 init without error');
if (e) {
console.log('=======E', e);
}
uploadfs.copyIn('test.txt', dstPath, function(e) {
if (e) {
console.log('=======EE', e);
}
assert(!e, 'S3 copyIn without error');
done();
});
});
});
it('CopyIn should work', function (done) {
return uploadfs.copyIn('test.txt', dstPath, function(e) {
assert(!e, 'S3 copyIn without error');
done();
});
});
it('CopyIn file should be available via s3', function (done) {
const url = uploadfs.getUrl() + '/one/two/three/test.txt';
const og = fs.readFileSync('test.txt', 'utf8');
request(url, {
gzip: true
}, (e, res, body) => {
assert(!e, 'Request success');
assert(res.statusCode === 200, 'Request status 200');
assert(res.body === og, 'Res body equals uploaded file');
done();
});
});
it('S3 CopyOut should work', done => {
const cpOutPath = 'copy-out-test.txt';
return uploadfs.copyOut(dstPath, cpOutPath, e => {
assert(!e, 'S3 copyOut without error');
const dl = fs.readFileSync(cpOutPath, 'utf8');
const og = fs.readFileSync('test.txt', 'utf8');
assert(dl === og, 'Downloaded file is equal to previous upload');
done();
});
});
it('S3 Disable / Enable should work as expected', done => {
return async.series({
disable: cb => {
uploadfs.disable(dstPath, e => {
assert(!e, 'uploadfs disable no err');
cb(null);
});
},
webShouldFail: cb => {
const url = uploadfs.getUrl() + dstPath;
return request(url, {
gzip: true
}, (e, res, body) => {
assert(res.statusCode >= 400, 'Request on disabled resource should fail');
cb(null);
});
},
enable: cb => {
uploadfs.enable(dstPath, e => {
assert(!e, 'uploadfs enable should not fail');
cb(null);
});
},
webShouldSucceed: cb => {
const url = uploadfs.getUrl() + dstPath;
return request(url, {
gzip: true
}, (e, res, body) => {
const og = fs.readFileSync('test.txt', 'utf8');
assert(!e, 'Request for enabled resource should not fail');
assert(res.statusCode < 400, 'Request for enabled resource should not fail');
assert(og === res.body, 'Downloaded content should be equal to previous upload');
assert(res.headers['content-type'] === 'text/plain', 'Check content-type header');
cb(null);
});
}
}, e => {
assert(!e, 'Series should succeed');
done();
});
});
it('S3 uploadfs Remove should work', done => {
return uploadfs.remove(dstPath, e => {
assert(!e, 'Remove should succeed');
setTimeout(() => {
const url = uploadfs.getUrl() + dstPath;
request(url, {
gzip: true
}, (e, res, body) => {
assert(!e);
assert(res.statusCode >= 400, 'Removed file is gone from s3');
done();
});
}, 5000);
});
});
it('S3 uploadfs copyImageIn should work', done => {
const imgDstPath = '/images/profiles/me';
uploadfs.copyImageIn('test.jpg', imgDstPath, (e, info) => {
assert(!e, 'S3 copyImageIn works');
setTimeout(() => {
const url = uploadfs.getUrl();
const paths = [ info.basePath + '.jpg' ];
paths.push(info.basePath + '.small.jpg');
paths.push(info.basePath + '.medium.jpg');
paths.push(info.basePath + '.large.jpg');
async.map(paths, (path, cb) => {
const imgPath = url + path;
request(imgPath, {
gzip: true,
// return a buffer so we can test bytes
encoding: null
}, (e, res, body) => {
assert(!e);
// Not suitable for images, make sure we didn't force it
assert(res.headers['content-encoding'] !== 'gzip');
assert(res.statusCode === 200);
// JPEG magic number check
assert(body[0] === 0xFF);
assert(body[1] === 0xD8);
// clean up
uploadfs.remove(path, e => {
assert(!e, 'Remove uploaded file after testing');
return cb();
});
});
}, e => {
assert(!e, 'Can request all copyImageInned images');
done();
});
// end async.each
}, 5000);
});
});
it('S3 uploadfs copyImageIn should work with custom sizes', done => {
const imgDstPath = '/images/profiles/me';
const customSizes = [
{
name: 'tiny',
width: 80,
height: 80
},
{
name: 'nice',
width: 120,
height: 120
}
];
uploadfs.copyImageIn('test.jpg', imgDstPath, { sizes: customSizes }, (e, info) => {
assert(!e, 'S3 copyImageIn works');
setTimeout(() => {
const url = uploadfs.getUrl();
// Default should be https
assert(url.startsWith('https://'));
const paths = [ info.basePath + '.jpg' ];
paths.push(info.basePath + '.tiny.jpg');
paths.push(info.basePath + '.nice.jpg');
async.map(paths, (path, cb) => {
const imgPath = url + path;
request(imgPath, {
gzip: true,
// return a buffer so we can test bytes
encoding: null
}, (e, res, body) => {
assert(!e);
// Not suitable for images, make sure we didn't force it
assert(res.headers['content-encoding'] !== 'gzip');
assert(res.statusCode === 200);
// JPEG magic number check
assert(body[0] === 0xFF);
assert(body[1] === 0xD8);
// clean up
uploadfs.remove(path, e => {
assert(!e, 'Remove uploaded file after testing');
return cb();
});
});
}, e => {
assert(!e, 'Can request all copyImageInned images');
done();
});
// end async.each
}, 5000);
});
});
});