-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
459 lines (401 loc) · 14.2 KB
/
plugin.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* We.js local storage plugin
*/
const gm = require('gm'),
uuid = require('uuid'),
path = require('path'),
mkdirp = require('mkdirp'),
fs = require('fs'),
recreateAllImageSizes = require('./lib/recreateAllImageSizes.js'),
resetAllImageURLs = require('./lib/resetAllImageURLs.js'),
convertHeicToJPG = require('./lib/convertHeicToJPG.js');
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
plugin.urlImageUploader = require('./lib/urlImageUploader.js');
plugin.urlUploader = plugin.urlImageUploader
plugin.urlFileUploader = require('./lib/urlFileUploader.js');
plugin.rebuildLocalImagesStyles = require('./lib/rebuildLocalImagesStyles.js');
plugin.defaultFilename = function defaultFilename (req, file, cb) {
file.name = Date.now() + '_' + uuid.v1() + '.' + (file.originalname.split('.').pop().toLowerCase());
cb(null, file.name);
}
plugin.getMulter = function() {
if (plugin.we.plugins['we-plugin-file']) {
return plugin.we.plugins['we-plugin-file'].multer;
} else if (plugin.we.plugins.project.multer) {
return plugin.we.plugins.project.multer;
} else {
plugin.we.log.warn('Multer not found');
}
}
// set plugin configs
plugin.setConfigs({
fileHostname: null, // config to set generic file hostname, if you dont want set an hostname set this to ""
fileImageHostname: null, // config to set image hostname, if you dont want set an hostname set this to ""
imageMaxSize: {
width: '1920',
height: '1440'
},
imageResizeStrategy: 'cut',
imageFillBG: '#ffffff',
upload: {
defaultImageStorage: 'localImages',
defaultFileStorage: 'localFiles',
file: {
uploadPath: projectPath + '/files/uploads/files'
},
image: {
uploadPath: projectPath + '/files/uploads/images'
},
storages: {
localImages: {
isLocalStorage: true,
getStorage() {
const multer = plugin.getMulter();
if (!multer) return;
return multer
.diskStorage({
destination: this.getDestination('original'),
filename: plugin.defaultFilename
});
},
/**
* Send one file to user
*
* @param {Object} image
* @param {Object} req
* @param {Object} res
* @param {String} style
*/
sendFile(image, req, res, style) {
const we = plugin.we;
const path = this.getPath(style, image.name);
// check if file exists with fs.stat
fs.stat(path, function afterCheckIfFileExists (err) {
if (err) {
return res.serverError(err);
} else {
const stream = fs.ReadStream(path);
if (image.mime) {
res.contentType(image.mime);
} else {
res.contentType('image/png');
}
// set http cache headers
if (!res.getHeader('Cache-Control')) {
res.setHeader('Cache-Control', 'public, max-age=' + we.config.cache.maxage);
}
stream.pipe(res);
stream.on('error', (err)=> {
we.log.error('image:findOne: error in send file', { error: err });
});
}
});
},
destroyFile(file, done) {
const self = this
plugin.we.utils.async.eachSeries(
plugin.we.config.upload.image.avaibleStyles,
function onEach (style, done) {
self.destroyOneImage(file, style, done);
},
function afterEachStyle (err) {
if (err) return done(err);
// delete the original format
self.destroyOneImage(file, 'original', done);
}
);
},
destroyOneImage(file, style, done) {
const path = this.getPath(style, file.name);
fs.exists(path, function afterCheckIfFileExists(exists) {
if (!exists) return done();
fs.unlink(path, done);
})
},
getUrlFromFile(format, file) {
if (typeof plugin.we.config.fileImageHostname == 'string') {
return plugin.we.config.fileImageHostname+'/api/v1/image/' + (format || 'original') + '/' + file.name;
}
return '/api/v1/image/' + (format || 'original') + '/' + file.name;
},
getDestination(style) {
if (!style) style = 'original';
return plugin.we.config.upload.image.uploadPath + '/' + style + '/';
},
getPath(style, name) {
return path.join(
this.getDestination(style),
name
);
},
generateImageStyles(file, done) {
const we = plugin.we;
this.convertFileIfIsNeed(file, (err)=> {
if (err) return done(err);
we.utils.async.eachSeries(
we.config.upload.image.avaibleStyles,
this.resizeEach.bind({ file: file, uploader: this }),
(err)=> {
if (err) return done(err);
this.resizeOriginalIfNeed(file, done);
}
);
});
},
resizeOriginalIfNeed(file, done) {
// max sizes
const ms = plugin.we.config.imageMaxSize;
gm(file.path)
.identify( (err, data)=> {
if (err) return done(err);
const ed = file.extraData || {};
ed.size = data.size;
file.extraData = ed;
if (
data.size.width > ms.width ||
data.size.height > ms.height
) {
gm(file.path)
.autoOrient()
.resize(ms.width, ms.height)
.gravity('Center')
.crop(ms.width, ms.height)
.write(file.path, done);
} else {
// dont need resize:
done();
}
});
},
/**
* Resize one image to fit image style size
*
* @param {String} imageStyle
* @param {Function} next callback
*/
resizeEach(imageStyle, next) {
const styles = plugin.we.config.upload.image.styles;
const originalFile = this.uploader.getPath('original', this.file.name);
const newImagePath = this.uploader.getPath(imageStyle, this.file.name);
// set new image style to save in DB
const urls = this.file.urls || {};
urls[imageStyle] = this.uploader.getUrlFromFile (imageStyle, this.file);
this.file.urls = urls;
const width = styles[imageStyle].width;
const height = (styles[imageStyle].height || styles[imageStyle].heigth);
// resize, center and crop to fit size
this.uploader.resizeImage(
originalFile, width, height, newImagePath, next
);
},
resizeImage(orig, width, height, dest, cb) {
if (
plugin.we.config.imageResizeStrategy == 'fill') {
this.resizeWithFill(
orig, width, height, dest, cb
);
} else {
// resize, center and crop to fit size
gm(orig)
.autoOrient()
.resize(width, height, '^')
.gravity('Center')
.crop(width, height)
.write(dest, cb);
}
},
resizeWithFill(orig, width, height, dest, cb) {
gm(orig)
.autoOrient()
.resize(width, height)
.gravity('Center')
.background(plugin.we.config.imageFillBG)
.extent(width, height)
.write(dest, cb);
},
convertFileIfIsNeed(file, cb) {
if (
file.mime === 'image/heif' ||
file.mime === 'image/heic'
) {
return this.convertHeifToJPG(file, cb);
}
return cb();
},
convertHeifToJPG(file, cb) {
const log = plugin.we.log;
log.verbose('convertHeifToJPG will convert file', { file: file });
const oldPath = file.path;
let dest = file.path.replace('heic', 'jpg').replace('heif', 'jpg');
log.verbose('convertHeifToJPG starting conversor', { file: file });
convertHeicToJPG(file.path, dest, plugin.we, (err)=> {
if (err) {
log.error('convertHeicToJPG error on convert image', {
file,
dest,
error: err
});
return cb(err);
}
file.path = dest;
file.name = file.name.replace('heic', 'jpg').replace('heif', 'jpg');
file.filename = file.name;
file.mimetype = 'image/jpeg';
file.mime = file.mimetype;
for(let name in file.urls) {
file.urls[name] = file.urls[name].replace('heic', 'jpg').replace('heif', 'jpg');
}
const stats = fs.statSync(dest);
file.size = stats.size;
fs.unlinkSync(oldPath);
cb();
});
}
},
localFiles: {
isLocalStorage: true,
getStorage() {
const multer = plugin.getMulter()
if (!multer) return;
return multer
.diskStorage({
destination: this.getDestination(),
// projectPath + '/files/uploads/files',
filename: plugin.defaultFilename
});
},
/**
* Send one file to user
*
* @param {Object} file
* @param {Object} req
* @param {Object} res
* @param {String} style
*/
sendFile(file, req, res) {
const we = plugin.we;
const path = this.getPath(null, file.name);
// check if file exists with fs.stat
fs.stat(path, function afterCheckIfFileExists (err) {
if (err) {
return res.notFound(err)
}
const stream = fs.ReadStream(path);
if (file.mime) res.contentType(file.mime);
// set http cache headers
if (!res.getHeader('Cache-Control')) {
res.setHeader('Cache-Control', 'public, max-age=' + we.config.cache.maxage)
}
stream.pipe(res);
stream.on('error', function onError(err) {
we.log.error('file:findOne: error in send file', { error: err })
res.serverError(err);
});
})
},
destroyFile(file, done) {
const path = this.getPath(null, file.name);
fs.exists(path, function afterCheckIfFileExists(exists) {
if (!exists) return done();
fs.unlink(path, done);
});
},
getUrlFromFile(format, file) {
if (typeof plugin.we.config.fileHostname == 'string') {
return plugin.we.config.fileHostname+'/api/v1/file-download/' + file.name;
}
return '/api/v1/file-download/' + file.name;
},
getDestination() {
return plugin.we.config.upload.file.uploadPath + '/';
},
getPath(style, name) {
return path.join(
this.getDestination(style),
name
);
}
}
}
}
});
/**
* Plugin fast loader for speed up We.js project bootstrap
*
* @param {Object} we
* @param {Function} done callback
*/
plugin.fastLoader = function fastLoader(we, done) {
// - Controllers:
we.controllers.imageLocal = new we.class.Controller({
recreateAllForOneStyle(req, res) {
recreateAllImageSizes(req.we, req.params.style, (err, result)=> {
if (err) return res.queryError(err);
res.send({ totalResized: result.count });
});
},
resetAllUrls(req, res) {
resetAllImageURLs(req.we, (err, result)=> {
if (err) return res.queryError(err);
res.send({ totalResized: result.count });
});
}
});
done();
}
plugin.setRoutes({
'get /image-local/resize-all/:style': {
'controller': 'imageLocal',
'action': 'recreateAllForOneStyle',
'permission': 'image_resizeAll',
'responseType': 'json'
},
'get /image-local/reset-all-urls': {
'controller': 'imageLocal',
'action': 'resetAllUrls',
'permission': 'image_resetAllUrls',
'responseType': 'json'
}
});
/**
* Create file and image upload Folders
* For local storage
*
* @param {Object} we
* @param {Function} done
*/
plugin.createFileFolder = function createFileFolder (we, done) {
// create file upload path
mkdirp(we.config.upload.file.uploadPath)
.catch((err)=> {
if (err) we.log.error('Error on create file upload path', { error: err });
});
// create image upload path
const imageStyles = we.config.upload.image.avaibleStyles
we.utils.async.each(imageStyles, (style, next)=> {
const imageDir = we.config.upload.image.uploadPath + '/' + style
fs.lstat(imageDir, (err)=> {
if (err) {
if (err.code === 'ENOENT') {
we.log.info('Creating the image upload directory: ' + imageDir)
return mkdirp(imageDir)
.then(()=> {
return next();
})
.catch(()=> {
if (err) we.log.error('Error on create upload path', { error: err });
return next();
});
}
we.log.error('Error on create image dir: ', { imageDir });
return next(err);
} else {
next();
}
});
}, done);
}
plugin.hooks.on('we:create:default:folders', plugin.createFileFolder);
return plugin;
};