forked from anacronw/multer-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (151 loc) · 5.7 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
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
var crypto = require('crypto');
var stream = require('stream');
var fileType = require('file-type');
var parallel = require('run-parallel');
function staticValue (value) {
return function (req, file, cb) {
cb(null, value)
}
}
var defaultAcl = staticValue('private');
var defaultContentType = staticValue('application/octet-stream');
var defaultMetadata = staticValue(null);
var defaultCacheControl = staticValue(null);
var defaultSSE = staticValue(null);
var defaultStorageClass = staticValue(null);
function defaultKey (req, file, cb) {
crypto.randomBytes(16, function (err, raw) {
cb(err, err ? undefined : raw.toString('hex'))
})
}
function autoContentType (req, file, cb) {
file.stream.once('data', function (firstChunk) {
var type = fileType(firstChunk);
var mime = (type === null ? 'application/octet-stream' : type.mime);
var outStream = new stream.PassThrough();
outStream.write(firstChunk);
file.stream.pipe(outStream);
cb(null, mime, outStream)
})
}
function collect (storage, req, file, cb) {
parallel([
storage.getBucket.bind(storage, req, file),
storage.getKey.bind(storage, req, file),
storage.getAcl.bind(storage, req, file),
storage.getMetadata.bind(storage, req, file),
storage.getCacheControl.bind(storage, req, file),
storage.getSSE.bind(storage, req, file),
storage.getStorageClass.bind(storage, req, file)
], function (err, values) {
if (err) return cb(err);
storage.getContentType(req, file, function (err, contentType, replacementStream) {
if (err) return cb(err);
cb.call(storage, null, {
bucket: values[0],
key: values[1],
acl: values[2],
metadata: values[3],
cacheControl: values[4],
contentType: contentType,
replacementStream: replacementStream,
sse: values[5],
storageClass: values[6]
})
})
})
}
function S3Storage (opts) {
switch (typeof opts.s3) {
case 'object': this.s3 = opts.s3; break;
default: throw new TypeError('Expected opts.s3 to be object')
}
switch (typeof opts.bucket) {
case 'function': this.getBucket = opts.bucket; break;
case 'string': this.getBucket = staticValue(opts.bucket); break;
case 'undefined': throw new Error('bucket is required');
default: throw new TypeError('Expected opts.bucket to be undefined, string or function')
}
switch (typeof opts.key) {
case 'function': this.getKey = opts.key; break;
case 'undefined': this.getKey = defaultKey; break;
default: throw new TypeError('Expected opts.key to be undefined or function')
}
switch (typeof opts.acl) {
case 'function': this.getAcl = opts.acl; break;
case 'string': this.getAcl = staticValue(opts.acl); break;
case 'undefined': this.getAcl = defaultAcl; break;
default: throw new TypeError('Expected opts.acl to be undefined, string or function')
}
switch (typeof opts.contentType) {
case 'function': this.getContentType = opts.contentType; break;
case 'undefined': this.getContentType = defaultContentType; break;
default: throw new TypeError('Expected opts.contentType to be undefined or function')
}
switch (typeof opts.metadata) {
case 'function': this.getMetadata = opts.metadata; break;
case 'undefined': this.getMetadata = defaultMetadata; break;
default: throw new TypeError('Expected opts.metadata to be undefined or function')
}
switch (typeof opts.cacheControl) {
case 'function': this.getCacheControl = opts.cacheControl; break;
case 'string': this.getCacheControl = staticValue(opts.cacheControl); break;
case 'undefined': this.getCacheControl = defaultCacheControl; break;
default: throw new TypeError('Expected opts.cacheControl to be undefined, string or function')
}
switch (typeof opts.sse) {
case 'function': this.getSSE = opts.sse; break;
case 'string': this.getSSE = staticValue(opts.sse); break;
case 'undefined': this.getSSE = defaultSSE; break;
default: throw new TypeError('Expected opts.sse to be undefined, string or function')
}
switch (typeof opts.storageClass) {
case 'function': this.getStorageClass = opts.storageClass; break;
case 'string': this.getStorageClass = staticValue(opts.storageClass); break;
case 'undefined': this.getStorageClass = defaultStorageClass; break;
default: throw new TypeError('Expected opts.storageClass to be undefined, string or function')
}
}
S3Storage.prototype._handleFile = function (req, file, cb) {
collect(this, req, file, function (err, opts) {
if (err) return cb(err);
var currentSize = 0;
var upload = this.s3.upload({
Bucket: opts.bucket,
Key: opts.key,
ACL: opts.acl,
CacheControl: opts.cacheControl,
ContentType: opts.contentType,
Metadata: opts.metadata,
Body: (opts.replacementStream || file.stream),
ServerSideEncryption: opts.sse,
StorageClass: opts.storageClass
});
upload.on('httpUploadProgress', function (ev) {
if (ev.total) currentSize = ev.total
});
upload.send(function (err, result) {
if (err) return cb(err);
cb(null, {
size: currentSize,
bucket: opts.bucket,
key: opts.key,
acl: opts.acl,
contentType: opts.contentType,
metadata: opts.metadata,
location: result.Location,
etag: result.ETag,
serverSideEncryption: opts.sse,
storageClass: opts.storageClass
})
})
})
};
S3Storage.prototype._removeFile = function (req, file, cb) {
this.s3.deleteObject({ Bucket: file.bucket, Key: file.key }, cb)
};
module.exports = function (opts) {
return new S3Storage(opts)
};
module.exports.AUTO_CONTENT_TYPE = autoContentType;
module.exports.DEFAULT_CONTENT_TYPE = defaultContentType;