-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
172 lines (133 loc) · 4.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
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
'use strict';
const fs = require('fs');
const extname = require('path').extname;
const S3 = require('aws-sdk').S3;
const auto = require('async').auto;
const each = require('async').each;
const map = require('async').map;
const uuid = require('uuid');
const resize = require('im-resize');
const metadata = require('im-metadata');
const Image = function Image(src, opts, upload) {
this.src = src;
this.opts = opts;
this.upload = upload;
return this;
};
Image.prototype.start = function start(cb) {
auto({
metadata: this.getMetadata.bind(this, this.src),
dest: this.getDest.bind(this),
versions: ['metadata', this.resizeVersions.bind(this)],
uploads: ['versions', 'dest', this.uploadVersions.bind(this)],
cleanup: ['uploads', this.removeVersions.bind(this)],
}, (err, results) => {
cb(err, results.uploads, results.metadata);
});
};
Image.prototype.getMetadata = function getMetadata(src, cb) {
metadata(src, {
exif: this.upload.opts.returnExif,
autoOrient: true,
}, cb);
};
Image.prototype.getDest = function getDest(cb) {
const prefix = this.opts.awsPath || this.upload.opts.aws.path;
const path = this.opts.path || this.upload._randomPath();
process.nextTick(() => cb(null, prefix + path));
};
Image.prototype.resizeVersions = function resizeVersions(results, cb) {
resize(results.metadata, {
path: this.upload.opts.resize.path,
prefix: this.upload.opts.resize.prefix,
quality: this.upload.opts.resize.quality,
versions: JSON.parse(JSON.stringify(this.upload.opts.versions)),
}, cb);
};
Image.prototype.uploadVersions = function uploadVersions(results, cb) {
if (this.upload.opts.original) {
const org = JSON.parse(JSON.stringify(this.upload.opts.original));
org.original = true;
org.width = results.metadata.width;
org.height = results.metadata.height;
org.path = this.src;
results.versions.push(org);
}
map(results.versions, this._upload.bind(this, results.dest), cb);
};
Image.prototype.removeVersions = function removeVersions(results, cb) {
each(results.uploads, (image, callback) => {
if ((!this.upload.opts.cleanup.original && image.original) ||
(!this.upload.opts.cleanup.versions && !image.original)
) {
return setTimeout(callback, 0);
}
return fs.unlink(image.path, callback);
}, cb);
};
Image.prototype._upload = function _upload(dest, version, cb) {
if (version.awsImageAcl == null) {
version.awsImageAcl = this.upload.opts.aws.acl;
}
const format = extname(version.path).substr(1).toLowerCase();
const options = {
Key: `${dest}${version.suffix || ''}.${format}`,
ACL: version.awsImageAcl,
Body: fs.createReadStream(version.path),
ContentType: `image/${format === 'jpg' ? 'jpeg' : format}`,
};
if (version.awsImageExpires) {
options.Expires = new Date(Date.now() + version.awsImageExpires);
}
if (version.awsImageMaxAge) {
options.CacheControl = `public, max-age=${version.awsImageMaxAge}`;
}
this.upload.s3.putObject(options, (err, data) => {
if (err) { return cb(err); }
version.etag = data.ETag;
version.key = options.Key;
if (this.upload.opts.url) {
version.url = this.upload.opts.url + options.Key;
}
return cb(null, version);
});
};
const Upload = function Upload(bucketName, opts) {
this.opts = opts || {};
if (!bucketName) {
throw new TypeError('Bucket name can not be undefined');
}
if (!this.opts.aws) { this.opts.aws = {}; }
if (!this.opts.aws.acl) { this.opts.aws.acl = 'private'; }
if (!this.opts.aws.httpOptions) { this.opts.aws.httpOptions = {}; }
if (!this.opts.aws.httpOptions.timeout) {
this.opts.aws.httpOptions.timeout = 10000;
}
if (!this.opts.aws.maxRetries) { this.opts.aws.maxRetries = 3; }
if (!this.opts.aws.params) { this.opts.aws.params = {}; }
this.opts.aws.params.Bucket = bucketName;
if (!this.opts.aws.path) { this.opts.aws.path = ''; }
if (!this.opts.aws.region) { this.opts.aws.region = 'us-east-1'; }
if (!this.opts.aws.sslEnabled) { this.opts.aws.sslEnabled = true; }
if (!this.opts.cleanup) { this.opts.cleanup = {}; }
if (!this.opts.returnExif) { this.opts.returnExif = false; }
if (!this.opts.resize) { this.opts.resize = {}; }
if (!this.opts.resize.quality) { this.opts.resize.quality = 70; }
if (!this.opts.versions) { this.opts.versions = []; }
if (!this.opts.url && this.opts.aws.region === 'us-east-1') {
this.opts.url = `https://s3.amazonaws.com/${bucketName}/`;
} else if (!this.opts.url && this.opts.aws.region === 'cn-north-1') {
this.opts.url = `https://s3.${this.opts.aws.region}.amazonaws.com/${bucketName}/`;
} else if (!this.opts.url) {
this.opts.url = `https://s3-${this.opts.aws.region}.amazonaws.com/${bucketName}/`;
}
this._randomPath = this.opts.randomPath || uuid;
this.s3 = new S3(this.opts.aws);
return this;
};
Upload.prototype.upload = function upload(src, opts, cb) {
const image = new Image(src, opts, this);
image.start(cb);
};
module.exports = Upload;
module.exports.Image = Image;