-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
images.js
76 lines (63 loc) · 2.96 KB
/
images.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
/* eslint-disable ghost/ghost-custom/max-api-complexity */
const storage = require('../../adapters/storage');
const imageTransform = require('@tryghost/image-transform');
const config = require('../../../shared/config');
const path = require('path');
module.exports = {
docName: 'images',
upload: {
statusCode: 201,
headers: {
cacheInvalidate: false
},
permissions: false,
async query(frame) {
const store = storage.getStorage('images');
// Normalize
const imageOptimizationOptions = config.get('imageOptimization');
// Trim _o from file name (not allowed suffix)
frame.file.name = frame.file.name.replace(/_o(\.\w+?)$/, '$1');
// CASE: image transform is not capable of transforming file (e.g. .gif)
if (imageTransform.shouldResizeFileExtension(frame.file.ext) && imageOptimizationOptions.resize) {
const out = `${frame.file.path}_processed`;
const originalPath = frame.file.path;
const options = Object.assign({
in: originalPath,
out,
ext: frame.file.ext,
width: config.get('imageOptimization:defaultMaxWidth')
}, imageOptimizationOptions);
try {
await imageTransform.resizeFromPath(options);
} catch (err) {
// If the image processing fails, we just want to store the original image
return store.save(frame.file);
}
// Store the processed/optimized image
const processedImageUrl = await store.save({
...frame.file,
path: out
});
let processedImageName = path.basename(processedImageUrl);
let processedImageDir = undefined;
if (store.urlToPath) {
// Currently urlToPath is not part of StorageBase, so not all storage provider have implemented it
const processedImagePath = store.urlToPath(processedImageUrl);
// Get the path and name of the processed image
// We want to store the original image on the same name + _o
// So we need to wait for the first store to finish before generating the name of the original image
processedImageName = path.basename(processedImagePath);
processedImageDir = path.dirname(processedImagePath);
}
// Store the original image
await store.save({
...frame.file,
path: originalPath,
name: imageTransform.generateOriginalImageName(processedImageName)
}, processedImageDir);
return processedImageUrl;
}
return store.save(frame.file);
}
}
};