-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (245 loc) · 7.2 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
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
const serveStatic = require("../../../../current/core/shared/express").static;
const fs = require("fs-extra");
const path = require("path");
const Promise = require("bluebird");
const moment = require("moment");
const config = require("../../../../current/core/shared/config");
const { i18n } = require("../../../../current/core/server/lib/common");
const logging = require("../../../../current/core/shared/logging");
const errors = require("@tryghost/errors");
const constants = require("@tryghost/constants");
const urlUtils = require("../../../../current/core/shared/url-utils");
const StorageBase = require("ghost-storage-base");
class CompressedAdapter extends StorageBase {
constructor(options) {
super(options);
this.storagePath = config.getContentPath("images");
const adapter = options || {};
console.log(this.storagePath, adapter);
let imageminPlugins = [];
if (adapter.jpegtran && adapter.jpegtran.active) {
const imageminJpegtran = require("imagemin-jpegtran");
imageminPlugins.push(imageminJpegtran(adapter.jpegtran));
}
if (adapter.mozjpeg && adapter.mozjpeg.active) {
const imageminMozjpeg = require("imagemin-mozjpeg");
imageminPlugins.push(imageminMozjpeg(adapter.mozjpeg));
}
if (adapter.pngquant && adapter.pngquant.active) {
const imageminPngquant = require("imagemin-pngquant");
imageminPlugins.push(imageminPngquant(adapter.pngquant));
}
if (adapter.imageminOptipng && adapter.imageminOptipng.active) {
const imageminOptipng = require("imagemin-optipng");
imageminPlugins.push(imageminOptipng(adapter.imageminOptipng));
}
if (adapter.gifsicle && adapter.gifsicle.active) {
const imageminGifsicle = require("imagemin-gifsicle");
imageminPlugins.push(imageminGifsicle(adapter.gifsicle));
}
if (adapter.giflossy && adapter.giflossy.active) {
const imageminGiflossy = require("imagemin-giflossy");
imageminPlugins.push(imageminGiflossy(adapter.giflossy));
}
if (adapter.webp && adapter.webp.active) {
const imageminWebp = require("imagemin-webp");
imageminPlugins.push(imageminWebp(adapter.webp));
}
this.imageminPlugins = imageminPlugins;
}
/**
* Saves a buffer in the targetPath
* - buffer is an instance of Buffer
* - returns a Promise which returns the full URL to retrieve the data
*/
saveRaw(buffer, targetPath) {
console.log("saveRaw");
const storagePath = path.join(this.storagePath, targetPath);
const targetDir = path.dirname(storagePath);
return fs
.mkdirs(targetDir)
.then(() => {
return fs.writeFile(storagePath, buffer);
})
.then(() => {
// For local file system storage can use relative path so add a slash
const fullUrl = urlUtils
.urlJoin(
"/",
urlUtils.getSubdir(),
urlUtils.STATIC_IMAGE_URL_PREFIX,
targetPath
)
.replace(new RegExp(`\\${path.sep}`, "g"), "/");
return fullUrl;
});
}
/**
* Saves the image to storage (the file system)
* - image is the express image object
* - returns a promise which ultimately returns the full url to the uploaded image
*
* @param image
* @param targetDir
* @returns {*}
*/
save(image, targetDir) {
// console.log("save", image, targetDir);
let targetFilename;
// NOTE: the base implementation of `getTargetDir` returns the format this.storagePath/YYYY/MM
targetDir = targetDir || this.getTargetDir(this.storagePath);
return this.getUniqueFileName(image, targetDir)
.then((filename) => {
targetFilename = filename;
return fs.mkdirs(targetDir);
})
.then(() => {
const imagemin = require("imagemin");
return (async () => {
try {
const imageminPlugins = this.imageminPlugins;
console.log("imageminPlugins", imageminPlugins);
const files = await imagemin([image.path], {
plugins: imageminPlugins,
});
fs.writeFile(
targetFilename,
Buffer.from(files[0].data, "base64"),
function (err) {
if (err) console.log(err);
}
);
} catch (error) {
console.log(error);
}
})();
})
.then(() => {
// The src for the image must be in URI format, not a file system path, which in Windows uses \
// For local file system storage can use relative path so add a slash
const fullUrl = urlUtils
.urlJoin(
"/",
urlUtils.getSubdir(),
urlUtils.STATIC_IMAGE_URL_PREFIX,
path.relative(this.storagePath, targetFilename)
)
.replace(new RegExp(`\\${path.sep}`, "g"), "/");
return fullUrl;
})
.catch((e) => {
return Promise.reject(e);
});
}
exists(fileName, targetDir) {
const filePath = path.join(targetDir || this.storagePath, fileName);
return fs
.stat(filePath)
.then(() => {
return true;
})
.catch(() => {
return false;
});
}
/**
* For some reason send divides the max age number by 1000
* Fallthrough: false ensures that if an image isn't found, it automatically 404s
* Wrap server static errors
*
* @returns {serveStaticContent}
*/
serve() {
const { storagePath } = this;
return function serveStaticContent(req, res, next) {
const startedAtMoment = moment();
return serveStatic(storagePath, {
maxAge: constants.ONE_YEAR_MS,
fallthrough: false,
onEnd: () => {
logging.info(
"LocalFileStorage.serve",
req.path,
moment().diff(startedAtMoment, "ms") + "ms"
);
},
})(req, res, (err) => {
if (err) {
if (err.statusCode === 404) {
return next(
new errors.NotFoundError({
message: i18n.t("errors.errors.imageNotFound"),
code: "STATIC_FILE_NOT_FOUND",
property: err.path,
})
);
}
if (err.statusCode === 400) {
return next(new errors.BadRequestError({ err: err }));
}
if (err.statusCode === 403) {
return next(new errors.NoPermissionError({ err: err }));
}
return next(new errors.GhostError({ err: err }));
}
next();
});
};
}
/**
* Not implemented.
* @returns {Promise.<*>}
*/
delete() {
console.log("delete");
return Promise.reject("not implemented");
}
/**
* Reads bytes from disk for a target image
* - path of target image (without content path!)
*
* @param options
*/
read(options) {
console.log("read");
options = options || {};
// remove trailing slashes
options.path = (options.path || "").replace(/\/$|\\$/, "");
const targetPath = path.join(this.storagePath, options.path);
return new Promise((resolve, reject) => {
fs.readFile(targetPath, (err, bytes) => {
if (err) {
if (err.code === "ENOENT" || err.code === "ENOTDIR") {
return reject(
new errors.NotFoundError({
err: err,
message: i18n.t(
"errors.errors.imageNotFoundWithRef",
{ img: options.path }
),
})
);
}
if (err.code === "ENAMETOOLONG") {
return reject(new errors.BadRequestError({ err: err }));
}
if (err.code === "EACCES") {
return reject(
new errors.NoPermissionError({ err: err })
);
}
return reject(
new errors.GhostError({
err: err,
message: i18n.t("errors.errors.cannotReadImage", {
img: options.path,
}),
})
);
}
resolve(bytes);
});
});
}
}
module.exports = CompressedAdapter;