-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (143 loc) · 3.56 KB
/
Copy pathindex.js
File metadata and controls
155 lines (143 loc) · 3.56 KB
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
'use strict';
const sharp = require('sharp');
module.exports = {
name: require('./package').name,
/**
* contains images as base64 encoded strings
*/
inlineImages: {},
/**
* array of already processed images
*/
processed: [],
included() {
this._super.included.apply(this, arguments);
let responsiveImageAddon = this.project.findAddonByName(
'ember-responsive-image'
);
if (responsiveImageAddon) {
responsiveImageAddon.addMetadataExtension(this.addMetaData, this);
responsiveImageAddon.addImagePreProcessor(this.imagePreProcessor, this);
}
},
/**
* Add metadata for LQIP-feature
*
* @param image
* @param metadata
* @param config
* @return {*} the metadata
* @throws Error if there's no generated image for inline option
* @private
*/
addMetaData(image, metadata, config) {
if (config.lqip) {
let width = this.getLqipWidth(config);
metadata.lqip = { width };
if (config.lqip.type === 'inline') {
if (!this.inlineImages[image]) {
throw Error(`There's no generated image for ${image}`);
}
metadata.lqip.image = this.inlineImages[image];
}
}
return metadata;
},
/**
* The image preprocessor provides the image data as a base64 string if necessary
*
* @param {sharp} sharped the sharp-object contains the image
* @param image the origin image name
* @param width
* @param config
* @return {sharp|Promise}
* @private
*/
imagePreProcessor(sharped, image, width, config) {
if (this.processed.indexOf(image) > -1 || !this.canProcessImage(config)) {
return sharped;
}
this.processed.push(image);
if (config.lqip && config.lqip.type === 'inline') {
return sharped
.toBuffer()
.then((buffer) => {
let quality = config.lqip.quality || config.quality;
return sharp(buffer)
.resize(this.getLqipWidth(config), null, {
withoutEnlargement: true,
})
.jpeg({
quality: quality,
progressive: true,
force: false,
})
.toBuffer();
})
.then((buffer) => {
this.inlineImages[image] = buffer.toString('base64');
return sharped;
});
} else {
return sharped;
}
},
/**
* Check if we have to add our custom image process
*
* @param config
* @return {boolean}
* @private
*/
canProcessImage(config) {
if (config.lqip) {
this.validateConfig(config);
return config.lqip.type === 'inline';
}
return false;
},
/**
* returns the width for lqip
*
* @param config
* return {Number} the width
*
* @private
*/
getLqipWidth(config) {
return (
config.lqip.width ||
config.supportedWidths.reduce(
(acc, val) => Math.min(acc, val),
Number.MAX_SAFE_INTEGER
)
);
},
/**
* validates the configuration
*
* @param config
* @throws Error if the config is wrong
*
* @private
*/
validateConfig(config) {
if (
!config.lqip.type ||
['inline', 'remote'].indexOf(config.lqip.type) < 0
) {
throw Error(
"You have to provide either 'inline' or 'remote' as 'type' to enable lqip"
);
}
if (
config.lqip.type === 'remote' &&
config.lqip.width &&
config.supportedWidths.indexOf(config.lqip.width) < 0
) {
throw Error(
"If you specify a 'width' on a 'lqip' 'remote'-type it has to be one of the 'supportedWidths'"
);
}
},
};