-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
220 lines (200 loc) · 6.29 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
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
/* eslint-disable func-names */
'use strict';
const { Storage } = require('@google-cloud/storage');
const Promise = require('bluebird');
const { PassThrough } = require('stream');
const defaultOptions = require('./config/default');
const sharpOptions = require('./lib/get-sharp-options');
const getDestination = require('./lib/get-destination');
const getFilename = require('./lib/get-filename');
const transformer = require('./lib/transformer');
const getFormat = require('./lib/get-format');
const logger = require('./lib/logger');
class MulterSharp {
constructor(options) {
/* eslint-disable no-param-reassign, no-underscore-dangle */
options.bucket = options.bucket || process.env.GCS_BUCKET || null;
options.projectId = options.projectId || process.env.GC_PROJECT || null;
options.keyFilename = options.keyFilename || process.env.GCS_KEYFILE || null;
this._check(options);
this.options = Object.assign({}, MulterSharp.defaultOptions, options || {});
this.getFilename = this.options.filename || getFilename;
this.sharpOptions = sharpOptions(this.options);
if (typeof options.destination === 'string') {
this.getDestination = ($0, $1, cb) => cb(null, options.destination);
} else {
this.getDestination = options.destination || getDestination;
}
this.gcStorage = new Storage({
projectId: options.projectId,
keyFilename: options.keyFilename
});
this.gcsBucket = this.gcStorage.bucket(options.bucket);
}
_check(options) {
if (!options.bucket) {
throw new Error(
'You have to specify bucket for Google Cloud Storage to work.'
);
}
if (!options.projectId) {
throw new Error(
'You have to specify project id for Google Cloud Storage to work.'
);
}
}
_eachUpload(file, filename, gcName, fileOptions, stream) {
return (size) => {
const filenameWithSuffix = `${filename}-${size.suffix}`;
const gcNameBySuffix = `${gcName}-${size.suffix}`;
const gcFile = this.gcsBucket.file(gcNameBySuffix);
const streamCopy = new PassThrough();
const resizerStream = transformer(this.sharpOptions, size);
const writableStream = gcFile.createWriteStream(fileOptions);
stream.pipe(streamCopy);
return new Promise((resolve, reject) => {
streamCopy.pipe(resizerStream).pipe(writableStream);
resizerStream.on('info', logger);
resizerStream.on('error', function (transformErr) {
resizerStream.unpipe(writableStream);
this.end();
reject(transformErr);
});
writableStream.on('error', function (gcErr) {
this.end();
reject(gcErr);
});
writableStream.on('finish', () => {
const uri = encodeURI(
`https://storage.googleapis.com/${
this.options.bucket
}/${gcNameBySuffix}`
);
resolve({
mimetype: getFormat(this.sharpOptions.toFormat) || file.mimetype,
path: uri,
filename: filenameWithSuffix,
suffix: size.suffix
});
});
});
};
}
_handleFile(req, file, cb) {
this.getDestination(req, file, (destErr, destination) => {
if (destErr) cb(destErr);
this.getFilename(req, file, (fileErr, filename) => {
if (fileErr) cb(fileErr);
const fileOptions = {
predefinedAcl: this.options.acl,
metadata: Object.assign(this.options.metadata, {
contentType: getFormat(this.sharpOptions.toFormat) || file.mimetype
}),
gzip: this.options.gzip
};
const gcName = typeof destination === 'string' && destination.length > 0
? `${destination}/${filename}`
: filename;
const gcFile = this.gcsBucket.file(gcName);
const { stream } = file;
const resizerStream = transformer(this.sharpOptions, this.options.size);
const writableStream = gcFile.createWriteStream(fileOptions);
if (
Array.isArray(this.options.sizes)
&& this.options.sizes.length > 0
) {
this._uploadWithMultipleSize(
this.options.sizes,
file,
filename,
gcName,
fileOptions,
stream,
cb
);
} else {
this._uploadOne(
stream,
file,
filename,
gcName,
resizerStream,
writableStream,
cb
);
}
});
});
}
_removeFile(req, file, cb) {
this.getDestination(req, file, (destErr, destination) => {
if (destErr) cb(destErr);
const gcName = typeof destination === 'string' && destination.length > 0
? `${destination}/${file.filename}`
: file.filename;
const gcFile = this.gcsBucket.file(gcName);
gcFile.delete(cb);
});
}
_uploadOne(
stream,
file,
filename,
gcName,
resizerStream,
writableStream,
cb
) {
stream.pipe(resizerStream).pipe(writableStream);
resizerStream
.on('info', logger)
.on('error', function (transformErr) {
resizerStream.unpipe(writableStream);
cb(transformErr);
this.end();
});
writableStream
.on('error', function (gcErr) {
cb(gcErr);
this.end();
})
.on('finish', () => {
const uri = encodeURI(
`https://storage.googleapis.com/${this.options.bucket}/${gcName}`
);
return cb(null, {
mimetype: getFormat(this.sharpOptions.toFormat) || file.mimetype,
path: uri,
filename
});
});
}
_uploadWithMultipleSize(
sizes,
file,
filename,
gcName,
fileOptions,
stream,
cb
) {
Promise.map(
sizes,
this._eachUpload(file, filename, gcName, fileOptions, stream)
)
.then((results) => {
// All resolve, do something
const mapArrayToObject = results.reduce((acc, curr) => {
acc[curr.suffix] = {};
acc[curr.suffix].path = curr.path;
acc[curr.suffix].filename = curr.filename;
return acc;
}, {});
cb(null, mapArrayToObject);
return null;
})
.catch(cb);
}
}
MulterSharp.defaultOptions = defaultOptions;
module.exports = (options) => new MulterSharp(options);