-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (156 loc) · 6.5 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
// Copyright (C) 2016 Glamping Hub (https://glampinghub.com)
// License: BSD 3-Clause
/*jslint node: true, single: true, this: true */
'use strict';
var path = require('path');
var fs = require('fs');
var BaseStore = require('ghost-storage-base');
var Promise = require('bluebird');
var tmp = require('tmp');
var sharp = require('sharp');
var imagemin = require('imagemin');
var imageminGifsicle = require('imagemin-gifsicle');
var imageminJpegtran = require('imagemin-jpegtran');
var imageminOptipng = require('imagemin-optipng');
try {
var config = require(path.join(process.cwd(), 'core/server/config')); // for ghost-docker
} catch (e) {
var config = require(path.join(process.cwd(), 'current/core/server/config'));
}
tmp.setGracefulCleanup();
class MinifyStore extends BaseStore{
constructor(storageConfig) {
super(storageConfig);
// Configure next storage instance
if (!storageConfig || !storageConfig.hasOwnProperty('nextStorage')) {
throw 'You must configure the "nextStorage" property for the "Ghost Minified Storage"!';
}
var NextStorage = require(path.join(config.getContentPath('storage'), storageConfig.nextStorage));
var configStorage = config.get('storage');
this.nextStorageInstance = new NextStorage(configStorage[storageConfig.nextStorage]);
// Configure each property
var that = this;
[
{name: 'saveOriginalFile', defaultValue: 'true'},
{name: 'deleteOriginalFile', defaultValue: true},
{name: 'maxWidth', defaultValue: false},
{name: 'maxHeight', defaultValue: false},
{name: 'regExWithoutSuffix', defaultValue: /^.+(\.[^\.]+)$/},
{name: 'regExResultWithoutSuffix', defaultValue: '$1$2'},
{name: 'regExWithSuffix', defaultValue: /^.+_resized(\.[^\.]+)$/},
{name: 'regExResultWithSuffix', defaultValue: '$1_resized$2'}
].forEach(function (property) {
that[property.name] = storageConfig.hasOwnProperty(property.name)
? storageConfig[property.name]
: property.defaultValue;
});
}
exists() {
return this.nextStorageInstance.exists.apply(this.nextStorageInstance, arguments);
}
save(file, targetDir) {
/* file =
< { fieldname: 'uploadimage',
< originalname: 'example.png',
< encoding: '7bit',
< mimetype: 'image/png',
< destination: '/tmp',
< filename: '67cc4b2c69cbf07d5839fbdb8ec76c44',
< path: '/tmp/67cc4b2c69cbf07d5839fbdb8ec76c44',
< size: 371703,
< name: 'example.png',
< type: 'image/png',
< context: { user: 1, client: null } }
*/
var that = this;
var resizeImageIfNeeds = function (src, resolve, reject) {
try {
// Resize image
var buffer = sharp(src)
.png({
progressive: true,
compressionLevel: 9
})
.jpeg({
quality: 80,
progressive: true,
optimizeScans: true
});
if (that.maxWidth || that.maxHeight) {
buffer = buffer.resize(that.maxWidth, that.maxHeight).max();
}
buffer.toBuffer(function (err, buffer, info) {
if (err) {
throw err;
}
resolve(buffer, info);
});
} catch (e) {
reject(e);
}
};
var minifyImage = function (buffer, resolve, reject) {
tmp.file(function (err, temporalFilePath, ignore, cleanupCallback) {
if (err) {
throw err;
}
try {
imagemin.buffer(buffer, {
plugins: [
imageminGifsicle(),
imageminJpegtran(),
imageminOptipng()
]
}).then(function (buffer) { // files = [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …]
fs.writeFile(temporalFilePath, buffer, function (err) {
if (err) {
throw err;
}
var newFileObject = JSON.parse(JSON.stringify(file)); // JS clone stuff
newFileObject.originalname = file.originalname.replace(that.regExWithoutSuffix, that.regExResultWithSuffix);
newFileObject.filename = path.basename(temporalFilePath);
newFileObject.path = temporalFilePath;
newFileObject.size = buffer.length;
resolve(newFileObject)
.then(cleanupCallback, cleanupCallback)
.catch(cleanupCallback);
});
});
} catch (e) {
cleanupCallback();
reject(e);
}
});
};
return new Promise(function (resolve, reject) {
// Save original image if needs
if (that.saveOriginalFile) {
that.nextStorageInstance.save(file, targetDir);
}
resizeImageIfNeeds(file.path, function (buffer) {
minifyImage(buffer, function (file) {
return that.nextStorageInstance
.save(file, targetDir)
.then(function (file2) {
resolve(file2);
});
}, reject);
}, reject);
});
}
serve() {
return this.nextStorageInstance.serve.apply(this.nextStorageInstance, arguments);
}
delete(fileName, targetDir) {
var promises = [this.nextStorageInstance.delete.apply(this.nextStorageInstance, arguments)];
// Delete original image if needs
if (this.deleteOriginalFile) {
promises.push(this.nextStorageInstance.delete(fileName.replace(this.regExWithSuffix, this.regExResultWithoutSuffix), targetDir));
}
return Promise.all(promises);
}
read() {
return this.nextStorageInstance.read.apply(this.nextStorageInstance, arguments);
}
}
module.exports = MinifyStore;